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 Yaml 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 Yaml, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Yaml |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @const string |
||
| 16 | */ |
||
| 17 | const BEFORE_FLAG = 'before'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @const string |
||
| 21 | */ |
||
| 22 | const AFTER_FLAG = 'after'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | const ONLY_FLAG = 'only'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const EXCEPT_FLAG = 'except'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A list of files. Real, full path. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $files = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Store the yaml document content as an array. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $documents = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $sort; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Base directory used to find yaml files. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $baseDirectory; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A list of closures to be used in only/except rules. |
||
| 62 | * |
||
| 63 | * @var Closure[] |
||
| 64 | */ |
||
| 65 | protected $rules = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $dir directory to scan for yaml files |
||
|
|
|||
| 69 | */ |
||
| 70 | 14 | public function __construct($baseDir, Finder $finder, $sort = 0) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * This is responsible for parsing a single yaml file and returning it into a format |
||
| 82 | * that Config can understand. Config will then be responsible for turning thie |
||
| 83 | * output into the final merged config. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | 14 | public function transform() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * This allows external rules to be added to only/except checks. Config is only |
||
| 109 | * supposed to be setup once, so adding rules is a one-way system. You cannot |
||
| 110 | * remove rules after being set. This also prevent built-in rules from being |
||
| 111 | * removed. |
||
| 112 | * |
||
| 113 | * @param string $rule |
||
| 114 | * @param Closure $func |
||
| 115 | */ |
||
| 116 | 5 | public function addRule($rule, Closure $func) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Checks to see if a rule is present |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | * |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | 4 | protected function hasRule($rule) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * This allows config to ignore only/except rules that have been set. This enables |
||
| 137 | * apps to ignore built-in rules without causing errors where a rule is undefined. |
||
| 138 | * This, is a one-way system and is only meant to be configured once. When you |
||
| 139 | * ignore a rule, you cannot un-ignore it. |
||
| 140 | * |
||
| 141 | * @param string $rule |
||
| 142 | */ |
||
| 143 | 1 | public function ignoreRule($rule) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Checks to see if a rule is ignored |
||
| 151 | * |
||
| 152 | * @param string $rule |
||
| 153 | * |
||
| 154 | * @return boolean |
||
| 155 | */ |
||
| 156 | 5 | protected function isRuleIgnored($rule) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Returns an array of YAML documents keyed by name. |
||
| 164 | * |
||
| 165 | * @return array; |
||
| 166 | */ |
||
| 167 | 14 | protected function getNamedYamlDocuments() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Because multiple documents aren't supported in symfony/yaml, we have to manually |
||
| 210 | * split the files up into their own documents before running them through the parser. |
||
| 211 | * Note: This is not a complete implementation of multi-document YAML parsing. There |
||
| 212 | * are valid yaml cases where this will fail, however they don't match our use-case. |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | 14 | protected function splitYamlDocuments() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * This generates an array of all document depndencies, keyed by document name. |
||
| 273 | * |
||
| 274 | * @param array $documents |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | 13 | protected function calculateDependencies($documents) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Incapsulates the logic for adding before/after dependencies. |
||
| 323 | * |
||
| 324 | * @param array|string $currentDocument |
||
| 325 | * @param string $name |
||
| 326 | * @param string $flag |
||
| 327 | * @param array $dependencies |
||
| 328 | * @param array $documents |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | 6 | protected function addDependencies($currentDocument, $name, $flag, $dependencies, $documents) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * This returns an array of documents which match the given pattern. The pattern is |
||
| 377 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
| 378 | * |
||
| 379 | * @param string $pattern |
||
| 380 | * @param array |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | 6 | protected function getMatchingDocuments($pattern, $documents) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * We need this to make the path relative from the base directory. We can't use realpath |
||
| 443 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
| 444 | * doesn't support these methods. |
||
| 445 | * |
||
| 446 | * @param string $filename |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | 1 | protected function makeRelative($filename) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * This method gets all headers and all yaml documents and stores them respectively. |
||
| 462 | * |
||
| 463 | * @return array a list of sorted yaml documents |
||
| 464 | */ |
||
| 465 | 14 | protected function getSortedYamlDocuments() |
|
| 491 | |||
| 492 | 14 | protected function filterByOnlyAndExcept($documents) |
|
| 515 | |||
| 516 | 5 | protected function testRules($header, $flag) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Tests a rule against the given expected result. |
||
| 538 | * |
||
| 539 | * @param string $rule |
||
| 540 | * @param string|array $params |
||
| 541 | * |
||
| 542 | * @return boolean |
||
| 543 | */ |
||
| 544 | 4 | protected function testSingleRule($rule, $params) |
|
| 563 | } |
||
| 564 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.