Complex classes like ImageProcessing 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ImageProcessing, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 27 | class ImageProcessing | ||
| 28 | { | ||
| 29 | /** | ||
| 30 | * @var AbstractImagine | ||
| 31 | */ | ||
| 32 | protected $imagine; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * @var ImageInterface | ||
| 36 | */ | ||
| 37 | protected $image; | ||
| 38 | |||
| 39 | |||
| 40 | /** | ||
| 41 | * constructor | ||
| 42 | * | ||
| 43 | * @param AbstractImagine | ||
| 44 | */ | ||
| 45 | 60 | public function __construct(AbstractImagine $imagine) | |
| 49 | |||
| 50 | /** | ||
| 51 | * set the imagine service | ||
| 52 | * | ||
| 53 | * @param AbstractImagine $imagine | ||
| 54 | * @return $this | ||
| 55 | */ | ||
| 56 | 60 | public function setImagineService(AbstractImagine $imagine) | |
| 57 |     { | ||
| 58 | 60 | $this->imagine = $imagine; | |
| 59 | |||
| 60 | 60 | return $this; | |
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Get the imagine service | ||
| 65 | * | ||
| 66 | * @return AbstractImagine | ||
| 67 | */ | ||
| 68 | 63 | public function getImagineService() | |
| 72 | |||
| 73 | /** | ||
| 74 | * Process command to image source and save to target | ||
| 75 | * | ||
| 76 | * @param string $source | ||
| 77 | * @param string $target | ||
| 78 | * @param string $commands | ||
| 79 | * | ||
| 80 | * @return void | ||
| 81 | */ | ||
| 82 | 54 | public function process($source, $target, $commands) | |
| 98 | |||
| 99 | /** | ||
| 100 | * Process command to create 404 image and save to target | ||
| 101 | * | ||
| 102 | * @param string $target | ||
| 103 | * @param string $commands | ||
| 104 | * | ||
| 105 | * @return void | ||
| 106 | */ | ||
| 107 | 6 | public function process404($target, $commands) | |
| 108 |     { | ||
| 109 | 6 |         if (file_exists($target)) { | |
| 110 | 3 | return; | |
| 111 | } | ||
| 112 | |||
| 113 | 6 | $targetFolder = pathinfo($target, PATHINFO_DIRNAME); | |
| 114 | 6 |         if (!file_exists($targetFolder)) { | |
| 115 | 6 | mkdir($targetFolder, 0777, true); | |
| 116 | 6 | } | |
| 117 | |||
| 118 | 6 | $text = 'Not found'; | |
| 119 | 6 | $backgroundColor = null; | |
| 120 | 6 | $color = null; | |
| 121 | 6 | $width = null; | |
| 122 | 6 | $height = null; | |
| 123 | 6 |         foreach ($this->analyseCommands($commands) as $command) { | |
| 124 | 6 |             if ('thumb' === $command['command'] || 'resize' === $command['command']) { | |
| 125 | 6 | $width = $command['params'][0]; | |
| 126 | 6 | $height = $command['params'][1]; | |
| 127 | |||
| 128 | 6 |             } elseif ('404' === $command['command']) { | |
| 129 | 3 |                 if (isset($command['params'][0]) && UrlSafeBase64::valid($command['params'][0])) { | |
| 130 | 3 | $text = UrlSafeBase64::decode($command['params'][0]); | |
| 131 | 3 | } | |
| 132 | 3 |                 if (isset($command['params'][1])) { | |
| 133 | 3 | $backgroundColor = $command['params'][1]; | |
| 134 | 3 | } | |
| 135 | 3 |                 if (isset($command['params'][2])) { | |
| 136 | 3 | $color = $command['params'][2]; | |
| 137 | 6 | } | |
| 138 | 3 |                 if (isset($command['params'][3])) { | |
| 139 | 3 | $width = $command['params'][3]; | |
| 140 | 3 | } | |
| 141 | 3 |                 if (isset($command['params'][4])) { | |
| 142 | 3 | $height = $command['params'][4]; | |
| 143 | 3 | } | |
| 144 | 3 | } | |
| 145 | 6 | } | |
| 146 | 6 | $this->image404($text, $backgroundColor, $color, $width, $height); | |
| 147 | |||
| 148 | 6 | $this->image->save($target); | |
| 149 | 6 | } | |
| 150 | |||
| 151 | /** | ||
| 152 | * Analyse commands string and returns array with command/params keys | ||
| 153 | * | ||
| 154 | * @param string $commands | ||
| 155 | * | ||
| 156 | * @return array | ||
| 157 | */ | ||
| 158 | 60 | protected function analyseCommands($commands) | |
| 159 |     { | ||
| 160 | 60 | $commandList = []; | |
| 161 | 60 |         foreach (explode('$', $commands) as $commandLine) { | |
| 162 | 60 |             $params = explode(',', $commandLine); | |
| 163 | 60 | $command = array_shift($params); | |
| 164 | 60 | $commandList[] = [ | |
| 165 | 60 | 'command' => $command, | |
| 166 | 60 | 'params' => $params, | |
| 167 | ]; | ||
| 168 | 60 | } | |
| 169 | 60 | return $commandList; | |
| 170 | } | ||
| 171 | |||
| 172 | /** | ||
| 173 | * Run command if exists | ||
| 174 | * | ||
| 175 | * @param array $command | ||
| 176 | * | ||
| 177 | * @return boolean | ||
| 178 | */ | ||
| 179 | 54 | protected function runCommand($command) | |
| 180 |     { | ||
| 181 | 54 | $method = 'image' . ucfirst(strtolower($command['command'])); | |
| 182 | 54 |         if (!method_exists($this, $method)) { | |
| 183 | 6 | return false; | |
| 184 | } | ||
| 185 | 48 | call_user_func_array([$this, $method], $command['params']); | |
| 186 | |||
| 187 | 33 | return true; | |
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Run custom command if exists | ||
| 192 | * | ||
| 193 | * @param array $command | ||
| 194 | * | ||
| 195 | * @return boolean | ||
| 196 | */ | ||
| 197 | 6 | protected function runCustomCommand($command) | |
| 198 |     { | ||
| 199 | 6 |         if (!CommandRegistry::hasCommand($command['command'])) { | |
| 200 | 3 |             throw new BadMethodCallException('Command "' . $command['command'] . '" not found'); | |
| 201 | } | ||
| 202 | 3 | $customCommand = CommandRegistry::getCommand($command['command']); | |
| 203 | |||
| 204 | 3 | array_unshift($command['params'], $this->image); | |
| 205 | 3 | call_user_func_array($customCommand, $command['params']); | |
| 206 | |||
| 207 | 3 | return true; | |
| 208 | } | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Command image thumb | ||
| 212 | * | ||
| 213 | * @param int $width | ||
| 214 | * @param int $height | ||
| 215 | * | ||
| 216 | * @return void | ||
| 217 | */ | ||
| 218 | 18 | protected function imageThumb($width, $height) | |
| 219 |     { | ||
| 220 | 18 | $width = (int) $width; | |
| 221 | 18 | $height = (int) $height; | |
| 222 | 18 |         if ($width <= 0) { | |
| 223 | 3 |             throw new BadMethodCallException('Invalid parameter width for command "thumb"'); | |
| 224 | } | ||
| 225 | 15 |         if ($height <= 0) { | |
| 226 | 3 |             throw new BadMethodCallException('Invalid parameter height for command "thumb"'); | |
| 227 | } | ||
| 228 | 12 | $this->image = $this->image->thumbnail(new Box($width, $height)); | |
| 229 | 12 | } | |
| 230 | |||
| 231 | /** | ||
| 232 | * Command image resize | ||
| 233 | * | ||
| 234 | * @param int $width | ||
| 235 | * @param int $height | ||
| 236 | * | ||
| 237 | * @return void | ||
| 238 | */ | ||
| 239 | 9 | protected function imageResize($width, $height) | |
| 240 |     { | ||
| 241 | 9 | $width = (int) $width; | |
| 242 | 9 | $height = (int) $height; | |
| 243 | 9 |         if ($width <= 0) { | |
| 244 | 3 |             throw new BadMethodCallException('Invalid parameter width for command "resize"'); | |
| 245 | } | ||
| 246 | 6 |         if ($height <= 0) { | |
| 247 | 3 |             throw new BadMethodCallException('Invalid parameter height for command "resize"'); | |
| 248 | } | ||
| 249 | 3 | $this->image->resize(new Box($width, $height)); | |
| 250 | 3 | } | |
| 251 | |||
| 252 | /** | ||
| 253 | * Command image grayscale | ||
| 254 | * | ||
| 255 | * @return void | ||
| 256 | */ | ||
| 257 | 3 | protected function imageGrayscale() | |
| 261 | |||
| 262 | /** | ||
| 263 | * Command image negative | ||
| 264 | * | ||
| 265 | * @return void | ||
| 266 | */ | ||
| 267 | 3 | protected function imageNegative() | |
| 271 | |||
| 272 | /** | ||
| 273 | * Command image gamma | ||
| 274 | * | ||
| 275 | * @param float $correction | ||
| 276 | * | ||
| 277 | * @return void | ||
| 278 | */ | ||
| 279 | 3 | protected function imageGamma($correction) | |
| 280 |     { | ||
| 281 | 3 | $correction = (float) $correction; | |
| 282 | |||
| 283 | 3 | $this->image->effects()->gamma($correction); | |
| 284 | 3 | } | |
| 285 | |||
| 286 | /** | ||
| 287 | * Command image colorize | ||
| 288 | * | ||
| 289 | * @param string $hexColor | ||
| 290 | * | ||
| 291 | * @return void | ||
| 292 | */ | ||
| 293 | 6 | protected function imageColorize($hexColor) | |
| 294 |     { | ||
| 295 | 6 |         if (strlen($hexColor) != 6 || !preg_match('![0-9abcdef]!i', $hexColor)) { | |
| 296 | 3 |             throw new BadMethodCallException('Invalid parameter color for command "colorize"'); | |
| 297 | } | ||
| 298 | 3 |         $color = $this->image->palette()->color('#' . $hexColor); | |
| 299 | |||
| 300 | 3 | $this->image->effects()->colorize($color); | |
| 301 | 3 | } | |
| 302 | |||
| 303 | /** | ||
| 304 | * Command image sharpen | ||
| 305 | * | ||
| 306 | * @return void | ||
| 307 | */ | ||
| 308 | 3 | protected function imageSharpen() | |
| 312 | |||
| 313 | /** | ||
| 314 | * Command image blur | ||
| 315 | * | ||
| 316 | * @param float $sigma | ||
| 317 | * | ||
| 318 | * @return void | ||
| 319 | */ | ||
| 320 | 3 | protected function imageBlur($sigma = 1) | |
| 321 |     { | ||
| 322 | 3 | $sigma = (float) $sigma; | |
| 323 | |||
| 324 | 3 | $this->image->effects()->blur($sigma); | |
| 325 | 3 | } | |
| 326 | |||
| 327 | /** | ||
| 328 | * Command image version | ||
| 329 | * | ||
| 330 | * @return void | ||
| 331 | */ | ||
| 332 | protected function imageVersion() | ||
| 335 | |||
| 336 | /** | ||
| 337 | * Command image resize | ||
| 338 | * | ||
| 339 | * @param string $text | ||
| 340 | * @param string $backgroundColor | ||
| 341 | * @param string $color | ||
| 342 | * @param int $width | ||
| 343 | * @param int $height | ||
| 344 | * | ||
| 345 | * @return void | ||
| 346 | */ | ||
| 347 | 6 | protected function image404($text, $backgroundColor, $color, $width, $height) | |
| 376 | |||
| 377 | /** | ||
| 378 | * Draw centered text in current image | ||
| 379 | * | ||
| 380 | * @param string $text | ||
| 381 | * @param string $color | ||
| 382 | * | ||
| 383 | * @return void | ||
| 384 | */ | ||
| 385 | 6 | protected function drawCenteredText($text, $color) | |
| 407 | } | ||
| 408 | 
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: