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