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() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Because multiple documents aren't supported in symfony/yaml, we have to manually |
||
| 206 | * split the files up into their own documents before running them through the parser. |
||
| 207 | * Note: This is not a complete implementation of multi-document YAML parsing. There |
||
| 208 | * are valid yaml cases where this will fail, however they don't match our use-case. |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | 14 | protected function splitYamlDocuments() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * This generates an array of all document depndencies, keyed by document name. |
||
| 269 | * |
||
| 270 | * @param array $documents |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | 14 | protected function calculateDependencies($documents) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Incapsulates the logic for adding before/after dependencies. |
||
| 307 | * |
||
| 308 | * @param array $header |
||
| 309 | * @param string $flag |
||
| 310 | * @param array $dependencies |
||
| 311 | * @param array $documents |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 13 | protected function addDependencies($header, $flag, $dependencies, $documents) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * This returns an array of documents which match the given pattern. The pattern is |
||
| 353 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
| 354 | * |
||
| 355 | * @param string $pattern |
||
| 356 | * @param array |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | 6 | protected function getMatchingDocuments($pattern, $documents) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * We need this to make the path relative from the base directory. We can't use realpath |
||
| 419 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
| 420 | * doesn't support these methods. |
||
| 421 | * |
||
| 422 | * @param string $filename |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | 1 | protected function makeRelative($filename) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * This method gets all headers and all yaml documents and stores them respectively. |
||
| 438 | * |
||
| 439 | * @return array a list of sorted yaml documents |
||
| 440 | */ |
||
| 441 | 14 | protected function getSortedYamlDocuments() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * This filteres out any yaml documents which don't pass their only |
||
| 464 | * or except statement tests. |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | 14 | protected function filterByOnlyAndExcept() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Tests the only except rules for a header. |
||
| 491 | * |
||
| 492 | * @param array $header |
||
| 493 | * @param string $flag |
||
| 494 | * |
||
| 495 | * @return boolean |
||
| 496 | */ |
||
| 497 | 13 | protected function testRules($header, $flag) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Tests a rule against the given expected result. |
||
| 525 | * |
||
| 526 | * @param string $rule |
||
| 527 | * @param string|array $params |
||
| 528 | * |
||
| 529 | * @return boolean |
||
| 530 | */ |
||
| 531 | 4 | protected function testSingleRule($rule, $params) |
|
| 550 | } |
||
| 551 |