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, LoggerAwareInterface |
||
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 | * @param string $binary |
||
43 | * @param array $options |
||
44 | * @param array $env |
||
|
|||
45 | */ |
||
46 | public function __construct($binary, array $options = [], array $env = null) |
||
57 | |||
58 | public function __destruct() |
||
62 | |||
63 | /** |
||
64 | * Set the logger to use to log debugging data. |
||
65 | * |
||
66 | * @param LoggerInterface $logger |
||
67 | */ |
||
68 | public function setLogger(LoggerInterface $logger) |
||
72 | |||
73 | /** |
||
74 | * This method must configure the media options. |
||
75 | * |
||
76 | * @see AbstractGenerator::addOption() |
||
77 | */ |
||
78 | abstract protected function configure(); |
||
79 | |||
80 | /** |
||
81 | * Sets the default extension. |
||
82 | * Useful when letting Snappy deal with file creation. |
||
83 | * |
||
84 | * @param string $defaultExtension |
||
85 | */ |
||
86 | public function setDefaultExtension($defaultExtension) |
||
90 | |||
91 | /** |
||
92 | * Gets the default extension. |
||
93 | * |
||
94 | * @return $string |
||
95 | */ |
||
96 | public function getDefaultExtension() |
||
100 | |||
101 | /** |
||
102 | * Sets an option. Be aware that option values are NOT validated and that |
||
103 | * it is your responsibility to validate user inputs. |
||
104 | * |
||
105 | * @param string $name The option to set |
||
106 | * @param mixed $value The value (NULL to unset) |
||
107 | * |
||
108 | * @throws \InvalidArgumentException |
||
109 | */ |
||
110 | public function setOption($name, $value) |
||
120 | |||
121 | /** |
||
122 | * Sets the timeout. |
||
123 | * |
||
124 | * @param int $timeout The timeout to set |
||
125 | */ |
||
126 | public function setTimeout($timeout) |
||
130 | |||
131 | /** |
||
132 | * Sets an array of options. |
||
133 | * |
||
134 | * @param array $options An associative array of options as name/value |
||
135 | */ |
||
136 | public function setOptions(array $options) |
||
142 | |||
143 | /** |
||
144 | * Returns all the options. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getOptions() |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function generate($input, $output, array $options = [], $overwrite = false) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function generateFromHtml($html, $output, array $options = [], $overwrite = false) |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function getOutput($input, array $options = []) |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function getOutputFromHtml($html, array $options = []) |
||
247 | |||
248 | /** |
||
249 | * Defines the binary. |
||
250 | * |
||
251 | * @param string $binary The path/name of the binary |
||
252 | */ |
||
253 | public function setBinary($binary) |
||
257 | |||
258 | /** |
||
259 | * Returns the binary. |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getBinary() |
||
267 | |||
268 | /** |
||
269 | * Returns the command for the given input and output files. |
||
270 | * |
||
271 | * @param array|string $input The input file |
||
272 | * @param string $output The ouput file |
||
273 | * @param array $options An optional array of options that will be used |
||
274 | * only for this command |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getCommand($input, $output, array $options = []) |
||
284 | |||
285 | /** |
||
286 | * Adds an option. |
||
287 | * |
||
288 | * @param string $name The name |
||
289 | * @param mixed $default An optional default value |
||
290 | * |
||
291 | * @throws \InvalidArgumentException |
||
292 | */ |
||
293 | protected function addOption($name, $default = null) |
||
301 | |||
302 | /** |
||
303 | * Adds an array of options. |
||
304 | * |
||
305 | * @param array $options |
||
306 | */ |
||
307 | protected function addOptions(array $options) |
||
313 | |||
314 | /** |
||
315 | * Merges the given array of options to the instance options and returns |
||
316 | * the result options array. It does NOT change the instance options. |
||
317 | * |
||
318 | * @param array $options |
||
319 | * |
||
320 | * @throws \InvalidArgumentException |
||
321 | * |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function mergeOptions(array $options) |
||
338 | |||
339 | /** |
||
340 | * Checks the specified output. |
||
341 | * |
||
342 | * @param string $output The output filename |
||
343 | * @param string $command The generation command |
||
344 | * |
||
345 | * @throws \RuntimeException if the output file generation failed |
||
346 | */ |
||
347 | protected function checkOutput($output, $command) |
||
367 | |||
368 | /** |
||
369 | * Checks the process return status. |
||
370 | * |
||
371 | * @param int $status The exit status code |
||
372 | * @param string $stdout The stdout content |
||
373 | * @param string $stderr The stderr content |
||
374 | * @param string $command The run command |
||
375 | * |
||
376 | * @throws \RuntimeException if the output file generation failed |
||
377 | */ |
||
378 | protected function checkProcessStatus($status, $stdout, $stderr, $command) |
||
393 | |||
394 | /** |
||
395 | * Creates a temporary file. |
||
396 | * The file is not created if the $content argument is null. |
||
397 | * |
||
398 | * @param string $content Optional content for the temporary file |
||
399 | * @param string $extension An optional extension for the filename |
||
400 | * |
||
401 | * @return string The filename |
||
402 | */ |
||
403 | protected function createTemporaryFile($content = null, $extension = null) |
||
429 | |||
430 | /** |
||
431 | * Removes all temporary files. |
||
432 | */ |
||
433 | public function removeTemporaryFiles() |
||
439 | |||
440 | /** |
||
441 | * Builds the command string. |
||
442 | * |
||
443 | * @param string $binary The binary path/name |
||
444 | * @param string/array $input Url(s) or file location(s) of the page(s) to process |
||
445 | * @param string $output File location to the image-to-be |
||
446 | * @param array $options An array of options |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | protected function buildCommand($binary, $input, $output, array $options = []) |
||
501 | |||
502 | /** |
||
503 | * Return true if the array is an associative array |
||
504 | * and not an indexed array. |
||
505 | * |
||
506 | * @param array $array |
||
507 | * |
||
508 | * @return bool |
||
509 | */ |
||
510 | protected function isAssociativeArray(array $array) |
||
514 | |||
515 | /** |
||
516 | * Executes the given command via shell and returns the complete output as |
||
517 | * a string. |
||
518 | * |
||
519 | * @param string $command |
||
520 | * |
||
521 | * @return array(status, stdout, stderr) |
||
522 | */ |
||
523 | protected function executeCommand($command) |
||
543 | |||
544 | /** |
||
545 | * Prepares the specified output. |
||
546 | * |
||
547 | * @param string $filename The output filename |
||
548 | * @param bool $overwrite Whether to overwrite the file if it already |
||
549 | * exist |
||
550 | * |
||
551 | * @throws Exception\FileAlreadyExistsException |
||
552 | * @throws \RuntimeException |
||
553 | * @throws \InvalidArgumentException |
||
554 | */ |
||
555 | protected function prepareOutput($filename, $overwrite) |
||
584 | |||
585 | /** |
||
586 | * Get TemporaryFolder. |
||
587 | * |
||
588 | * @return string |
||
589 | */ |
||
590 | public function getTemporaryFolder() |
||
598 | |||
599 | /** |
||
600 | * Set temporaryFolder. |
||
601 | * |
||
602 | * @param string $temporaryFolder |
||
603 | * |
||
604 | * @return $this |
||
605 | */ |
||
606 | public function setTemporaryFolder($temporaryFolder) |
||
612 | |||
613 | /** |
||
614 | * Wrapper for the "file_get_contents" function. |
||
615 | * |
||
616 | * @param string $filename |
||
617 | * |
||
618 | * @return string |
||
619 | */ |
||
620 | protected function getFileContents($filename) |
||
624 | |||
625 | /** |
||
626 | * Wrapper for the "file_exists" function. |
||
627 | * |
||
628 | * @param string $filename |
||
629 | * |
||
630 | * @return bool |
||
631 | */ |
||
632 | protected function fileExists($filename) |
||
636 | |||
637 | /** |
||
638 | * Wrapper for the "is_file" method. |
||
639 | * |
||
640 | * @param string $filename |
||
641 | * |
||
642 | * @return bool |
||
643 | */ |
||
644 | protected function isFile($filename) |
||
648 | |||
649 | /** |
||
650 | * Wrapper for the "filesize" function. |
||
651 | * |
||
652 | * @param string $filename |
||
653 | * |
||
654 | * @return int or FALSE on failure |
||
655 | */ |
||
656 | protected function filesize($filename) |
||
660 | |||
661 | /** |
||
662 | * Wrapper for the "unlink" function. |
||
663 | * |
||
664 | * @param string $filename |
||
665 | * |
||
666 | * @return bool |
||
667 | */ |
||
668 | protected function unlink($filename) |
||
672 | |||
673 | /** |
||
674 | * Wrapper for the "is_dir" function. |
||
675 | * |
||
676 | * @param string $filename |
||
677 | * |
||
678 | * @return bool |
||
679 | */ |
||
680 | protected function isDir($filename) |
||
684 | |||
685 | /** |
||
686 | * Wrapper for the mkdir function. |
||
687 | * |
||
688 | * @param string $pathname |
||
689 | * |
||
690 | * @return bool |
||
691 | */ |
||
692 | protected function mkdir($pathname) |
||
696 | |||
697 | /** |
||
698 | * Reset all options to their initial values. |
||
699 | * |
||
700 | * @return void |
||
701 | */ |
||
702 | public function resetOptions() |
||
707 | } |
||
708 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.