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 AbstractGenerator 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 AbstractGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class AbstractGenerator implements GeneratorInterface |
||
| 17 | { |
||
| 18 | private $binary; |
||
| 19 | private $options = []; |
||
| 20 | private $env; |
||
| 21 | private $timeout = false; |
||
| 22 | private $defaultExtension; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $temporaryFolder; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var boolean |
||
| 31 | */ |
||
| 32 | protected $ignoreContentNotFound = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | public $temporaryFiles = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor |
||
| 41 | * |
||
| 42 | * @param string $binary |
||
| 43 | * @param array $options |
||
| 44 | * @param array $env |
||
| 45 | */ |
||
| 46 | public function __construct($binary, array $options = [], array $env = null) |
||
| 56 | |||
| 57 | public function __destruct() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * This method must configure the media options |
||
| 64 | * |
||
| 65 | * @see AbstractGenerator::addOption() |
||
| 66 | */ |
||
| 67 | abstract protected function configure(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Sets the default extension. |
||
| 71 | * Useful when letting Snappy deal with file creation |
||
| 72 | * |
||
| 73 | * @param string $defaultExtension |
||
| 74 | */ |
||
| 75 | public function setDefaultExtension($defaultExtension) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Gets the default extension |
||
| 82 | * |
||
| 83 | * @return $string |
||
|
|
|||
| 84 | */ |
||
| 85 | public function getDefaultExtension() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Sets an option. Be aware that option values are NOT validated and that |
||
| 92 | * it is your responsibility to validate user inputs |
||
| 93 | * |
||
| 94 | * @param string $name The option to set |
||
| 95 | * @param mixed $value The value (NULL to unset) |
||
| 96 | * |
||
| 97 | * @throws \InvalidArgumentException |
||
| 98 | */ |
||
| 99 | public function setOption($name, $value) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Sets the timeout. Be aware that option only works with symfony |
||
| 110 | * |
||
| 111 | * @param integer $timeout The timeout to set |
||
| 112 | */ |
||
| 113 | public function setTimeout($timeout) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets an array of options |
||
| 120 | * |
||
| 121 | * @param array $options An associative array of options as name/value |
||
| 122 | */ |
||
| 123 | public function setOptions(array $options) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns all the options |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | public function getOptions() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritDoc} |
||
| 142 | */ |
||
| 143 | public function generate($input, $output, array $options = [], $overwrite = false) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritDoc} |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function generateFromHtml($html, $output, array $options = [], $overwrite = false) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritDoc} |
||
| 180 | */ |
||
| 181 | public function getOutput($input, array $options = []) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritDoc} |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function getOutputFromHtml($html, array $options = []) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Defines the binary |
||
| 213 | * |
||
| 214 | * @param string $binary The path/name of the binary |
||
| 215 | */ |
||
| 216 | public function setBinary($binary) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the binary |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getBinary() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the command for the given input and output files |
||
| 233 | * |
||
| 234 | * @param array|string $input The input file |
||
| 235 | * @param string $output The ouput file |
||
| 236 | * @param array $options An optional array of options that will be used |
||
| 237 | * only for this command |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getCommand($input, $output, array $options = []) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Adds an option |
||
| 250 | * |
||
| 251 | * @param string $name The name |
||
| 252 | * @param mixed $default An optional default value |
||
| 253 | * |
||
| 254 | * @throws \InvalidArgumentException |
||
| 255 | */ |
||
| 256 | protected function addOption($name, $default = null) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Adds an array of options |
||
| 267 | * |
||
| 268 | * @param array $options |
||
| 269 | */ |
||
| 270 | protected function addOptions(array $options) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Merges the given array of options to the instance options and returns |
||
| 279 | * the result options array. It does NOT change the instance options. |
||
| 280 | * |
||
| 281 | * @param array $options |
||
| 282 | * @throws \InvalidArgumentException |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | protected function mergeOptions(array $options) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Checks the specified output |
||
| 303 | * |
||
| 304 | * @param string $output The output filename |
||
| 305 | * @param string $command The generation command |
||
| 306 | * |
||
| 307 | * @throws \RuntimeException if the output file generation failed |
||
| 308 | */ |
||
| 309 | protected function checkOutput($output, $command) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Checks the process return status |
||
| 330 | * |
||
| 331 | * @param int $status The exit status code |
||
| 332 | * @param string $stdout The stdout content |
||
| 333 | * @param string $stderr The stderr content |
||
| 334 | * @param string $command The run command |
||
| 335 | * |
||
| 336 | * @throws \RuntimeException if the output file generation failed |
||
| 337 | */ |
||
| 338 | protected function checkProcessStatus($status, $stdout, $stderr, $command) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Creates a temporary file. |
||
| 357 | * The file is not created if the $content argument is null |
||
| 358 | * |
||
| 359 | * @param string $content Optional content for the temporary file |
||
| 360 | * @param string $extension An optional extension for the filename |
||
| 361 | * |
||
| 362 | * @return string The filename |
||
| 363 | */ |
||
| 364 | protected function createTemporaryFile($content = null, $extension = null) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Removes all temporary files |
||
| 393 | */ |
||
| 394 | public function removeTemporaryFiles() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Builds the command string |
||
| 403 | * |
||
| 404 | * @param string $binary The binary path/name |
||
| 405 | * @param string/array $input Url(s) or file location(s) of the page(s) to process |
||
| 406 | * @param string $output File location to the image-to-be |
||
| 407 | * @param array $options An array of options |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function buildCommand($binary, $input, $output, array $options = []) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Return true if the array is an associative array |
||
| 468 | * and not an indexed array |
||
| 469 | * |
||
| 470 | * @param array $array |
||
| 471 | * |
||
| 472 | * @return boolean |
||
| 473 | */ |
||
| 474 | protected function isAssociativeArray(array $array) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Executes the given command via shell and returns the complete output as |
||
| 481 | * a string |
||
| 482 | * |
||
| 483 | * @param string $command |
||
| 484 | * |
||
| 485 | * @return array(status, stdout, stderr) |
||
| 486 | */ |
||
| 487 | protected function executeCommand($command) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Prepares the specified output |
||
| 506 | * |
||
| 507 | * @param string $filename The output filename |
||
| 508 | * @param boolean $overwrite Whether to overwrite the file if it already |
||
| 509 | * exist |
||
| 510 | * |
||
| 511 | * @throws Exception\FileAlreadyExistsException |
||
| 512 | * @throws \RuntimeException |
||
| 513 | * @throws \InvalidArgumentException |
||
| 514 | */ |
||
| 515 | protected function prepareOutput($filename, $overwrite) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get TemporaryFolder |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function getTemporaryFolder() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Set temporaryFolder |
||
| 560 | * |
||
| 561 | * @param string $temporaryFolder |
||
| 562 | * |
||
| 563 | * @return $this |
||
| 564 | */ |
||
| 565 | public function setTemporaryFolder($temporaryFolder) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | public function isIgnoreContentNotFound() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @param bool $ignoreContentNotFound |
||
| 582 | * |
||
| 583 | * @return AbstractGenerator |
||
| 584 | */ |
||
| 585 | public function setIgnoreContentNotFound($ignoreContentNotFound) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Wrapper for the "file_get_contents" function |
||
| 594 | * |
||
| 595 | * @param string $filename |
||
| 596 | * |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | protected function getFileContents($filename) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Wrapper for the "file_exists" function |
||
| 606 | * |
||
| 607 | * @param string $filename |
||
| 608 | * |
||
| 609 | * @return boolean |
||
| 610 | */ |
||
| 611 | protected function fileExists($filename) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Wrapper for the "is_file" method |
||
| 618 | * |
||
| 619 | * @param string $filename |
||
| 620 | * |
||
| 621 | * @return boolean |
||
| 622 | */ |
||
| 623 | protected function isFile($filename) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Wrapper for the "filesize" function |
||
| 630 | * |
||
| 631 | * @param string $filename |
||
| 632 | * |
||
| 633 | * @return integer or FALSE on failure |
||
| 634 | */ |
||
| 635 | protected function filesize($filename) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Wrapper for the "unlink" function |
||
| 642 | * |
||
| 643 | * @param string $filename |
||
| 644 | * |
||
| 645 | * @return boolean |
||
| 646 | */ |
||
| 647 | protected function unlink($filename) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Wrapper for the "is_dir" function |
||
| 654 | * |
||
| 655 | * @param string $filename |
||
| 656 | * |
||
| 657 | * @return boolean |
||
| 658 | */ |
||
| 659 | protected function isDir($filename) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Wrapper for the mkdir function |
||
| 666 | * |
||
| 667 | * @param string $pathname |
||
| 668 | * |
||
| 669 | * @return boolean |
||
| 670 | */ |
||
| 671 | protected function mkdir($pathname) |
||
| 675 | } |
||
| 676 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.