【问题标题】:Scrutinizer score optimization审查员分数优化
【发布时间】:2015-07-14 17:04:08
【问题描述】:

我开始开发一个图像处理库,并决定使用 Scrutinizer 在一个很酷的包中进行测试和检查,但我不确定如何优化我的代码以限制一些降低整体分数的统计数据。

我在下面向您展示的代码具有“条件:6”,将其推至“B”等级。代码没有什么特别之处,它会检查潜在的丢失权限或无效目录并相应地抛出异常,因此在不使代码反应完全不同的情况下降低分数是相当困难的:

/**
 * Writes the image to a writable source
 *
 * @param int $compression Compression level 0-9, defaults to 9
 *
 * @throws \StandardExceptions\IOExceptions\FileNotWritableException
 * @throws \StandardExceptions\IOExceptions\DirectoryNotFoundException
 * @throws \StandardExceptions\IOExceptions\DirectoryNotWritableException
 */
public function write($compression = 9)
{
    $path = $this->getPath();
    $directory = $path->getPathInfo();
    if (file_exists($path->getPathname()) && !$path->isWritable()) {
        throw new FileNotWritableException();
    } elseif (!is_dir($directory->getPathname())) {
        throw new DirectoryNotFoundException();
    } elseif (is_dir($directory->getPathname()) && !$directory->isWritable()) {
        throw new DirectoryNotWritableException();
    }
    $options = [
        'png_compression_level' => $compression,
        'resolution-x' => $this->getDensity()->getDensity(),
        'resolution-y' => $this->getDensity()->getDensity(),
        'resolution-units' => $this->mapDensity($this->getDensity()),
    ];
    $this->getImage()->save($path, $options);
}

我不明白为什么我有 6 个条件,而那里只有 3 个条件。我不明白我怎么能降低这个!

【问题讨论】:

    标签: php scrutinizer


    【解决方案1】:

    我试图将我的代码拆分为更多的函数,这现在使代码在 IMO 的可读性降低,并在类中创建了许多无用的函数,但至少它可以工作,并且我能够将每个块中的条件减少到 1 或 2的代码有效地将我的代码带到“A”。

    /**
     * Writes the image to a writable source
     *
     * @param int $compression Compression level 0-9, defaults to 9
     */
    public function write($compression = 9)
    {
        $this->checkFileIsWritable();
        $this->checkDirectoryExists();
        $this->checkDirectoryIsWritable();
        $options = [
            'png_compression_level' => $compression,
            'resolution-x' => $this->getDensity()->getDensity(),
            'resolution-y' => $this->getDensity()->getDensity(),
            'resolution-units' => $this->mapDensity($this->getDensity()),
        ];
        $this->getImage()->save($this->getPath(), $options);
    }
    
    /**
     * Checks that the path is valid and throws exceptions
     * if there is something wrong
     *
     * @throws \StandardExceptions\IOExceptions\FileNotWritableException
     */
    public function checkFileIsWritable()
    {
        $path = $this->getPath();
        if (file_exists($path->getPathname()) && !$path->isWritable()) {
            throw new FileNotWritableException();
        }
    }
    
    /**
     * Checks that the path is valid and throws exceptions
     * if there is something wrong
     *
     * @throws \StandardExceptions\IOExceptions\DirectoryNotFoundException
     */
    public function checkDirectoryExists()
    {
        $path = $this->getPath();
        $directory = $path->getPathInfo();
        if (!is_dir($directory->getPathname())) {
            throw new DirectoryNotFoundException();
        }
    }
    
    /**
     * Checks that the is writable
     *
     * @throws \StandardExceptions\IOExceptions\DirectoryNotWritableException
     */
    public function checkDirectoryIsWritable()
    {
        $path = $this->getPath();
        $directory = $path->getPathInfo();
        if (is_dir($directory->getPathname()) && !$directory->isWritable()) {
            throw new DirectoryNotWritableException();
        }
    }
    

    【讨论】:

    • 您现在拥有的三行是保护子句-您可以随时将三行拆分为一个新函数-可能类似于isFileWritable()。即使这些行仍然存在 - 它更具可读性 - 但这是因为您不必阅读这三个新功能。
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2017-12-05
    • 1970-01-01
    • 2012-05-19
    • 2023-02-15
    相关资源
    最近更新 更多