Complex classes like YamlTransformer 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 YamlTransformer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class YamlTransformer implements TransformerInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @const string |
||
| 18 | */ |
||
| 19 | const BEFORE_FLAG = 'before'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @const string |
||
| 23 | */ |
||
| 24 | const AFTER_FLAG = 'after'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | const ONLY_FLAG = 'only'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const EXCEPT_FLAG = 'except'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * A list of files. Real, full path. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $files = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Store the yaml document content as an array. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $documents = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var ConfigCollectionInterface |
||
| 52 | */ |
||
| 53 | protected $collection; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Base directory used to find yaml files. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $baseDirectory; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * A list of closures to be used in only/except rules. |
||
| 64 | * |
||
| 65 | * @var Closure[] |
||
| 66 | */ |
||
| 67 | protected $rules = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * A list of ignored before/after statements. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $ignoreRules = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $baseDir directory to scan for yaml files |
||
| 78 | * @param Finder $finder |
||
| 79 | * @param ConfigCollectionInterface $collection |
||
| 80 | */ |
||
| 81 | 13 | public function __construct($baseDir, Finder $finder, ConfigCollectionInterface $collection) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * This is responsible for parsing a single yaml file and returning it into a format |
||
| 93 | * that Config can understand. Config will then be responsible for turning thie |
||
| 94 | * output into the final merged config. |
||
| 95 | * |
||
| 96 | * @return ConfigCollectionInterface |
||
| 97 | */ |
||
| 98 | 13 | public function transform() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * This allows external rules to be added to only/except checks. Config is only |
||
| 115 | * supposed to be setup once, so adding rules is a one-way system. You cannot |
||
| 116 | * remove rules after being set. This also prevent built-in rules from being |
||
| 117 | * removed. |
||
| 118 | * |
||
| 119 | * @param string $rule |
||
| 120 | * @param Closure $func |
||
| 121 | */ |
||
| 122 | 5 | public function addRule($rule, Closure $func) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Checks to see if a rule is present |
||
| 130 | * |
||
| 131 | * @var string |
||
| 132 | * |
||
| 133 | * @return boolean |
||
| 134 | */ |
||
| 135 | 4 | protected function hasRule($rule) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * This allows config to ignore only/except rules that have been set. This enables |
||
| 143 | * apps to ignore built-in rules without causing errors where a rule is undefined. |
||
| 144 | * This, is a one-way system and is only meant to be configured once. When you |
||
| 145 | * ignore a rule, you cannot un-ignore it. |
||
| 146 | * |
||
| 147 | * @param string $rule |
||
| 148 | */ |
||
| 149 | 1 | public function ignoreRule($rule) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Checks to see if a rule is ignored |
||
| 157 | * |
||
| 158 | * @param string $rule |
||
| 159 | * |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | 5 | protected function isRuleIgnored($rule) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Returns an array of YAML documents keyed by name. |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 13 | 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 | 13 | 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) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Incapsulates the logic for adding before/after dependencies. |
||
| 311 | * |
||
| 312 | * @param array $header |
||
| 313 | * @param string $flag |
||
| 314 | * @param array $dependencies |
||
| 315 | * @param array $documents |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 12 | protected function addDependencies($header, $flag, $dependencies, $documents) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * This returns an array of documents which match the given pattern. The pattern is |
||
| 357 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
| 358 | * |
||
| 359 | * @param string $pattern |
||
| 360 | * @param array |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | 6 | protected function getMatchingDocuments($pattern, $documents) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * We need this to make the path relative from the base directory. We can't use realpath |
||
| 423 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
| 424 | * doesn't support these methods. |
||
| 425 | * |
||
| 426 | * @param string $filename |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 1 | protected function makeRelative($filename) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * This method gets all headers and all yaml documents and stores them respectively. |
||
| 442 | * |
||
| 443 | * @return array a list of sorted yaml documents |
||
| 444 | */ |
||
| 445 | 13 | protected function getSortedYamlDocuments() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * This filteres out any yaml documents which don't pass their only |
||
| 468 | * or except statement tests. |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | 13 | protected function filterByOnlyAndExcept() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Tests the only except rules for a header. |
||
| 495 | * |
||
| 496 | * @param array $header |
||
| 497 | * @param string $flag |
||
| 498 | * |
||
| 499 | * @return boolean |
||
| 500 | */ |
||
| 501 | 12 | protected function testRules($header, $flag) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Tests a rule against the given expected result. |
||
| 529 | * |
||
| 530 | * @param string $rule |
||
| 531 | * @param string|array $params |
||
| 532 | * |
||
| 533 | * @return boolean |
||
| 534 | */ |
||
| 535 | 4 | protected function testSingleRule($rule, $params) |
|
| 554 | } |
||
| 555 |