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 |
||
| 5 | class Application |
||
| 6 | { |
||
| 7 | protected $outputFile = ''; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Run the cgr tool, a safer alternative to `composer global require`. |
||
| 11 | * |
||
| 12 | * @param array $argv The global $argv array passed in by PHP |
||
| 13 | * @param string $home The path to the user's home directory |
||
| 14 | * @return integer |
||
| 15 | */ |
||
| 16 | public function run($argv, $home) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Set up output redirection. Used by tests. |
||
| 31 | */ |
||
| 32 | public function setOutputFile($outputFile) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Figure out everything we're going to do, but don't do any of it |
||
| 39 | * yet, just return the command objects to run. |
||
| 40 | */ |
||
| 41 | public function parseArgvAndGetCommandList($argv, $home) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Figure out everything we're going to do, but don't do any of it |
||
| 52 | * yet, just return the command objects to run. |
||
| 53 | */ |
||
| 54 | public function separateProjectAndGetCommandList($argv, $home, $options) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Run all of the commands in a list. Abort early if any fail. |
||
| 63 | * |
||
| 64 | * @param array $commandList An array of CommandToExec |
||
| 65 | * @return integer |
||
| 66 | */ |
||
| 67 | public function runCommandList($commandList, $options) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Return an array containing a list of commands to execute. Depending on |
||
| 80 | * the composition of the aguments and projects parameters, this list will |
||
| 81 | * contain either a single command string to call through to composer (if |
||
| 82 | * cgr is being used as a composer alias), or it will contain a list of |
||
| 83 | * appropriate replacement 'composer global require' commands that install |
||
| 84 | * each project in its own installation directory, while installing each |
||
| 85 | * projects' binaries in the global Composer bin directory, |
||
| 86 | * ~/.composer/vendor/bin. |
||
| 87 | * |
||
| 88 | * @param array $composerArgs |
||
| 89 | * @param array $projects |
||
| 90 | * @param array $options |
||
| 91 | * @return CommandToExec |
||
| 92 | */ |
||
| 93 | public function getCommandsToExec($command, $composerArgs, $projects, $options) |
||
| 94 | { |
||
| 95 | $execPath = $options['composer-path']; |
||
| 96 | // If command was not 'global require', 'global update' or |
||
| 97 | // 'global remove', then call through to the standard composer |
||
| 98 | // with all of the original args. |
||
| 99 | if (empty($command)) { |
||
| 100 | return array(new CommandToExec($execPath, $composerArgs)); |
||
| 101 | } |
||
| 102 | // Call requireCommand, updateCommand, or removeCommand, as appropriate. |
||
| 103 | $methodName = "{$command}Command"; |
||
| 104 | if (method_exists($this, $methodName)) { |
||
| 105 | return $this->$methodName($execPath, $composerArgs, $projects, $options); |
||
| 106 | } else { |
||
| 107 | // If there is no specific implementation for the requested command, then call 'generalCommand'. |
||
| 108 | return $this->generalCommand($command, $execPath, $composerArgs, $projects, $options); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Return our list of default option values, with paths relative to |
||
| 114 | * the provided home directory. |
||
| 115 | * @param string $home The user's home directory |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function getDefaultOptionValues($home) |
||
| 119 | { |
||
| 120 | return array( |
||
| 121 | 'composer' => false, |
||
| 122 | 'composer-path' => 'composer', |
||
| 123 | 'base-dir' => "$home/.composer/global", |
||
| 124 | 'bin-dir' => "$home/.composer/vendor/bin", |
||
| 125 | 'stability' => false, |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Replace option default values with the corresponding |
||
| 131 | * environment variable value, if it is set. |
||
| 132 | */ |
||
| 133 | protected function overlayEnvironmentValues($defaults) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * We use our own special-purpose argv parser. The options that apply |
||
| 148 | * to this tool are identified by a simple associative array, where |
||
| 149 | * the key is the option name, and the value is its default value. |
||
| 150 | * The result of this function is an array of two items containing: |
||
| 151 | * - An array of the items in $argv not used to set an option value |
||
| 152 | * - An array of options containing the user-specified or default values |
||
| 153 | * |
||
| 154 | * @param array $argv The global $argv passed in by php |
||
| 155 | * @param array $optionDefaultValues An associative array |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function parseOutOurOptions($argv, $optionDefaultValues) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * After our options are removed by parseOutOurOptions, those items remaining |
||
| 177 | * in $argv will be separated into a list of projects and versions, and |
||
| 178 | * anything else that is not a project:version. Returns an array of two |
||
| 179 | * items containing: |
||
| 180 | * - An associative array, where the key is the project name and the value |
||
| 181 | * is the version (or an empty string, if no version was specified) |
||
| 182 | * - The remaining $argv items not used to build the projects array. |
||
| 183 | * |
||
| 184 | * @param array $argv The $argv array from parseOutOurOptions() |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function separateProjectsFromArgs($argv, $options) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Provide a safer version of `composer global require`. Each project |
||
| 238 | * listed in $projects will be installed into its own project directory. |
||
| 239 | * The binaries from each project will still be placed in the global |
||
| 240 | * composer bin directory. |
||
| 241 | * |
||
| 242 | * @param string $execPath The path to composer |
||
| 243 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 244 | * on to Composer |
||
| 245 | * @param array $projects A list of projects to install, with the key |
||
| 246 | * specifying the project name, and the value specifying its version. |
||
| 247 | * @param array $options User options from the command line; see |
||
| 248 | * $optionDefaultValues in the main() function. |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function requireCommand($execPath, $composerArgs, $projects, $options) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * General command handler. |
||
| 263 | * |
||
| 264 | * @param string $composerCommand The composer command to run e.g. require |
||
| 265 | * @param string $execPath The path to composer |
||
| 266 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 267 | * on to Composer |
||
| 268 | * @param array $projects A list of projects to install, with the key |
||
| 269 | * specifying the project name, and the value specifying its version. |
||
| 270 | * @param array $options User options from the command line; see |
||
| 271 | * $optionDefaultValues in the main() function. |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | public function generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * If --stability VALUE is provided, then run a `composer config minimum-stability VALUE` |
||
| 291 | * command to configure composer.json appropriately. |
||
| 292 | * |
||
| 293 | * @param string $execPath The path to composer |
||
| 294 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 295 | * on to Composer |
||
| 296 | * @param array $projects A list of projects to install, with the key |
||
| 297 | * specifying the project name, and the value specifying its version. |
||
| 298 | * @param array $options User options from the command line; see |
||
| 299 | * $optionDefaultValues in the main() function. |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function configureProjectStability($execPath, $composerArgs, $projects, $options) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Run `composer global update`. Not only do we want to update the |
||
| 323 | * "global" Composer project, we also want to update all of the |
||
| 324 | * "isolated" projects installed via cgr in ~/.composer/global. |
||
| 325 | * |
||
| 326 | * @param string $command 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 update. |
||
| 330 | * @param array $options User options from the command line; see |
||
| 331 | * $optionDefaultValues in the main() function. |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function updateCommand($execPath, $composerArgs, $projects, $options) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Convert from an array of projects to an array where the key is the |
||
| 346 | * project name, and the value (version) is an empty string. |
||
| 347 | * |
||
| 348 | * @param string[] $projects |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public function flipProjectsArray($projects) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Return $project:$version, or just $project if there is no $version. |
||
| 360 | * |
||
| 361 | * @param string $project The project to install |
||
| 362 | * @param string $version The version desired |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function projectWithVersion($project, $version) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Generate command string to call `composer COMMAND` to install one project. |
||
| 375 | * |
||
| 376 | * @param string $command The path to composer |
||
| 377 | * @param array $composerArgs The arguments to pass to composer |
||
| 378 | * @param string $projectWithVersion The project:version to install |
||
| 379 | * @param array $env Environment to set prior to exec |
||
| 380 | * @param string $installLocation Location to install the project |
||
| 381 | * @return CommandToExec |
||
| 382 | */ |
||
| 383 | public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Generate command string to call `composer config KEY VALUE` to install one project. |
||
| 392 | * |
||
| 393 | * @param string $command The path to composer |
||
| 394 | * @param array $composerArgs The arguments to pass to composer |
||
| 395 | * @param string $key The config item to set |
||
| 396 | * @param string $value The value to set the config item to |
||
| 397 | * @param array $env Environment to set prior to exec |
||
| 398 | * @param string $installLocation Location to install the project |
||
| 399 | * @return CommandToExec |
||
| 400 | */ |
||
| 401 | public function buildConfigCommand($execPath, $composerArgs, $key, $value, $env, $installLocation) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Identify an argument that could be a Composer version string. |
||
| 410 | * |
||
| 411 | * @param string $arg The argument to test |
||
| 412 | * @return boolean |
||
| 413 | */ |
||
| 414 | public function isComposerVersion($arg) |
||
| 419 | } |
||
| 420 |
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: