Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ImageMinify 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 ImageMinify, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | class ImageMinify extends BaseTask |
||
| 69 | { |
||
| 70 | /** |
||
| 71 | * Destination directory for the minified images. |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $to; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Array of the source files. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $dirs = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Symfony 2 filesystem. |
||
| 86 | * |
||
| 87 | * @var sfFilesystem |
||
| 88 | */ |
||
| 89 | protected $fs; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Target directory for the downloaded binary executables. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $executableTargetDir; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Array for the downloaded binary executables. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $executablePaths = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Array for the individual results of all the files. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $results = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Default minifier to use. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | protected $minifier; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Array for minifier options. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $minifierOptions = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Supported minifiers. |
||
| 128 | * |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | protected $minifiers = [ |
||
| 132 | // Default 4 |
||
| 133 | 'optipng', |
||
| 134 | 'gifsicle', |
||
| 135 | 'jpegtran', |
||
| 136 | 'svgo', |
||
| 137 | // PNG |
||
| 138 | 'pngquant', |
||
| 139 | 'advpng', |
||
| 140 | 'pngout', |
||
| 141 | 'zopflipng', |
||
| 142 | 'pngcrush', |
||
| 143 | // JPG |
||
| 144 | 'jpegoptim', |
||
| 145 | 'jpeg-recompress', |
||
| 146 | ]; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Binary repositories of Imagemin. |
||
| 150 | * |
||
| 151 | * @link https://github.com/imagemin |
||
| 152 | * |
||
| 153 | * @var string[] |
||
| 154 | */ |
||
| 155 | protected $imageminRepos = [ |
||
| 156 | // PNG |
||
| 157 | 'optipng' => 'https://github.com/imagemin/optipng-bin', |
||
| 158 | 'pngquant' => 'https://github.com/imagemin/pngquant-bin', |
||
| 159 | 'advpng' => 'https://github.com/imagemin/advpng-bin', |
||
| 160 | 'pngout' => 'https://github.com/imagemin/pngout-bin', |
||
| 161 | 'zopflipng' => 'https://github.com/imagemin/zopflipng-bin', |
||
| 162 | 'pngcrush' => 'https://github.com/imagemin/pngcrush-bin', |
||
| 163 | // Gif |
||
| 164 | 'gifsicle' => 'https://github.com/imagemin/gifsicle-bin', |
||
| 165 | // JPG |
||
| 166 | 'jpegtran' => 'https://github.com/imagemin/jpegtran-bin', |
||
| 167 | 'jpegoptim' => 'https://github.com/imagemin/jpegoptim-bin', |
||
| 168 | 'cjpeg' => 'https://github.com/imagemin/mozjpeg-bin', // note: we do not support this minifier because it creates JPG from non-JPG files |
||
| 169 | 'jpeg-recompress' => 'https://github.com/imagemin/jpeg-recompress-bin', |
||
| 170 | // WebP |
||
| 171 | 'cwebp' => 'https://github.com/imagemin/cwebp-bin', // note: we do not support this minifier because it creates WebP from non-WebP files |
||
| 172 | ]; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string|string[] $dirs |
||
| 176 | */ |
||
| 177 | public function __construct($dirs) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function run() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Sets the target directory where the files will be copied to. |
||
| 234 | * |
||
| 235 | * @param string $target |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function to($target) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Sets the minifier. |
||
| 248 | * |
||
| 249 | * @param string $minifier |
||
| 250 | * @param array $options |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function minifier($minifier, array $options = []) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string[] $dirs |
||
| 264 | * |
||
| 265 | * @return array|\Robo\Result |
||
| 266 | * |
||
| 267 | * @throws \Robo\Exception\TaskException |
||
| 268 | */ |
||
| 269 | View Code Duplication | protected function findFiles($dirs) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $file |
||
| 319 | * @param string $to |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | protected function getTarget($file, $to) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string[] $files |
||
| 332 | * |
||
| 333 | * @return \Robo\Result |
||
| 334 | */ |
||
| 335 | protected function minify($files) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | View Code Duplication | protected function getOS() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $command |
||
| 453 | * |
||
| 454 | * @return \Robo\Result |
||
| 455 | */ |
||
| 456 | protected function executeCommand($command) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $executable |
||
| 486 | * |
||
| 487 | * @return \Robo\Result |
||
| 488 | */ |
||
| 489 | protected function installFromImagemin($executable) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param string $from |
||
| 561 | * @param string $to |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | protected function optipng($from, $to) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param string $from |
||
| 579 | * @param string $to |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | protected function jpegtran($from, $to) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param string $from |
||
| 592 | * @param string $to |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | protected function gifsicle($from, $to) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param string $from |
||
| 605 | * @param string $to |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | protected function svgo($from, $to) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param string $from |
||
| 618 | * @param string $to |
||
| 619 | * |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | protected function pngquant($from, $to) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param string $from |
||
| 631 | * @param string $to |
||
| 632 | * |
||
| 633 | * @return string |
||
| 634 | */ |
||
| 635 | protected function advpng($from, $to) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param string $from |
||
| 646 | * @param string $to |
||
| 647 | * |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | protected function pngout($from, $to) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @param string $from |
||
| 659 | * @param string $to |
||
| 660 | * |
||
| 661 | * @return string |
||
| 662 | */ |
||
| 663 | protected function zopflipng($from, $to) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @param string $from |
||
| 672 | * @param string $to |
||
| 673 | * |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | protected function pngcrush($from, $to) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param string $from |
||
| 685 | * @param string $to |
||
| 686 | * |
||
| 687 | * @return string |
||
| 688 | */ |
||
| 689 | protected function jpegoptim($from, $to) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param string $from |
||
| 699 | * @param string $to |
||
| 700 | * |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | protected function jpegRecompress($from, $to) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @param string $text |
||
| 712 | * |
||
| 713 | * @return string |
||
| 714 | */ |
||
| 715 | public static function camelCase($text) |
||
| 727 | } |
||
| 728 |
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: