I started developping an image manipulation library and decided to go with Scrutinizer for the testing and inspection in one cool package but i'm not sure how to optimize my code to limit some of the stats that are degrading the overall score.
The code i'm showing you below has "Conditions: 6" which pushes it to "B" rating. There isn't anything special about the code, it checks for potential missing permissions or invalid directories and throw exceptions accordingly, so lowering the score there is quite hard without making the code react completely differently:
/**
* 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);
}
I don't understand why i have 6 conditions while there are only 3 conditions in there. And i don't understand how i could lower this!