Complex classes like CommandLineOptions 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 CommandLineOptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class CommandLineOptions |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Error code for invalid input |
||
| 34 | */ |
||
| 35 | const INPUT_ERROR = 23; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The minimum rule priority. |
||
| 39 | * |
||
| 40 | * @var integer |
||
| 41 | */ |
||
| 42 | protected $minimumPriority = Rule::LOWEST_PRIORITY; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The maximum rule priority. |
||
| 46 | * |
||
| 47 | * @var integer |
||
| 48 | */ |
||
| 49 | protected $maximumPriority = Rule::HIGHEST_PRIORITY; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * A php source code filename or directory. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $inputPath; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The specified report format. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $reportFormat; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * An optional filename for the generated report. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $reportFile; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Additional report files. |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected $reportFiles = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * A ruleset filename or a comma-separated string of ruleset filenames. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $ruleSets; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * File name of a PHPUnit code coverage report. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $coverageReport; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * A string of comma-separated extensions for valid php source code filenames. |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $extensions; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * A string of comma-separated pattern that is used to exclude directories. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $ignore; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Should the shell show the current phpmd version? |
||
| 109 | * |
||
| 110 | * @var boolean |
||
| 111 | */ |
||
| 112 | protected $version = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Should PHPMD run in strict mode? |
||
| 116 | * |
||
| 117 | * @var boolean |
||
| 118 | * @since 1.2.0 |
||
| 119 | */ |
||
| 120 | protected $strict = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Should PHPMD exit without error code even if violation is found? |
||
| 124 | * |
||
| 125 | * @var boolean |
||
| 126 | */ |
||
| 127 | protected $ignoreViolationsOnExit = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * List of available rule-sets. |
||
| 131 | * |
||
| 132 | * @var array(string) |
||
| 133 | */ |
||
| 134 | protected $availableRuleSets = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Constructs a new command line options instance. |
||
| 138 | * |
||
| 139 | * @param array $args |
||
| 140 | * @param array $availableRuleSets |
||
| 141 | * @throws \InvalidArgumentException |
||
| 142 | */ |
||
| 143 | 40 | public function __construct(array $args, array $availableRuleSets = array()) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Returns a php source code filename or directory. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 6 | public function getInputPath() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the specified report format. |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 2 | public function getReportFormat() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the output filename for a generated report or <b>null</b> when |
||
| 247 | * the report should be displayed in STDOUT. |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | 4 | public function getReportFile() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns a hash with report files specified for different renderers. The |
||
| 258 | * key represents the report format and the value the report file location. |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | 8 | public function getReportFiles() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Returns a ruleset filename or a comma-separated string of ruleset |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 6 | public function getRuleSets() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the minimum rule priority. |
||
| 279 | * |
||
| 280 | * @return integer |
||
| 281 | */ |
||
| 282 | 6 | public function getMinimumPriority() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the maximum rule priority. |
||
| 289 | * |
||
| 290 | * @return integer |
||
| 291 | */ |
||
| 292 | 5 | public function getMaximumPriority() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the file name of a supplied code coverage report or <b>NULL</b> |
||
| 299 | * if the user has not supplied the --coverage option. |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 6 | public function getCoverageReport() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns a string of comma-separated extensions for valid php source code |
||
| 310 | * filenames or <b>null</b> when this argument was not set. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 4 | public function getExtensions() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Returns string of comma-separated pattern that is used to exclude |
||
| 321 | * directories or <b>null</b> when this argument was not set. |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | 4 | public function getIgnore() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Was the <b>--version</b> passed to PHPMD's command line interface? |
||
| 332 | * |
||
| 333 | * @return boolean |
||
| 334 | */ |
||
| 335 | 6 | public function hasVersion() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Was the <b>--strict</b> option passed to PHPMD's command line interface? |
||
| 342 | * |
||
| 343 | * @return boolean |
||
| 344 | * @since 1.2.0 |
||
| 345 | */ |
||
| 346 | 6 | public function hasStrict() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Was the <b>--ignore-violations-on-exit</b> passed to PHPMD's command line interface? |
||
| 353 | * |
||
| 354 | * @return boolean |
||
| 355 | */ |
||
| 356 | 6 | public function ignoreViolationsOnExit() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Creates a report renderer instance based on the user's command line |
||
| 363 | * argument. |
||
| 364 | * |
||
| 365 | * Valid renderers are: |
||
| 366 | * <ul> |
||
| 367 | * <li>xml</li> |
||
| 368 | * <li>html</li> |
||
| 369 | * <li>text</li> |
||
| 370 | * <li>json</li> |
||
| 371 | * </ul> |
||
| 372 | * |
||
| 373 | * @param string $reportFormat |
||
| 374 | * @return \PHPMD\AbstractRenderer |
||
| 375 | * @throws \InvalidArgumentException When the specified renderer does not exist. |
||
| 376 | */ |
||
| 377 | 12 | public function createRenderer($reportFormat = null) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return \PHPMD\Renderer\XMLRenderer |
||
| 397 | */ |
||
| 398 | 1 | protected function createXmlRenderer() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @return \PHPMD\Renderer\TextRenderer |
||
| 405 | */ |
||
| 406 | 5 | protected function createTextRenderer() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @return \PHPMD\Renderer\HTMLRenderer |
||
| 413 | */ |
||
| 414 | 1 | protected function createHtmlRenderer() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @return \PHPMD\Renderer\JSONRenderer |
||
| 421 | */ |
||
| 422 | protected function createJsonRenderer() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return \PHPMD\AbstractRenderer |
||
| 429 | * @throws \InvalidArgumentException |
||
| 430 | */ |
||
| 431 | 5 | protected function createCustomRenderer() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns usage information for the PHPMD command line interface. |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | 4 | public function usage() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Get a list of available renderers |
||
| 499 | * |
||
| 500 | * @return string The list of renderers found. |
||
| 501 | */ |
||
| 502 | 4 | protected function getListOfAvailableRenderers() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Logs a deprecated option to the current user interface. |
||
| 524 | * |
||
| 525 | * @param string $deprecatedName |
||
| 526 | * @param string $newName |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 2 | protected function logDeprecated($deprecatedName, $newName) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * This method takes the given input file, reads the newline separated paths |
||
| 542 | * from that file and creates a comma separated string of the file paths. If |
||
| 543 | * the given <b>$inputFile</b> not exists, this method will throw an |
||
| 544 | * exception. |
||
| 545 | * |
||
| 546 | * @param string $inputFile Specified input file name. |
||
| 547 | * @return string |
||
| 548 | * @throws \InvalidArgumentException If the specified input file does not exist. |
||
| 549 | * @since 1.1.0 |
||
| 550 | */ |
||
| 551 | 6 | protected function readInputFile($inputFile) |
|
| 558 | } |
||
| 559 |
If you suppress an error, we recommend checking for the error condition explicitly: