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 |
||
| 21 | abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $temporaryFiles = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $temporaryFolder; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var null|string |
||
| 35 | */ |
||
| 36 | private $binary; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $options = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var null|array |
||
| 45 | */ |
||
| 46 | private $env; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var null|int |
||
| 50 | */ |
||
| 51 | private $timeout; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $defaultExtension; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var LoggerInterface |
||
| 60 | */ |
||
| 61 | private $logger; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param null|string $binary |
||
| 65 | * @param array $options |
||
| 66 | * @param null|array $env |
||
| 67 | */ |
||
| 68 | public function __construct(string $binary = null, array $options = [], array $env = null) |
||
| 81 | |||
| 82 | public function __destruct() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Set the logger to use to log debugging data. |
||
| 89 | * |
||
| 90 | * @param LoggerInterface $logger |
||
| 91 | */ |
||
| 92 | public function setLogger(LoggerInterface $logger): self |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Sets the default extension. |
||
| 101 | * Useful when letting Snappy deal with file creation. |
||
| 102 | * |
||
| 103 | * @param string $defaultExtension |
||
| 104 | */ |
||
| 105 | public function setDefaultExtension(string $defaultExtension): self |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Gets the default extension. |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getDefaultExtension(): string |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Sets an option. Be aware that option values are NOT validated and that |
||
| 124 | * it is your responsibility to validate user inputs. |
||
| 125 | * |
||
| 126 | * @param string $name The option to set |
||
| 127 | * @param mixed $value The value (NULL to unset) |
||
| 128 | * |
||
| 129 | * @throws InvalidArgumentException |
||
| 130 | */ |
||
| 131 | public function setOption(string $name, $value): self |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Sets the timeout. |
||
| 146 | * |
||
| 147 | * @param null|int $timeout The timeout to set |
||
| 148 | */ |
||
| 149 | public function setTimeout(?int $timeout): self |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets an array of options. |
||
| 158 | * |
||
| 159 | * @param array $options An associative array of options as name/value |
||
| 160 | */ |
||
| 161 | public function setOptions(array $options): self |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns all the options. |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getOptions(): array |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function generate($input, string $output, array $options = [], bool $overwrite = false): void |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | public function generateFromHtml($html, string $output, array $options = [], bool $overwrite = false): void |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function getOutput($input, array $options = []): string |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function getOutputFromHtml($html, array $options = []): string |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Defines the binary. |
||
| 267 | * |
||
| 268 | * @param null|string $binary The path/name of the binary |
||
| 269 | */ |
||
| 270 | public function setBinary(?string $binary): self |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the binary. |
||
| 279 | * |
||
| 280 | * @return null|string |
||
| 281 | */ |
||
| 282 | public function getBinary(): ?string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the command for the given input and output files. |
||
| 289 | * |
||
| 290 | * @param array|string $input The input file |
||
| 291 | * @param string $output The ouput file |
||
| 292 | * @param array $options An optional array of options that will be used |
||
| 293 | * only for this command |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getCommand($input, string $output, array $options = []): string |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Removes all temporary files. |
||
| 310 | */ |
||
| 311 | public function removeTemporaryFiles(): void |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get TemporaryFolder. |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getTemporaryFolder(): string |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set temporaryFolder. |
||
| 334 | * |
||
| 335 | * @param string $temporaryFolder |
||
| 336 | * |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function setTemporaryFolder(string $temporaryFolder): self |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Reset all options to their initial values. |
||
| 348 | * |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | public function resetOptions(): void |
||
| 356 | |||
| 357 | /** |
||
| 358 | * This method must configure the media options. |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | * |
||
| 362 | * @see AbstractGenerator::addOption() |
||
| 363 | */ |
||
| 364 | abstract protected function configure(): void; |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Adds an option. |
||
| 368 | * |
||
| 369 | * @param string $name The name |
||
| 370 | * @param mixed $default An optional default value |
||
| 371 | * |
||
| 372 | * @throws InvalidArgumentException |
||
| 373 | */ |
||
| 374 | protected function addOption(string $name, $default = null): self |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Adds an array of options. |
||
| 387 | * |
||
| 388 | * @param array $options |
||
| 389 | */ |
||
| 390 | protected function addOptions(array $options): self |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Merges the given array of options to the instance options and returns |
||
| 401 | * the result options array. It does NOT change the instance options. |
||
| 402 | * |
||
| 403 | * @param array $options |
||
| 404 | * |
||
| 405 | * @throws InvalidArgumentException |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | protected function mergeOptions(array $options): array |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Checks the specified output. |
||
| 426 | * |
||
| 427 | * @param string $output The output filename |
||
| 428 | * @param string $command The generation command |
||
| 429 | * |
||
| 430 | * @throws RuntimeException if the output file generation failed |
||
| 431 | */ |
||
| 432 | protected function checkOutput(string $output, string $command): void |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Checks the process return status. |
||
| 447 | * |
||
| 448 | * @param int $status The exit status code |
||
| 449 | * @param string $stdout The stdout content |
||
| 450 | * @param string $stderr The stderr content |
||
| 451 | * @param string $command The run command |
||
| 452 | * |
||
| 453 | * @throws RuntimeException if the output file generation failed |
||
| 454 | */ |
||
| 455 | protected function checkProcessStatus(int $status, string $stdout, string $stderr, string $command): void |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Creates a temporary file. |
||
| 464 | * The file is not created if the $content argument is null. |
||
| 465 | * |
||
| 466 | * @param null|string $content Optional content for the temporary file |
||
| 467 | * @param null|string $extension An optional extension for the filename |
||
| 468 | * |
||
| 469 | * @return string The filename |
||
| 470 | */ |
||
| 471 | protected function createTemporaryFile(?string $content = null, ?string $extension = null): string |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Builds the command string. |
||
| 500 | * |
||
| 501 | * @param string $binary The binary path/name |
||
| 502 | * @param array|string $input Url(s) or file location(s) of the page(s) to process |
||
| 503 | * @param string $output File location to the image-to-be |
||
| 504 | * @param array $options An array of options |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | protected function buildCommand(string $binary, $input, string $output, array $options = []): string |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Return true if the array is an associative array |
||
| 562 | * and not an indexed array. |
||
| 563 | * |
||
| 564 | * @param array $array |
||
| 565 | * |
||
| 566 | * @return bool |
||
| 567 | */ |
||
| 568 | protected function isAssociativeArray(array $array): bool |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Executes the given command via shell and returns the complete output as |
||
| 575 | * a string. |
||
| 576 | * |
||
| 577 | * @param string $command |
||
| 578 | * |
||
| 579 | * @return array [status, stdout, stderr] |
||
| 580 | */ |
||
| 581 | protected function executeCommand(string $command): array |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Prepares the specified output. |
||
| 604 | * |
||
| 605 | * @param string $filename The output filename |
||
| 606 | * @param bool $overwrite Whether to overwrite the file if it already |
||
| 607 | * exist |
||
| 608 | * |
||
| 609 | * @throws FileAlreadyExistsException |
||
| 610 | * @throws RuntimeException |
||
| 611 | * @throws InvalidArgumentException |
||
| 612 | */ |
||
| 613 | protected function prepareOutput(string $filename, bool $overwrite): void |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Wrapper for the "file_get_contents" function. |
||
| 634 | * |
||
| 635 | * @param string $filename |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | protected function getFileContents(string $filename): string |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Wrapper for the "file_exists" function. |
||
| 652 | * |
||
| 653 | * @param string $filename |
||
| 654 | * |
||
| 655 | * @return bool |
||
| 656 | */ |
||
| 657 | protected function fileExists(string $filename): bool |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Wrapper for the "is_file" method. |
||
| 664 | * |
||
| 665 | * @param string $filename |
||
| 666 | * |
||
| 667 | * @return bool |
||
| 668 | */ |
||
| 669 | protected function isFile(string $filename): bool |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Wrapper for the "filesize" function. |
||
| 676 | * |
||
| 677 | * @param string $filename |
||
| 678 | * |
||
| 679 | * @return int |
||
| 680 | */ |
||
| 681 | protected function filesize(string $filename): int |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Wrapper for the "unlink" function. |
||
| 694 | * |
||
| 695 | * @param string $filename |
||
| 696 | * |
||
| 697 | * @return bool |
||
| 698 | */ |
||
| 699 | protected function unlink(string $filename): bool |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Wrapper for the "is_dir" function. |
||
| 706 | * |
||
| 707 | * @param string $filename |
||
| 708 | * |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | protected function isDir(string $filename): bool |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Wrapper for the mkdir function. |
||
| 718 | * |
||
| 719 | * @param string $pathname |
||
| 720 | * |
||
| 721 | * @return bool |
||
| 722 | */ |
||
| 723 | protected function mkdir(string $pathname): bool |
||
| 727 | } |
||
| 728 |