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 |
||
18 | abstract class AbstractGenerator implements GeneratorInterface |
||
19 | { |
||
20 | private $binary; |
||
21 | private $options = []; |
||
22 | private $env; |
||
23 | private $timeout = false; |
||
24 | private $defaultExtension; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $temporaryFolder; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | public $temporaryFiles = []; |
||
35 | |||
36 | /** |
||
37 | * @var LoggerInterface |
||
38 | */ |
||
39 | private $logger; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param string $binary |
||
45 | * @param array $options |
||
46 | * @param array $env |
||
47 | */ |
||
48 | public function __construct($binary, array $options = [], array $env = null) |
||
59 | |||
60 | public function __destruct() |
||
64 | |||
65 | /** |
||
66 | * Set the logger to use to log debugging data. |
||
67 | * |
||
68 | * @param LoggerInterface $logger |
||
69 | */ |
||
70 | public function setLogger(LoggerInterface $logger) |
||
74 | |||
75 | /** |
||
76 | * This method must configure the media options |
||
77 | * |
||
78 | * @see AbstractGenerator::addOption() |
||
79 | */ |
||
80 | abstract protected function configure(); |
||
81 | |||
82 | /** |
||
83 | * Sets the default extension. |
||
84 | * Useful when letting Snappy deal with file creation |
||
85 | * |
||
86 | * @param string $defaultExtension |
||
87 | */ |
||
88 | public function setDefaultExtension($defaultExtension) |
||
92 | |||
93 | /** |
||
94 | * Gets the default extension |
||
95 | * |
||
96 | * @return $string |
||
|
|||
97 | */ |
||
98 | public function getDefaultExtension() |
||
102 | |||
103 | /** |
||
104 | * Sets an option. Be aware that option values are NOT validated and that |
||
105 | * it is your responsibility to validate user inputs |
||
106 | * |
||
107 | * @param string $name The option to set |
||
108 | * @param mixed $value The value (NULL to unset) |
||
109 | * |
||
110 | * @throws \InvalidArgumentException |
||
111 | */ |
||
112 | public function setOption($name, $value) |
||
122 | |||
123 | /** |
||
124 | * Sets the timeout. |
||
125 | * |
||
126 | * @param integer $timeout The timeout to set |
||
127 | */ |
||
128 | public function setTimeout($timeout) |
||
132 | |||
133 | /** |
||
134 | * Sets an array of options |
||
135 | * |
||
136 | * @param array $options An associative array of options as name/value |
||
137 | */ |
||
138 | public function setOptions(array $options) |
||
144 | |||
145 | /** |
||
146 | * Returns all the options |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function getOptions() |
||
154 | |||
155 | /** |
||
156 | * {@inheritDoc} |
||
157 | */ |
||
158 | public function generate($input, $output, array $options = [], $overwrite = false) |
||
199 | |||
200 | /** |
||
201 | * {@inheritDoc} |
||
202 | */ |
||
203 | View Code Duplication | public function generateFromHtml($html, $output, array $options = [], $overwrite = false) |
|
216 | |||
217 | /** |
||
218 | * {@inheritDoc} |
||
219 | */ |
||
220 | public function getOutput($input, array $options = []) |
||
230 | |||
231 | /** |
||
232 | * {@inheritDoc} |
||
233 | */ |
||
234 | View Code Duplication | public function getOutputFromHtml($html, array $options = []) |
|
249 | |||
250 | /** |
||
251 | * Defines the binary |
||
252 | * |
||
253 | * @param string $binary The path/name of the binary |
||
254 | */ |
||
255 | public function setBinary($binary) |
||
259 | |||
260 | /** |
||
261 | * Returns the binary |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getBinary() |
||
269 | |||
270 | /** |
||
271 | * Returns the command for the given input and output files |
||
272 | * |
||
273 | * @param array|string $input The input file |
||
274 | * @param string $output The ouput file |
||
275 | * @param array $options An optional array of options that will be used |
||
276 | * only for this command |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getCommand($input, $output, array $options = []) |
||
286 | |||
287 | /** |
||
288 | * Adds an option |
||
289 | * |
||
290 | * @param string $name The name |
||
291 | * @param mixed $default An optional default value |
||
292 | * |
||
293 | * @throws \InvalidArgumentException |
||
294 | */ |
||
295 | protected function addOption($name, $default = null) |
||
303 | |||
304 | /** |
||
305 | * Adds an array of options |
||
306 | * |
||
307 | * @param array $options |
||
308 | */ |
||
309 | protected function addOptions(array $options) |
||
315 | |||
316 | /** |
||
317 | * Merges the given array of options to the instance options and returns |
||
318 | * the result options array. It does NOT change the instance options. |
||
319 | * |
||
320 | * @param array $options |
||
321 | * @throws \InvalidArgumentException |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | protected function mergeOptions(array $options) |
||
339 | |||
340 | /** |
||
341 | * Checks the specified output |
||
342 | * |
||
343 | * @param string $output The output filename |
||
344 | * @param string $command The generation command |
||
345 | * |
||
346 | * @throws \RuntimeException if the output file generation failed |
||
347 | */ |
||
348 | protected function checkOutput($output, $command) |
||
366 | |||
367 | /** |
||
368 | * Checks the process return status |
||
369 | * |
||
370 | * @param int $status The exit status code |
||
371 | * @param string $stdout The stdout content |
||
372 | * @param string $stderr The stderr content |
||
373 | * @param string $command The run command |
||
374 | * |
||
375 | * @throws \RuntimeException if the output file generation failed |
||
376 | */ |
||
377 | protected function checkProcessStatus($status, $stdout, $stderr, $command) |
||
389 | |||
390 | /** |
||
391 | * Creates a temporary file. |
||
392 | * The file is not created if the $content argument is null |
||
393 | * |
||
394 | * @param string $content Optional content for the temporary file |
||
395 | * @param string $extension An optional extension for the filename |
||
396 | * |
||
397 | * @return string The filename |
||
398 | */ |
||
399 | protected function createTemporaryFile($content = null, $extension = null) |
||
425 | |||
426 | /** |
||
427 | * Removes all temporary files |
||
428 | */ |
||
429 | public function removeTemporaryFiles() |
||
435 | |||
436 | /** |
||
437 | * Builds the command string |
||
438 | * |
||
439 | * @param string $binary The binary path/name |
||
440 | * @param string/array $input Url(s) or file location(s) of the page(s) to process |
||
441 | * @param string $output File location to the image-to-be |
||
442 | * @param array $options An array of options |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | protected function buildCommand($binary, $input, $output, array $options = []) |
||
500 | |||
501 | /** |
||
502 | * Return true if the array is an associative array |
||
503 | * and not an indexed array |
||
504 | * |
||
505 | * @param array $array |
||
506 | * |
||
507 | * @return boolean |
||
508 | */ |
||
509 | protected function isAssociativeArray(array $array) |
||
513 | |||
514 | /** |
||
515 | * Executes the given command via shell and returns the complete output as |
||
516 | * a string |
||
517 | * |
||
518 | * @param string $command |
||
519 | * |
||
520 | * @return array(status, stdout, stderr) |
||
521 | */ |
||
522 | protected function executeCommand($command) |
||
538 | |||
539 | /** |
||
540 | * Prepares the specified output |
||
541 | * |
||
542 | * @param string $filename The output filename |
||
543 | * @param boolean $overwrite Whether to overwrite the file if it already |
||
544 | * exist |
||
545 | * |
||
546 | * @throws Exception\FileAlreadyExistsException |
||
547 | * @throws \RuntimeException |
||
548 | * @throws \InvalidArgumentException |
||
549 | */ |
||
550 | protected function prepareOutput($filename, $overwrite) |
||
578 | |||
579 | /** |
||
580 | * Get TemporaryFolder |
||
581 | * |
||
582 | * @return string |
||
583 | */ |
||
584 | public function getTemporaryFolder() |
||
592 | |||
593 | /** |
||
594 | * Set temporaryFolder |
||
595 | * |
||
596 | * @param string $temporaryFolder |
||
597 | * |
||
598 | * @return $this |
||
599 | */ |
||
600 | public function setTemporaryFolder($temporaryFolder) |
||
606 | |||
607 | /** |
||
608 | * Wrapper for the "file_get_contents" function |
||
609 | * |
||
610 | * @param string $filename |
||
611 | * |
||
612 | * @return string |
||
613 | */ |
||
614 | protected function getFileContents($filename) |
||
618 | |||
619 | /** |
||
620 | * Wrapper for the "file_exists" function |
||
621 | * |
||
622 | * @param string $filename |
||
623 | * |
||
624 | * @return boolean |
||
625 | */ |
||
626 | protected function fileExists($filename) |
||
630 | |||
631 | /** |
||
632 | * Wrapper for the "is_file" method |
||
633 | * |
||
634 | * @param string $filename |
||
635 | * |
||
636 | * @return boolean |
||
637 | */ |
||
638 | protected function isFile($filename) |
||
642 | |||
643 | /** |
||
644 | * Wrapper for the "filesize" function |
||
645 | * |
||
646 | * @param string $filename |
||
647 | * |
||
648 | * @return integer or FALSE on failure |
||
649 | */ |
||
650 | protected function filesize($filename) |
||
654 | |||
655 | /** |
||
656 | * Wrapper for the "unlink" function |
||
657 | * |
||
658 | * @param string $filename |
||
659 | * |
||
660 | * @return boolean |
||
661 | */ |
||
662 | protected function unlink($filename) |
||
666 | |||
667 | /** |
||
668 | * Wrapper for the "is_dir" function |
||
669 | * |
||
670 | * @param string $filename |
||
671 | * |
||
672 | * @return boolean |
||
673 | */ |
||
674 | protected function isDir($filename) |
||
678 | |||
679 | /** |
||
680 | * Wrapper for the mkdir function |
||
681 | * |
||
682 | * @param string $pathname |
||
683 | * |
||
684 | * @return boolean |
||
685 | */ |
||
686 | protected function mkdir($pathname) |
||
690 | } |
||
691 |
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.