0

我开始开发一个图像处理库,并决定在一个很酷的包中使用 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 个条件。我不明白我怎么能降低这个!

4

1 回答 1

0

我试图将我的代码拆分成更多的函数,这现在使代码在 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();
    }
}
于 2015-07-14T18:53:32.740 回答