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 user's home directory |
||
| 24 | * @return integer |
||
| 25 | */ |
||
| 26 | public function run($argv, $home) |
||
| 44 | |||
| 45 | public function help($argv) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set up output redirection. Used by tests. |
||
| 87 | */ |
||
| 88 | public function setOutputFile($outputFile) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Figure out everything we're going to do, but don't do any of it |
||
| 95 | * yet, just return the command objects to run. |
||
| 96 | */ |
||
| 97 | public function parseArgvAndGetCommandList($argv, $home) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Figure out everything we're going to do, but don't do any of it |
||
| 108 | * yet, just return the command objects to run. |
||
| 109 | */ |
||
| 110 | public function separateProjectAndGetCommandList($argv, $home, $options) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Run all of the commands in a list. Abort early if any fail. |
||
| 126 | * |
||
| 127 | * @param array $commandList An array of CommandToExec |
||
| 128 | * @return integer |
||
| 129 | */ |
||
| 130 | public function runCommandList($commandList, $options) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return an array containing a list of commands to execute. Depending on |
||
| 143 | * the composition of the aguments and projects parameters, this list will |
||
| 144 | * contain either a single command string to call through to composer (if |
||
| 145 | * cgr is being used as a composer alias), or it will contain a list of |
||
| 146 | * appropriate replacement 'composer global require' commands that install |
||
| 147 | * each project in its own installation directory, while installing each |
||
| 148 | * projects' binaries in the global Composer bin directory, |
||
| 149 | * ~/.composer/vendor/bin. |
||
| 150 | * |
||
| 151 | * @param array $composerArgs |
||
| 152 | * @param array $projects |
||
| 153 | * @param array $options |
||
| 154 | * @return CommandToExec |
||
| 155 | */ |
||
| 156 | public function getCommandsToExec($command, $composerArgs, $projects, $options) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Return our list of default option values, with paths relative to |
||
| 172 | * the provided home directory. |
||
| 173 | * @param string $home The user's home directory |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function getDefaultOptionValues($home) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Replace option default values with the corresponding |
||
| 189 | * environment variable value, if it is set. |
||
| 190 | */ |
||
| 191 | protected function overlayEnvironmentValues($defaults) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * We use our own special-purpose argv parser. The options that apply |
||
| 206 | * to this tool are identified by a simple associative array, where |
||
| 207 | * the key is the option name, and the value is its default value. |
||
| 208 | * The result of this function is an array of two items containing: |
||
| 209 | * - An array of the items in $argv not used to set an option value |
||
| 210 | * - An array of options containing the user-specified or default values |
||
| 211 | * |
||
| 212 | * @param array $argv The global $argv passed in by php |
||
| 213 | * @param array $optionDefaultValues An associative array |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function parseOutOurOptions($argv, $optionDefaultValues) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * After our options are removed by parseOutOurOptions, those items remaining |
||
| 235 | * in $argv will be separated into a list of projects and versions, and |
||
| 236 | * anything else that is not a project:version. Returns an array of two |
||
| 237 | * items containing: |
||
| 238 | * - An associative array, where the key is the project name and the value |
||
| 239 | * is the version (or an empty string, if no version was specified) |
||
| 240 | * - The remaining $argv items not used to build the projects array. |
||
| 241 | * |
||
| 242 | * @param array $argv The $argv array from parseOutOurOptions() |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function separateProjectsFromArgs($argv, $options) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Provide a safer version of `composer global require`. Each project |
||
| 296 | * listed in $projects will be installed into its own project directory. |
||
| 297 | * The binaries from each project will still be placed in the global |
||
| 298 | * composer bin directory. |
||
| 299 | * |
||
| 300 | * @param string $execPath The path to composer |
||
| 301 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 302 | * on to Composer |
||
| 303 | * @param array $projects A list of projects to install, with the key |
||
| 304 | * specifying the project name, and the value specifying its version. |
||
| 305 | * @param array $options User options from the command line; see |
||
| 306 | * $optionDefaultValues in the main() function. |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function requireCommand($execPath, $composerArgs, $projects, $options) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * General command handler. |
||
| 321 | * |
||
| 322 | * @param string $composerCommand The composer command to run e.g. require |
||
| 323 | * @param string $execPath The path to composer |
||
| 324 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 325 | * on to Composer |
||
| 326 | * @param array $projects A list of projects to install, with the key |
||
| 327 | * specifying the project name, and the value specifying its version. |
||
| 328 | * @param array $options User options from the command line; see |
||
| 329 | * $optionDefaultValues in the main() function. |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * If --stability VALUE is provided, then run a `composer config minimum-stability VALUE` |
||
| 349 | * command to configure composer.json appropriately. |
||
| 350 | * |
||
| 351 | * @param string $execPath The path to composer |
||
| 352 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 353 | * on to Composer |
||
| 354 | * @param array $projects A list of projects to install, with the key |
||
| 355 | * specifying the project name, and the value specifying its version. |
||
| 356 | * @param array $options User options from the command line; see |
||
| 357 | * $optionDefaultValues in the main() function. |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function configureProjectStability($execPath, $composerArgs, $projects, $options) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Run `composer info`. Not only do we want to display the information of |
||
| 381 | * the "global" Composer project, we also want to get the infomation of |
||
| 382 | * all the "isolated" projects installed via cgr in ~/.composer/global. |
||
| 383 | * |
||
| 384 | * @param string $command The path to composer |
||
| 385 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 386 | * on to Composer |
||
| 387 | * @param array $projects A list of projects to update. |
||
| 388 | * @param array $options User options from the command line; see |
||
| 389 | * $optionDefaultValues in the main() function. |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function infoCommand($execPath, $composerArgs, $projects, $options) |
|
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Run `composer global update`. Not only do we want to update the |
||
| 405 | * "global" Composer project, we also want to update all of the |
||
| 406 | * "isolated" projects installed via cgr in ~/.composer/global. |
||
| 407 | * |
||
| 408 | * @param string $command The path to composer |
||
| 409 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 410 | * on to Composer |
||
| 411 | * @param array $projects A list of projects to update. |
||
| 412 | * @param array $options User options from the command line; see |
||
| 413 | * $optionDefaultValues in the main() function. |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | View Code Duplication | public function updateCommand($execPath, $composerArgs, $projects, $options) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Convert from an array of projects to an array where the key is the |
||
| 428 | * project name, and the value (version) is an empty string. |
||
| 429 | * |
||
| 430 | * @param string[] $projects |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function flipProjectsArray($projects) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Return $project:$version, or just $project if there is no $version. |
||
| 442 | * |
||
| 443 | * @param string $project The project to install |
||
| 444 | * @param string $version The version desired |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function projectWithVersion($project, $version) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Generate command string to call `composer COMMAND` to install one project. |
||
| 457 | * |
||
| 458 | * @param string $command The path to composer |
||
| 459 | * @param array $composerArgs The arguments to pass to composer |
||
| 460 | * @param string $projectWithVersion The project:version to install |
||
| 461 | * @param array $env Environment to set prior to exec |
||
| 462 | * @param string $installLocation Location to install the project |
||
| 463 | * @return CommandToExec |
||
| 464 | */ |
||
| 465 | public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Generate command string to call `composer config KEY VALUE` to install one project. |
||
| 474 | * |
||
| 475 | * @param string $execPath The path to composer |
||
| 476 | * @param array $composerArgs The arguments to pass to composer |
||
| 477 | * @param string $key The config item to set |
||
| 478 | * @param string $value The value to set the config item to |
||
| 479 | * @param array $env Environment to set prior to exec |
||
| 480 | * @param string $installLocation Location to install the project |
||
| 481 | * @return CommandToExec |
||
| 482 | */ |
||
| 483 | public function buildConfigCommand($execPath, $composerArgs, $key, $value, $env, $installLocation) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Identify an argument that could be a Composer version string. |
||
| 492 | * |
||
| 493 | * @param string $arg The argument to test |
||
| 494 | * @return boolean |
||
| 495 | */ |
||
| 496 | public function isComposerVersion($arg) |
||
| 505 | } |
||
| 506 |
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: