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) |
||
205 | |||
206 | /** |
||
207 | * We use our own special-purpose argv parser. The options that apply |
||
208 | * to this tool are identified by a simple associative array, where |
||
209 | * the key is the option name, and the value is its default value. |
||
210 | * The result of this function is an array of two items containing: |
||
211 | * - An array of the items in $argv not used to set an option value |
||
212 | * - An array of options containing the user-specified or default values |
||
213 | * |
||
214 | * @param array $argv The global $argv passed in by php |
||
215 | * @param array $optionDefaultValues An associative array |
||
216 | * @return array |
||
217 | */ |
||
218 | public function parseOutOurOptions($argv, $optionDefaultValues) |
||
234 | |||
235 | /** |
||
236 | * After our options are removed by parseOutOurOptions, those items remaining |
||
237 | * in $argv will be separated into a list of projects and versions, and |
||
238 | * anything else that is not a project:version. Returns an array of two |
||
239 | * items containing: |
||
240 | * - An associative array, where the key is the project name and the value |
||
241 | * is the version (or an empty string, if no version was specified) |
||
242 | * - The remaining $argv items not used to build the projects array. |
||
243 | * |
||
244 | * @param array $argv The $argv array from parseOutOurOptions() |
||
245 | * @return array |
||
246 | */ |
||
247 | public function separateProjectsFromArgs($argv, $options) |
||
297 | |||
298 | /** |
||
299 | * Provide a safer version of `composer global require`. Each project |
||
300 | * listed in $projects will be installed into its own project directory. |
||
301 | * The binaries from each project will still be placed in the global |
||
302 | * composer bin directory. |
||
303 | * |
||
304 | * @param string $execPath The path to composer |
||
305 | * @param array $composerArgs Anything from the global $argv to be passed |
||
306 | * on to Composer |
||
307 | * @param array $projects A list of projects to install, with the key |
||
308 | * specifying the project name, and the value specifying its version. |
||
309 | * @param array $options User options from the command line; see |
||
310 | * $optionDefaultValues in the main() function. |
||
311 | * @return array |
||
312 | */ |
||
313 | public function requireCommand($execPath, $composerArgs, $projects, $options) |
||
322 | |||
323 | public function extendCommand($execPath, $composerArgs, $projects, $options) |
||
341 | |||
342 | protected function getProjectToExtend($projects) |
||
349 | |||
350 | /** |
||
351 | * General command handler. |
||
352 | * |
||
353 | * @param string $composerCommand The composer command to run e.g. require |
||
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 generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
||
378 | |||
379 | /** |
||
380 | * Remove command handler. Build an `rm -rf` command. |
||
381 | * |
||
382 | * @param string $execPath The path to composer (ignored) |
||
383 | * @param array $composerArgs Anything from the global $argv to be passed |
||
384 | * on to Composer (ignored) |
||
385 | * @param array $projects A list of projects to install, with the key |
||
386 | * specifying the project name, and the value specifying its version. |
||
387 | * @param array $options User options from the command line; see |
||
388 | * $optionDefaultValues in the main() function. |
||
389 | * @return array |
||
390 | */ |
||
391 | public function removeCommand($execPath, $composerArgs, $projects, $options) |
||
402 | |||
403 | /** |
||
404 | * Command handler for commands where the project should not be provided |
||
405 | * as a parameter to Composer (e.g. 'update'). |
||
406 | * |
||
407 | * @param string $composerCommand The composer command to run e.g. require |
||
408 | * @param string $execPath 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 install, with the key |
||
412 | * specifying the project name, and the value specifying its version. |
||
413 | * @param array $options User options from the command line; see |
||
414 | * $optionDefaultValues in the main() function. |
||
415 | * @return array |
||
416 | */ |
||
417 | public function noProjectArgCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
||
430 | |||
431 | /** |
||
432 | * If --stability VALUE is provided, then run a `composer config minimum-stability VALUE` |
||
433 | * command to configure composer.json appropriately. |
||
434 | * |
||
435 | * @param string $execPath The path to composer |
||
436 | * @param array $composerArgs Anything from the global $argv to be passed |
||
437 | * on to Composer |
||
438 | * @param array $projects A list of projects to install, with the key |
||
439 | * specifying the project name, and the value specifying its version. |
||
440 | * @param array $options User options from the command line; see |
||
441 | * $optionDefaultValues in the main() function. |
||
442 | * @return array |
||
443 | */ |
||
444 | public function configureProjectStability($execPath, $composerArgs, $projects, $options) |
||
461 | |||
462 | /** |
||
463 | * Run `composer info`. Not only do we want to display the information of |
||
464 | * the "global" Composer project, we also want to get the infomation of |
||
465 | * all the "isolated" projects installed via cgr in ~/.composer/global. |
||
466 | * |
||
467 | * @param string $command The path to composer |
||
468 | * @param array $composerArgs Anything from the global $argv to be passed |
||
469 | * on to Composer |
||
470 | * @param array $projects A list of projects to update. |
||
471 | * @param array $options User options from the command line; see |
||
472 | * $optionDefaultValues in the main() function. |
||
473 | * @return array |
||
474 | */ |
||
475 | View Code Duplication | public function infoCommand($execPath, $composerArgs, $projects, $options) |
|
484 | |||
485 | /** |
||
486 | * Run `composer global update`. Not only do we want to update the |
||
487 | * "global" Composer project, we also want to update all of the |
||
488 | * "isolated" projects installed via cgr in ~/.composer/global. |
||
489 | * |
||
490 | * @param string $command The path to composer |
||
491 | * @param array $composerArgs Anything from the global $argv to be passed |
||
492 | * on to Composer |
||
493 | * @param array $projects A list of projects to update. |
||
494 | * @param array $options User options from the command line; see |
||
495 | * $optionDefaultValues in the main() function. |
||
496 | * @return array |
||
497 | */ |
||
498 | View Code Duplication | public function updateCommand($execPath, $composerArgs, $projects, $options) |
|
507 | |||
508 | /** |
||
509 | * Convert from an array of projects to an array where the key is the |
||
510 | * project name, and the value (version) is an empty string. |
||
511 | * |
||
512 | * @param string[] $projects |
||
513 | * @return array |
||
514 | */ |
||
515 | public function flipProjectsArray($projects) |
||
521 | |||
522 | /** |
||
523 | * Return $project:$version, or just $project if there is no $version. |
||
524 | * |
||
525 | * @param string $project The project to install |
||
526 | * @param string $version The version desired |
||
527 | * @return string |
||
528 | */ |
||
529 | public function projectWithVersion($project, $version) |
||
536 | |||
537 | /** |
||
538 | * Generate command string to call `composer COMMAND` to install one project. |
||
539 | * |
||
540 | * @param string $command The path to composer |
||
541 | * @param array $composerArgs The arguments to pass to composer |
||
542 | * @param string $projectWithVersion The project:version to install |
||
543 | * @param array $env Environment to set prior to exec |
||
544 | * @param string $installLocation Location to install the project |
||
545 | * @return CommandToExec |
||
546 | */ |
||
547 | public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation) |
||
556 | |||
557 | /** |
||
558 | * Generate command string to call `composer config KEY VALUE` to install one project. |
||
559 | * |
||
560 | * @param string $execPath The path to composer |
||
561 | * @param array $composerArgs The arguments to pass to composer |
||
562 | * @param string $key The config item to set |
||
563 | * @param string $value The value to set the config item to |
||
564 | * @param array $env Environment to set prior to exec |
||
565 | * @param string $installLocation Location to install the project |
||
566 | * @return CommandToExec |
||
567 | */ |
||
568 | public function buildConfigCommand($execPath, $composerArgs, $key, $value, $env, $installLocation) |
||
574 | |||
575 | /** |
||
576 | * Identify an argument that could be a Composer version string. |
||
577 | * |
||
578 | * @param string $arg The argument to test |
||
579 | * @return boolean |
||
580 | */ |
||
581 | public function isComposerVersion($arg) |
||
590 | } |
||
591 |
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: