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 Installer 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 Installer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Installer |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * The help text of the installer. |
||
| 44 | */ |
||
| 45 | const HELP_TEXT = <<<HELP |
||
| 46 | Puli Installer |
||
| 47 | -------------- |
||
| 48 | Options |
||
| 49 | --help this help |
||
| 50 | --check for checking environment only |
||
| 51 | --force forces the installation even if the system requirements are not satisfied |
||
| 52 | --ansi force ANSI color output |
||
| 53 | --no-ansi disable ANSI color output |
||
| 54 | --quiet do not output unimportant messages |
||
| 55 | --install-dir="..." set the target installation directory |
||
| 56 | --version="..." install a specific version |
||
| 57 | --filename="..." set the target filename (default: puli.phar) |
||
| 58 | --disable-tls disable SSL/TLS security for file downloads |
||
| 59 | |||
| 60 | HELP; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The api url to determine the available versions of puli. |
||
| 64 | */ |
||
| 65 | const VERSION_API_URL = '%s://puli.io/download/versions.json'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The phar download url. |
||
| 69 | */ |
||
| 70 | const PHAR_DOWNLOAD_URL = '%s://puli.io/download/%s/puli.phar'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $stability; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | private $check; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | private $help; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | private $force; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | private $quiet; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var bool |
||
| 99 | */ |
||
| 100 | private $disableTls; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $installDir; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | private $version; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | private $filename; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Runs the installer. |
||
| 119 | * |
||
| 120 | * @param array $argv The console arguments. |
||
| 121 | * |
||
| 122 | * @return int The return status. |
||
| 123 | */ |
||
| 124 | 5 | public function run(array $argv) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Installs puli to the current working directory. |
||
| 158 | * |
||
| 159 | * @return int The return status. |
||
| 160 | * |
||
| 161 | * @throws Exception |
||
| 162 | */ |
||
| 163 | 5 | private function installPuli() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Downloads a URL to a path. |
||
| 289 | * |
||
| 290 | * @param HttpClient $httpClient The client to use. |
||
| 291 | * @param string $url The URL to download. |
||
| 292 | * @param string $targetPath The path to download the URL to. |
||
| 293 | * |
||
| 294 | * @return bool Whether the download completed successfully. |
||
| 295 | */ |
||
| 296 | 4 | private function downloadFile(HttpClient $httpClient, $url, $targetPath) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Downloads the available puli versions. |
||
| 326 | * |
||
| 327 | * @param HttpClient $httpClient The client to use. |
||
| 328 | * @param string $url The URL to download. |
||
| 329 | * |
||
| 330 | * @return array The available versions, null if the download failed. |
||
| 331 | * |
||
| 332 | * @throws RuntimeException If an error occurs. |
||
| 333 | */ |
||
| 334 | 5 | public function downloadVersions(HttpClient $httpClient, $url) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Validates that the given path is a valid PHAR file. |
||
| 363 | * |
||
| 364 | * @param string $path A path to a PHAR file. |
||
| 365 | * |
||
| 366 | * @throws Exception If an error occurs. |
||
| 367 | */ |
||
| 368 | 4 | private function validatePhar($path) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Check the platform for possible issues on running Puli. |
||
| 403 | * |
||
| 404 | * @return bool Whether the platform requirements are satisfied. |
||
| 405 | */ |
||
| 406 | 5 | private function validateSystem() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Validate whether the passed command line options are correct. |
||
| 600 | * |
||
| 601 | * @return bool Returns `true` if the options are valid and `false` otherwise. |
||
| 602 | */ |
||
| 603 | 5 | private function validateOptions() |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Parses the command line options. |
||
| 628 | * |
||
| 629 | * @param string[] $argv The command line options. |
||
| 630 | */ |
||
| 631 | 5 | private function parseOptions(array $argv) |
|
| 632 | { |
||
| 633 | 5 | $this->check = in_array('--check', $argv); |
|
| 634 | 5 | $this->help = in_array('--help', $argv); |
|
| 635 | 5 | $this->force = in_array('--force', $argv); |
|
| 636 | 5 | $this->quiet = in_array('--quiet', $argv); |
|
| 637 | 5 | $this->disableTls = in_array('--disable-tls', $argv); |
|
| 638 | 5 | $this->installDir = false; |
|
| 639 | 5 | $this->version = false; |
|
| 640 | 5 | $this->filename = 'puli.phar'; |
|
| 641 | 5 | $this->stability = 'unstable'; |
|
| 642 | 5 | if (in_array('--stable', $argv)) { |
|
| 643 | 1 | $this->stability = 'stable'; |
|
| 644 | 1 | } |
|
| 645 | |||
| 646 | // --no-ansi wins over --ansi |
||
| 647 | 5 | if (in_array('--no-ansi', $argv)) { |
|
| 648 | 4 | define('USE_ANSI', false); |
|
| 649 | 5 | } elseif (in_array('--ansi', $argv)) { |
|
| 650 | define('USE_ANSI', true); |
||
| 651 | } else { |
||
| 652 | // On Windows, default to no ANSI, except in ANSICON and ConEmu. |
||
| 653 | // Everywhere else, default to ANSI if stdout is a terminal. |
||
| 654 | 1 | define('USE_ANSI', ('\\' === DIRECTORY_SEPARATOR) |
|
| 655 | 1 | ? (false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI')) |
|
| 656 | 1 | : (function_exists('posix_isatty') && posix_isatty(1))); |
|
| 657 | } |
||
| 658 | |||
| 659 | 5 | foreach ($argv as $key => $val) { |
|
| 660 | 5 | View Code Duplication | if (0 === strpos($val, '--install-dir')) { |
| 661 | 2 | if (13 === strlen($val) && isset($argv[$key + 1])) { |
|
| 662 | 2 | $this->installDir = trim($argv[$key + 1]); |
|
| 663 | 2 | } else { |
|
| 664 | $this->installDir = trim(substr($val, 14)); |
||
| 665 | } |
||
| 666 | 2 | } |
|
| 667 | |||
| 668 | 5 | View Code Duplication | if (0 === strpos($val, '--version')) { |
| 669 | 1 | if (9 === strlen($val) && isset($argv[$key + 1])) { |
|
| 670 | 1 | $this->version = trim($argv[$key + 1]); |
|
| 671 | 1 | } else { |
|
| 672 | $this->version = trim(substr($val, 10)); |
||
| 673 | } |
||
| 674 | 1 | } |
|
| 675 | |||
| 676 | 5 | View Code Duplication | if (0 === strpos($val, '--filename')) { |
| 677 | if (10 === strlen($val) && isset($argv[$key + 1])) { |
||
| 678 | $this->filename = trim($argv[$key + 1]); |
||
| 679 | } else { |
||
| 680 | $this->filename = trim(substr($val, 11)); |
||
| 681 | } |
||
| 682 | } |
||
| 683 | 5 | } |
|
| 684 | 5 | } |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Prints output indicating a success. |
||
| 688 | * |
||
| 689 | * @param string $text The text to print. |
||
| 690 | */ |
||
| 691 | 5 | private function success($text) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Prints output indicating an error. |
||
| 698 | * |
||
| 699 | * @param string $text The text to print. |
||
| 700 | */ |
||
| 701 | 1 | private function error($text) |
|
| 702 | { |
||
| 703 | 1 | printf(USE_ANSI ? "\033[31;31m%s\033[0m" : '%s', $text.PHP_EOL); |
|
| 704 | 1 | } |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Prints output indicating some information. |
||
| 708 | * |
||
| 709 | * @param string $text The text to print. |
||
| 710 | */ |
||
| 711 | 5 | private function info($text) |
|
| 715 | } |
||
| 716 |
If you suppress an error, we recommend checking for the error condition explicitly: