Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 监查者分数优化_Php_Scrutinizer - Fatal编程技术网

Php 监查者分数优化

Php 监查者分数优化,php,scrutinizer,Php,Scrutinizer,我开始开发一个图像处理库,并决定在一个很酷的软件包中使用Scrumnizer进行测试和检查,但我不确定如何优化我的代码,以限制某些降低总分的统计数据 我在下面向您展示的代码有“条件:6”,这将它推到“B”等级。代码没有任何特殊之处,它会检查可能丢失的权限或无效目录,并相应地抛出异常,因此在不使代码做出完全不同的反应的情况下,很难降低分数: /** * Writes the image to a writable source * * @param int $compression Comp

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

我在下面向您展示的代码有“条件: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个条件。我不明白我怎么能降低这个

我尝试将代码拆分为更多的函数,这会降低代码的可读性,并在类中创建许多无用的函数,但至少它可以工作,并且我能够将每个代码块中的条件减少到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();
    }
}

您现在拥有的三行是guard子句—您可以随时将这三行拆分为一个新函数—可能类似于
isfilewriteable()
。即使行仍然在那里-它更可读-但这是因为您不必阅读这三个新函数。