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) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Set up output redirection. Used by tests. |
||
| 79 | */ |
||
| 80 | public function setOutputFile($outputFile) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Figure out everything we're going to do, but don't do any of it |
||
| 87 | * yet, just return the command objects to run. |
||
| 88 | */ |
||
| 89 | public function parseArgvAndGetCommandList($argv, $home) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Figure out everything we're going to do, but don't do any of it |
||
| 100 | * yet, just return the command objects to run. |
||
| 101 | */ |
||
| 102 | public function separateProjectAndGetCommandList($argv, $home, $options) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Run all of the commands in a list. Abort early if any fail. |
||
| 118 | * |
||
| 119 | * @param array $commandList An array of CommandToExec |
||
| 120 | * @return integer |
||
| 121 | */ |
||
| 122 | public function runCommandList($commandList, $options) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Return an array containing a list of commands to execute. Depending on |
||
| 135 | * the composition of the aguments and projects parameters, this list will |
||
| 136 | * contain either a single command string to call through to composer (if |
||
| 137 | * cgr is being used as a composer alias), or it will contain a list of |
||
| 138 | * appropriate replacement 'composer global require' commands that install |
||
| 139 | * each project in its own installation directory, while installing each |
||
| 140 | * projects' binaries in the global Composer bin directory, |
||
| 141 | * ~/.composer/vendor/bin. |
||
| 142 | * |
||
| 143 | * @param array $composerArgs |
||
| 144 | * @param array $projects |
||
| 145 | * @param array $options |
||
| 146 | * @return CommandToExec |
||
| 147 | */ |
||
| 148 | public function getCommandsToExec($command, $composerArgs, $projects, $options) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return our list of default option values, with paths relative to |
||
| 164 | * the provided home directory. |
||
| 165 | * @param string $home The user's home directory |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function getDefaultOptionValues($home) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Replace option default values with the corresponding |
||
| 181 | * environment variable value, if it is set. |
||
| 182 | */ |
||
| 183 | protected function overlayEnvironmentValues($defaults) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * We use our own special-purpose argv parser. The options that apply |
||
| 198 | * to this tool are identified by a simple associative array, where |
||
| 199 | * the key is the option name, and the value is its default value. |
||
| 200 | * The result of this function is an array of two items containing: |
||
| 201 | * - An array of the items in $argv not used to set an option value |
||
| 202 | * - An array of options containing the user-specified or default values |
||
| 203 | * |
||
| 204 | * @param array $argv The global $argv passed in by php |
||
| 205 | * @param array $optionDefaultValues An associative array |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function parseOutOurOptions($argv, $optionDefaultValues) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * After our options are removed by parseOutOurOptions, those items remaining |
||
| 227 | * in $argv will be separated into a list of projects and versions, and |
||
| 228 | * anything else that is not a project:version. Returns an array of two |
||
| 229 | * items containing: |
||
| 230 | * - An associative array, where the key is the project name and the value |
||
| 231 | * is the version (or an empty string, if no version was specified) |
||
| 232 | * - The remaining $argv items not used to build the projects array. |
||
| 233 | * |
||
| 234 | * @param array $argv The $argv array from parseOutOurOptions() |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function separateProjectsFromArgs($argv, $options) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Provide a safer version of `composer global require`. Each project |
||
| 288 | * listed in $projects will be installed into its own project directory. |
||
| 289 | * The binaries from each project will still be placed in the global |
||
| 290 | * composer bin directory. |
||
| 291 | * |
||
| 292 | * @param string $execPath The path to composer |
||
| 293 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 294 | * on to Composer |
||
| 295 | * @param array $projects A list of projects to install, with the key |
||
| 296 | * specifying the project name, and the value specifying its version. |
||
| 297 | * @param array $options User options from the command line; see |
||
| 298 | * $optionDefaultValues in the main() function. |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function requireCommand($execPath, $composerArgs, $projects, $options) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * General command handler. |
||
| 313 | * |
||
| 314 | * @param string $composerCommand The composer command to run e.g. require |
||
| 315 | * @param string $execPath The path to composer |
||
| 316 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 317 | * on to Composer |
||
| 318 | * @param array $projects A list of projects to install, with the key |
||
| 319 | * specifying the project name, and the value specifying its version. |
||
| 320 | * @param array $options User options from the command line; see |
||
| 321 | * $optionDefaultValues in the main() function. |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * If --stability VALUE is provided, then run a `composer config minimum-stability VALUE` |
||
| 341 | * command to configure composer.json appropriately. |
||
| 342 | * |
||
| 343 | * @param string $execPath The path to composer |
||
| 344 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 345 | * on to Composer |
||
| 346 | * @param array $projects A list of projects to install, with the key |
||
| 347 | * specifying the project name, and the value specifying its version. |
||
| 348 | * @param array $options User options from the command line; see |
||
| 349 | * $optionDefaultValues in the main() function. |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function configureProjectStability($execPath, $composerArgs, $projects, $options) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Run `composer global update`. Not only do we want to update the |
||
| 373 | * "global" Composer project, we also want to update all of the |
||
| 374 | * "isolated" projects installed via cgr in ~/.composer/global. |
||
| 375 | * |
||
| 376 | * @param string $command The path to composer |
||
| 377 | * @param array $composerArgs Anything from the global $argv to be passed |
||
| 378 | * on to Composer |
||
| 379 | * @param array $projects A list of projects to update. |
||
| 380 | * @param array $options User options from the command line; see |
||
| 381 | * $optionDefaultValues in the main() function. |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public function updateCommand($execPath, $composerArgs, $projects, $options) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Convert from an array of projects to an array where the key is the |
||
| 396 | * project name, and the value (version) is an empty string. |
||
| 397 | * |
||
| 398 | * @param string[] $projects |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | public function flipProjectsArray($projects) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Return $project:$version, or just $project if there is no $version. |
||
| 410 | * |
||
| 411 | * @param string $project The project to install |
||
| 412 | * @param string $version The version desired |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function projectWithVersion($project, $version) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Generate command string to call `composer COMMAND` to install one project. |
||
| 425 | * |
||
| 426 | * @param string $command The path to composer |
||
| 427 | * @param array $composerArgs The arguments to pass to composer |
||
| 428 | * @param string $projectWithVersion The project:version to install |
||
| 429 | * @param array $env Environment to set prior to exec |
||
| 430 | * @param string $installLocation Location to install the project |
||
| 431 | * @return CommandToExec |
||
| 432 | */ |
||
| 433 | public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Generate command string to call `composer config KEY VALUE` to install one project. |
||
| 442 | * |
||
| 443 | * @param string $execPath The path to composer |
||
| 444 | * @param array $composerArgs The arguments to pass to composer |
||
| 445 | * @param string $key The config item to set |
||
| 446 | * @param string $value The value to set the config item to |
||
| 447 | * @param array $env Environment to set prior to exec |
||
| 448 | * @param string $installLocation Location to install the project |
||
| 449 | * @return CommandToExec |
||
| 450 | */ |
||
| 451 | public function buildConfigCommand($execPath, $composerArgs, $key, $value, $env, $installLocation) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Identify an argument that could be a Composer version string. |
||
| 460 | * |
||
| 461 | * @param string $arg The argument to test |
||
| 462 | * @return boolean |
||
| 463 | */ |
||
| 464 | public function isComposerVersion($arg) |
||
| 473 | } |
||
| 474 |
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: