| Total Complexity | 40 |
| Total Lines | 279 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like QRGdImage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QRGdImage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class QRGdImage extends QROutputAbstract{ |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The GD image resource |
||
| 31 | * |
||
| 32 | * @see imagecreatetruecolor() |
||
| 33 | * @var resource|\GdImage |
||
| 34 | */ |
||
| 35 | protected $image; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The allocated background color |
||
| 39 | * |
||
| 40 | * @see \imagecolorallocate() |
||
| 41 | */ |
||
| 42 | protected int $background; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Whether we're running in upscale mode (scale < 20) |
||
| 46 | * |
||
| 47 | * @see \chillerlan\QRCode\QROptions::$drawCircularModules |
||
| 48 | */ |
||
| 49 | protected bool $upscaled = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritDoc |
||
| 53 | * |
||
| 54 | * @throws \chillerlan\QRCode\Output\QRCodeOutputException |
||
| 55 | * @noinspection PhpMissingParentConstructorInspection |
||
| 56 | */ |
||
| 57 | public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){ |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @inheritDoc |
||
| 84 | */ |
||
| 85 | public static function moduleValueIsValid($value):bool{ |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $value |
||
| 109 | * |
||
| 110 | * @inheritDoc |
||
| 111 | * @throws \chillerlan\QRCode\Output\QRCodeOutputException |
||
| 112 | */ |
||
| 113 | protected function prepareModuleValue($value):int{ |
||
| 114 | $values = []; |
||
| 115 | |||
| 116 | foreach(array_values($value) as $i => $val){ |
||
| 117 | |||
| 118 | if($i > 2){ |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | |||
| 122 | $values[] = max(0, min(255, intval($val))); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** @phan-suppress-next-line PhanParamTooFewInternalUnpack */ |
||
| 126 | $color = imagecolorallocate($this->image, ...$values); |
||
|
|
|||
| 127 | |||
| 128 | if($color === false){ |
||
| 129 | throw new QRCodeOutputException('could not set color: imagecolorallocate() error'); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $color; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritDoc |
||
| 137 | */ |
||
| 138 | protected function getDefaultModuleValue(bool $isDark):int{ |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @inheritDoc |
||
| 144 | * |
||
| 145 | * @return string|resource|\GdImage |
||
| 146 | * |
||
| 147 | * @phan-suppress PhanUndeclaredTypeReturnType, PhanTypeMismatchReturn |
||
| 148 | * @throws \ErrorException |
||
| 149 | */ |
||
| 150 | public function dump(string $file = null){ |
||
| 151 | |||
| 152 | /** @phan-suppress-next-line PhanTypeMismatchArgumentInternal */ |
||
| 153 | set_error_handler(function(int $severity, string $msg, string $file, int $line):void{ |
||
| 154 | throw new ErrorException($msg, 0, $severity, $file, $line); |
||
| 155 | }); |
||
| 156 | |||
| 157 | $this->setBgColor(); |
||
| 158 | |||
| 159 | imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $this->background); |
||
| 160 | |||
| 161 | $this->drawImage(); |
||
| 162 | |||
| 163 | if($this->upscaled){ |
||
| 164 | // scale down to the expected size |
||
| 165 | $this->image = imagescale($this->image, ($this->length / 10), ($this->length / 10)); |
||
| 166 | $this->upscaled = false; |
||
| 167 | } |
||
| 168 | |||
| 169 | // set transparency after scaling, otherwise it would be undone |
||
| 170 | // @see https://www.php.net/manual/en/function.imagecolortransparent.php#77099 |
||
| 171 | $this->setTransparencyColor(); |
||
| 172 | |||
| 173 | if($this->options->returnResource){ |
||
| 174 | restore_error_handler(); |
||
| 175 | |||
| 176 | return $this->image; |
||
| 177 | } |
||
| 178 | |||
| 179 | $imageData = $this->dumpImage(); |
||
| 180 | |||
| 181 | $this->saveToFile($imageData, $file); |
||
| 182 | |||
| 183 | if($this->options->imageBase64){ |
||
| 184 | $imageData = $this->toBase64DataURI($imageData, 'image/'.$this->options->outputType); |
||
| 185 | } |
||
| 186 | |||
| 187 | restore_error_handler(); |
||
| 188 | |||
| 189 | return $imageData; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sets the background color |
||
| 194 | */ |
||
| 195 | protected function setBgColor():void{ |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Sets the transparency color |
||
| 212 | */ |
||
| 213 | protected function setTransparencyColor():void{ |
||
| 214 | |||
| 215 | if($this->options->outputType === QROutputInterface::GDIMAGE_JPG || !$this->options->imageTransparent){ |
||
| 216 | return; |
||
| 217 | } |
||
| 218 | |||
| 219 | $transparencyColor = $this->background; |
||
| 220 | |||
| 221 | if($this::moduleValueIsValid($this->options->transparencyColor)){ |
||
| 222 | $transparencyColor = $this->prepareModuleValue($this->options->transparencyColor); |
||
| 223 | } |
||
| 224 | |||
| 225 | imagecolortransparent($this->image, $transparencyColor); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Creates the QR image |
||
| 230 | */ |
||
| 231 | protected function drawImage():void{ |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Creates a single QR pixel with the given settings |
||
| 241 | */ |
||
| 242 | protected function setPixel(int $x, int $y):void{ |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Creates the final image by calling the desired GD output function |
||
| 271 | * |
||
| 272 | * @throws \chillerlan\QRCode\Output\QRCodeOutputException |
||
| 273 | */ |
||
| 274 | protected function dumpImage():string{ |
||
| 309 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.