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 Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Application |
||
| 16 | { |
||
| 17 | protected $outputFile = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Run the cgr tool, a safer alternative to `composer global require`. |
||
| 21 | * |
||
| 22 | * @param array $argv The global $argv array passed in by PHP |
||
| 23 | * @param string $home The path to the composer home directory |
||
| 24 | * @return integer |
||
| 25 | */ |
||
| 26 | public function run($argv, $home) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns the first argument after `help`, or the |
||
| 47 | * first argument if `--help` is present. Otherwise, |
||
| 48 | * returns an empty string. |
||
| 49 | */ |
||
| 50 | public function getHelpArgValue($argv) |
||
| 73 | |||
| 74 | public function help($helpArg) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Set up output redirection. Used by tests. |
||
| 90 | */ |
||
| 91 | public function setOutputFile($outputFile) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Figure out everything we're going to do, but don't do any of it |
||
| 98 | * yet, just return the command objects to run. |
||
| 99 | */ |
||
| 100 | public function parseArgvAndGetCommandList($argv, $home) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Figure out everything we're going to do, but don't do any of it |
||
| 111 | * yet, just return the command objects to run. |
||
| 112 | */ |
||
| 113 | public function separateProjectAndGetCommandList($argv, $home, $options) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Run all of the commands in a list. Abort early if any fail. |
||
| 129 | * |
||
| 130 | * @param array $commandList An array of CommandToExec |
||
| 131 | * @return integer |
||
| 132 | */ |
||
| 133 | public function runCommandList($commandList, $options) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return an array containing a list of commands to execute. Depending on |
||
| 146 | * the composition of the aguments and projects parameters, this list will |
||
| 147 | * contain either a single command string to call through to composer (if |
||
| 148 | * cgr is being used as a composer alias), or it will contain a list of |
||
| 149 | * appropriate replacement 'composer global require' commands that install |
||
| 150 | * each project in its own installation directory, while installing each |
||
| 151 | * projects' binaries in the global Composer bin directory, |
||
| 152 | * ~/.composer/vendor/bin. |
||
| 153 | * |
||
| 154 | * @param array $composerArgs |
||
| 155 | * @param array $projects |
||
| 156 | * @param array $options |
||
| 157 | * @return CommandToExec |
||
| 158 | */ |
||
| 159 | public function getCommandsToExec($command, $composerArgs, $projects, $options) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return our list of default option values, with paths relative to |
||
| 175 | * the provided home directory. |
||
| 176 | * @param string $home The composer home directory |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function getDefaultOptionValues($home) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Replace option default values with the corresponding |
||
| 192 | * environment variable value, if it is set. |
||
| 193 | */ |
||
| 194 | protected function overlayEnvironmentValues($defaults) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * We use our own special-purpose argv parser. The options that apply |
||
| 209 | * to this tool are identified by a simple associative array, where |
||
| 210 | * the key is the option name, and the value is its default value. |
||
| 211 | * The result of this function is an array of two items containing: |
||
| 212 | * - An array of the items in $argv not used to set an option value |
||
| 213 | * - An array of options containing the user-specified or default values |
||
| 214 | * |
||
| 215 | * @param array $argv The global $argv passed in by php |
||
| 216 | * @param array $optionDefaultValues An associative array |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public function parseOutOurOptions($argv, $optionDefaultValues) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * After our options are removed by parseOutOurOptions, those items remaining |
||
| 238 | * in $argv will be separated into a list of projects and versions, and |
||
| 239 | * anything else that is not a project:version. Returns an array of two |
||
| 240 | * items containing: |
||
| 241 | * - An associative array, where the key is the project name and the value |
||
| 242 | * is the version (or an empty string, if no version was specified) |
||
| 243 | * - The remaining $argv items not used to build the projects array. |
||
| 244 | * |
||
| 245 | * @param array $argv The $argv array from parseOutOurOptions() |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public function separateProjectsFromArgs($argv, $options) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Provide a safer version of `composer global require`. Each project |
||
| 299 | * listed in $projects will be installed into its own project directory. |
||
| 300 | * The binaries from each project will still be placed in the global |
||
| 301 | * composer bin directory. |
||
| 302 | * |
||
| 303 | * @param string $execPath The path to composer |
||
| 304 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 305 | * on to Composer |
||
| 306 | * @param array $projects A list of projects to install, with the key |
||
| 307 | * specifying the project name, and the value specifying its version. |
||
| 308 | * @param array $options User options from the command line; see |
||
| 309 | * $optionDefaultValues in the main() function. |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function requireCommand($execPath, $composerArgs, $projects, $options) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * General command handler. |
||
| 324 | * |
||
| 325 | * @param string $composerCommand The composer command to run e.g. require |
||
| 326 | * @param string $execPath The path to composer |
||
| 327 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 328 | * on to Composer |
||
| 329 | * @param array $projects A list of projects to install, with the key |
||
| 330 | * specifying the project name, and the value specifying its version. |
||
| 331 | * @param array $options User options from the command line; see |
||
| 332 | * $optionDefaultValues in the main() function. |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * If --stability VALUE is provided, then run a `composer config minimum-stability VALUE` |
||
| 352 | * command to configure composer.json appropriately. |
||
| 353 | * |
||
| 354 | * @param string $execPath The path to composer |
||
| 355 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 356 | * on to Composer |
||
| 357 | * @param array $projects A list of projects to install, with the key |
||
| 358 | * specifying the project name, and the value specifying its version. |
||
| 359 | * @param array $options User options from the command line; see |
||
| 360 | * $optionDefaultValues in the main() function. |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | public function configureProjectStability($execPath, $composerArgs, $projects, $options) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Run `composer info`. Not only do we want to display the information of |
||
| 384 | * the "global" Composer project, we also want to get the infomation of |
||
| 385 | * all the "isolated" projects installed via cgr in ~/.composer/global. |
||
| 386 | * |
||
| 387 | * @param string $command The path to composer |
||
| 388 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 389 | * on to Composer |
||
| 390 | * @param array $projects A list of projects to update. |
||
| 391 | * @param array $options User options from the command line; see |
||
| 392 | * $optionDefaultValues in the main() function. |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | View Code Duplication | public function infoCommand($execPath, $composerArgs, $projects, $options) |
|
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * Run `composer global update`. Not only do we want to update the |
||
| 408 | * "global" Composer project, we also want to update all of the |
||
| 409 | * "isolated" projects installed via cgr in ~/.composer/global. |
||
| 410 | * |
||
| 411 | * @param string $command The path to composer |
||
| 412 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 413 | * on to Composer |
||
| 414 | * @param array $projects A list of projects to update. |
||
| 415 | * @param array $options User options from the command line; see |
||
| 416 | * $optionDefaultValues in the main() function. |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | View Code Duplication | public function updateCommand($execPath, $composerArgs, $projects, $options) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Convert from an array of projects to an array where the key is the |
||
| 431 | * project name, and the value (version) is an empty string. |
||
| 432 | * |
||
| 433 | * @param string[] $projects |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function flipProjectsArray($projects) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Return $project:$version, or just $project if there is no $version. |
||
| 445 | * |
||
| 446 | * @param string $project The project to install |
||
| 447 | * @param string $version The version desired |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function projectWithVersion($project, $version) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Generate command string to call `composer COMMAND` to install one project. |
||
| 460 | * |
||
| 461 | * @param string $command The path to composer |
||
| 462 | * @param array $composerArgs The arguments to pass to composer |
||
| 463 | * @param string $projectWithVersion The project:version to install |
||
| 464 | * @param array $env Environment to set prior to exec |
||
| 465 | * @param string $installLocation Location to install the project |
||
| 466 | * @return CommandToExec |
||
| 467 | */ |
||
| 468 | public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Generate command string to call `composer config KEY VALUE` to install one project. |
||
| 477 | * |
||
| 478 | * @param string $execPath The path to composer |
||
| 479 | * @param array $composerArgs The arguments to pass to composer |
||
| 480 | * @param string $key The config item to set |
||
| 481 | * @param string $value The value to set the config item to |
||
| 482 | * @param array $env Environment to set prior to exec |
||
| 483 | * @param string $installLocation Location to install the project |
||
| 484 | * @return CommandToExec |
||
| 485 | */ |
||
| 486 | public function buildConfigCommand($execPath, $composerArgs, $key, $value, $env, $installLocation) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Identify an argument that could be a Composer version string. |
||
| 495 | * |
||
| 496 | * @param string $arg The argument to test |
||
| 497 | * @return boolean |
||
| 498 | */ |
||
| 499 | public function isComposerVersion($arg) |
||
| 508 | } |
||
| 509 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: