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 |
||
13 | class YamlTransformer implements TransformerInterface |
||
14 | { |
||
15 | /** |
||
16 | * @const string |
||
17 | */ |
||
18 | const BEFORE_FLAG = 'before'; |
||
19 | |||
20 | /** |
||
21 | * @const string |
||
22 | */ |
||
23 | const AFTER_FLAG = 'after'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | const ONLY_FLAG = 'only'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | const EXCEPT_FLAG = 'except'; |
||
34 | |||
35 | /** |
||
36 | * A list of files. Real, full path. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $files = []; |
||
41 | |||
42 | /** |
||
43 | * Store the yaml document content as an array. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $documents = []; |
||
48 | |||
49 | /** |
||
50 | * Base directory used to find yaml files. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $baseDirectory; |
||
55 | |||
56 | /** |
||
57 | * A list of closures to be used in only/except rules. |
||
58 | * |
||
59 | * @var Closure[] |
||
60 | */ |
||
61 | protected $rules = []; |
||
62 | |||
63 | /** |
||
64 | * A list of ignored before/after statements. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $ignoreRules = []; |
||
69 | |||
70 | /** |
||
71 | * @param string $baseDir directory to scan for yaml files |
||
72 | * @param Finder $finder |
||
73 | */ |
||
74 | 14 | public function __construct($baseDir, Finder $finder) |
|
82 | |||
83 | /** |
||
84 | * @param string $baseDir directory to scan for yaml files |
||
85 | * @param Finder $finder |
||
86 | * @return static |
||
87 | */ |
||
88 | public static function create($baseDir, Finder $finder) |
||
92 | |||
93 | /** |
||
94 | * This is responsible for parsing a single yaml file and returning it into a format |
||
95 | * that Config can understand. Config will then be responsible for turning thie |
||
96 | * output into the final merged config. |
||
97 | * |
||
98 | * @param MutableConfigCollectionInterface $collection |
||
99 | * @return MutableConfigCollectionInterface |
||
100 | */ |
||
101 | 14 | public function transform(MutableConfigCollectionInterface $collection) |
|
130 | |||
131 | /** |
||
132 | * This allows external rules to be added to only/except checks. Config is only |
||
133 | * supposed to be setup once, so adding rules is a one-way system. You cannot |
||
134 | * remove rules after being set. This also prevent built-in rules from being |
||
135 | * removed. |
||
136 | * |
||
137 | * @param string $rule |
||
138 | * @param Closure $func |
||
139 | * @return $this |
||
140 | */ |
||
141 | 5 | public function addRule($rule, Closure $func) |
|
147 | |||
148 | /** |
||
149 | * Checks to see if a rule is present |
||
150 | * |
||
151 | * @var string |
||
152 | * |
||
153 | * @return boolean |
||
154 | */ |
||
155 | 4 | protected function hasRule($rule) |
|
160 | |||
161 | /** |
||
162 | * This allows config to ignore only/except rules that have been set. This enables |
||
163 | * apps to ignore built-in rules without causing errors where a rule is undefined. |
||
164 | * This, is a one-way system and is only meant to be configured once. When you |
||
165 | * ignore a rule, you cannot un-ignore it. |
||
166 | * |
||
167 | * @param string $rule |
||
168 | */ |
||
169 | 1 | public function ignoreRule($rule) |
|
174 | |||
175 | /** |
||
176 | * Checks to see if a rule is ignored |
||
177 | * |
||
178 | * @param string $rule |
||
179 | * |
||
180 | * @return boolean |
||
181 | */ |
||
182 | 5 | protected function isRuleIgnored($rule) |
|
188 | |||
189 | /** |
||
190 | * Returns an array of YAML documents keyed by name. |
||
191 | * |
||
192 | * @return array |
||
193 | * @throws Exception |
||
194 | */ |
||
195 | 14 | protected function getNamedYamlDocuments() |
|
235 | |||
236 | /** |
||
237 | * Because multiple documents aren't supported in symfony/yaml, we have to manually |
||
238 | * split the files up into their own documents before running them through the parser. |
||
239 | * Note: This is not a complete implementation of multi-document YAML parsing. There |
||
240 | * are valid yaml cases where this will fail, however they don't match our use-case. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | 14 | protected function splitYamlDocuments() |
|
297 | |||
298 | /** |
||
299 | * This generates an array of all document depndencies, keyed by document name. |
||
300 | * |
||
301 | * @param array $documents |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | 14 | protected function calculateDependencies($documents) |
|
335 | |||
336 | /** |
||
337 | * Incapsulates the logic for adding before/after dependencies. |
||
338 | * |
||
339 | * @param array $header |
||
340 | * @param string $flag |
||
341 | * @param array $dependencies |
||
342 | * @param array $documents |
||
343 | * |
||
344 | * @return array |
||
345 | */ |
||
346 | 13 | protected function addDependencies($header, $flag, $dependencies, $documents) |
|
381 | |||
382 | /** |
||
383 | * This returns an array of documents which match the given pattern. The pattern is |
||
384 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
385 | * |
||
386 | * @param string $pattern |
||
387 | * @param array $documents |
||
388 | * @param string $flag 'before' / 'after' |
||
389 | * @return array |
||
390 | */ |
||
391 | 8 | protected function getMatchingDocuments($pattern, $documents, $flag) |
|
466 | |||
467 | /** |
||
468 | * We need this to make the path relative from the base directory. We can't use realpath |
||
469 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
470 | * doesn't support these methods. |
||
471 | * |
||
472 | * @param string $filename |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | 2 | protected function makeRelative($filename) |
|
485 | |||
486 | /** |
||
487 | * This method gets all headers and all yaml documents and stores them respectively. |
||
488 | * |
||
489 | * @return array a list of sorted yaml documents |
||
490 | */ |
||
491 | 14 | protected function getSortedYamlDocuments() |
|
511 | |||
512 | /** |
||
513 | * This filteres out any yaml documents which don't pass their only |
||
514 | * or except statement tests. |
||
515 | * |
||
516 | * @return array |
||
517 | */ |
||
518 | 14 | protected function filterByOnlyAndExcept() |
|
538 | |||
539 | /** |
||
540 | * Tests the only except rules for a header. |
||
541 | * |
||
542 | * @param array $header |
||
543 | * @param string $flag |
||
544 | * @return bool |
||
545 | * @throws Exception |
||
546 | */ |
||
547 | 13 | protected function testRules($header, $flag) |
|
568 | |||
569 | /** |
||
570 | * Tests a rule against the given expected result. |
||
571 | * |
||
572 | * @param string $rule |
||
573 | * @param string|array $params |
||
574 | * @param string $flag |
||
575 | * @return bool |
||
576 | * @throws Exception |
||
577 | */ |
||
578 | 4 | protected function testSingleRule($rule, $params, $flag = self::ONLY_FLAG) |
|
601 | |||
602 | /** |
||
603 | * Evaluate condition against a set of data using the appropriate conjuction for the flag |
||
604 | * |
||
605 | * @param array $source Items to apply condition to |
||
606 | * @param string $flag Flag type |
||
607 | * @param callable $condition Callback to evaluate each item in the $source. Both key and value |
||
608 | * of each item in $source will be passed as arguments. This callback should return true, false, |
||
609 | * or null to skip |
||
610 | * @return bool Evaluation of the applied condition |
||
611 | */ |
||
612 | 5 | protected function evaluateConditions($source, $flag, $condition) |
|
635 | } |
||
636 |