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 | * A list of ignored before/after statements. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $ignoreRules = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $baseDir directory to scan for yaml files |
||
| 76 | * @param Finder $finder |
||
| 77 | * @param int $sort |
||
| 78 | */ |
||
| 79 | 14 | public function __construct($baseDir, Finder $finder, $sort = 0) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * This is responsible for parsing a single yaml file and returning it into a format |
||
| 91 | * that Config can understand. Config will then be responsible for turning thie |
||
| 92 | * output into the final merged config. |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | 14 | public function transform() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * This allows external rules to be added to only/except checks. Config is only |
||
| 112 | * supposed to be setup once, so adding rules is a one-way system. You cannot |
||
| 113 | * remove rules after being set. This also prevent built-in rules from being |
||
| 114 | * removed. |
||
| 115 | * |
||
| 116 | * @param string $rule |
||
| 117 | * @param Closure $func |
||
| 118 | */ |
||
| 119 | 5 | public function addRule($rule, Closure $func) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Checks to see if a rule is present |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | * |
||
| 130 | * @return boolean |
||
| 131 | */ |
||
| 132 | 4 | protected function hasRule($rule) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * This allows config to ignore only/except rules that have been set. This enables |
||
| 140 | * apps to ignore built-in rules without causing errors where a rule is undefined. |
||
| 141 | * This, is a one-way system and is only meant to be configured once. When you |
||
| 142 | * ignore a rule, you cannot un-ignore it. |
||
| 143 | * |
||
| 144 | * @param string $rule |
||
| 145 | */ |
||
| 146 | 1 | public function ignoreRule($rule) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Checks to see if a rule is ignored |
||
| 154 | * |
||
| 155 | * @param string $rule |
||
| 156 | * |
||
| 157 | * @return boolean |
||
| 158 | */ |
||
| 159 | 5 | protected function isRuleIgnored($rule) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Returns an array of YAML documents keyed by name. |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 14 | protected function getNamedYamlDocuments() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Because multiple documents aren't supported in symfony/yaml, we have to manually |
||
| 213 | * split the files up into their own documents before running them through the parser. |
||
| 214 | * Note: This is not a complete implementation of multi-document YAML parsing. There |
||
| 215 | * are valid yaml cases where this will fail, however they don't match our use-case. |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | 14 | protected function splitYamlDocuments() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * This generates an array of all document depndencies, keyed by document name. |
||
| 276 | * |
||
| 277 | * @param array $documents |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 13 | protected function calculateDependencies($documents) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Incapsulates the logic for adding before/after dependencies. |
||
| 319 | * |
||
| 320 | * @param array $header |
||
| 321 | * @param string $flag |
||
| 322 | * @param array $dependencies |
||
| 323 | * @param array $documents |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | 13 | protected function addDependencies($header, $flag, $dependencies, $documents) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * This returns an array of documents which match the given pattern. The pattern is |
||
| 379 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
| 380 | * |
||
| 381 | * @param string $pattern |
||
| 382 | * @param array |
||
| 383 | * |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | 6 | protected function getMatchingDocuments($pattern, $documents) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * We need this to make the path relative from the base directory. We can't use realpath |
||
| 445 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
| 446 | * doesn't support these methods. |
||
| 447 | * |
||
| 448 | * @param string $filename |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | 1 | protected function makeRelative($filename) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * This method gets all headers and all yaml documents and stores them respectively. |
||
| 464 | * |
||
| 465 | * @return array a list of sorted yaml documents |
||
| 466 | */ |
||
| 467 | 14 | protected function getSortedYamlDocuments() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * This filteres out any yaml documents which don't pass their only |
||
| 496 | * or except statement tests. |
||
| 497 | * |
||
| 498 | * @param array $documents |
||
| 499 | * |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | 14 | protected function filterByOnlyAndExcept($documents) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Tests the only except rules for a header. |
||
| 524 | * |
||
| 525 | * @param array $header |
||
| 526 | * @param string $flag |
||
| 527 | * |
||
| 528 | * @return boolean |
||
| 529 | */ |
||
| 530 | 13 | protected function testRules($header, $flag) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Tests a rule against the given expected result. |
||
| 558 | * |
||
| 559 | * @param string $rule |
||
| 560 | * @param string|array $params |
||
| 561 | * |
||
| 562 | * @return boolean |
||
| 563 | */ |
||
| 564 | 4 | protected function testSingleRule($rule, $params) |
|
| 583 | } |
||
| 584 |