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() |
|
129 | |||
130 | /** |
||
131 | * This allows external rules to be added to only/except checks. Config is only |
||
132 | * supposed to be setup once, so adding rules is a one-way system. You cannot |
||
133 | * remove rules after being set. This also prevent built-in rules from being |
||
134 | * removed. |
||
135 | * |
||
136 | * @param string $rule |
||
137 | * @param Closure $func |
||
138 | 5 | */ |
|
139 | public function addRule($rule, Closure $func) |
||
144 | |||
145 | /** |
||
146 | * Checks to see if a rule is present |
||
147 | * |
||
148 | * @var string |
||
149 | * |
||
150 | * @return boolean |
||
151 | 4 | */ |
|
152 | protected function hasRule($rule) |
||
157 | |||
158 | /** |
||
159 | * This allows config to ignore only/except rules that have been set. This enables |
||
160 | * apps to ignore built-in rules without causing errors where a rule is undefined. |
||
161 | * This, is a one-way system and is only meant to be configured once. When you |
||
162 | * ignore a rule, you cannot un-ignore it. |
||
163 | * |
||
164 | * @param string $rule |
||
165 | 1 | */ |
|
166 | public function ignoreRule($rule) |
||
171 | |||
172 | /** |
||
173 | * Checks to see if a rule is ignored |
||
174 | * |
||
175 | * @param string $rule |
||
176 | * |
||
177 | * @return boolean |
||
178 | 5 | */ |
|
179 | protected function isRuleIgnored($rule) |
||
185 | |||
186 | /** |
||
187 | * Returns an array of YAML documents keyed by name. |
||
188 | * |
||
189 | * @return array |
||
190 | 13 | */ |
|
191 | protected function getNamedYamlDocuments() |
||
224 | |||
225 | /** |
||
226 | * Because multiple documents aren't supported in symfony/yaml, we have to manually |
||
227 | * split the files up into their own documents before running them through the parser. |
||
228 | * Note: This is not a complete implementation of multi-document YAML parsing. There |
||
229 | * are valid yaml cases where this will fail, however they don't match our use-case. |
||
230 | * |
||
231 | * @return array |
||
232 | 13 | */ |
|
233 | protected function splitYamlDocuments() |
||
287 | |||
288 | /** |
||
289 | * This generates an array of all document depndencies, keyed by document name. |
||
290 | * |
||
291 | * @param array $documents |
||
292 | * |
||
293 | * @return array |
||
294 | 13 | */ |
|
295 | protected function calculateDependencies($documents) |
||
325 | |||
326 | /** |
||
327 | * Incapsulates the logic for adding before/after dependencies. |
||
328 | * |
||
329 | * @param array $header |
||
330 | * @param string $flag |
||
331 | * @param array $dependencies |
||
332 | * @param array $documents |
||
333 | * |
||
334 | * @return array |
||
335 | 12 | */ |
|
336 | protected function addDependencies($header, $flag, $dependencies, $documents) |
||
371 | |||
372 | /** |
||
373 | * This returns an array of documents which match the given pattern. The pattern is |
||
374 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
375 | * |
||
376 | * @param string $pattern |
||
377 | * @param array |
||
378 | * |
||
379 | * @return array |
||
380 | 6 | */ |
|
381 | protected function getMatchingDocuments($pattern, $documents) |
||
437 | |||
438 | /** |
||
439 | * We need this to make the path relative from the base directory. We can't use realpath |
||
440 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
441 | * doesn't support these methods. |
||
442 | * |
||
443 | * @param string $filename |
||
444 | * |
||
445 | * @return string |
||
446 | 1 | */ |
|
447 | protected function makeRelative($filename) |
||
456 | |||
457 | /** |
||
458 | * This method gets all headers and all yaml documents and stores them respectively. |
||
459 | * |
||
460 | * @return array a list of sorted yaml documents |
||
461 | 13 | */ |
|
462 | protected function getSortedYamlDocuments() |
||
482 | |||
483 | /** |
||
484 | * This filteres out any yaml documents which don't pass their only |
||
485 | * or except statement tests. |
||
486 | * |
||
487 | * @return array |
||
488 | 13 | */ |
|
489 | protected function filterByOnlyAndExcept() |
||
509 | |||
510 | /** |
||
511 | * Tests the only except rules for a header. |
||
512 | * |
||
513 | * @param array $header |
||
514 | * @param string $flag |
||
515 | * |
||
516 | * @return boolean |
||
517 | 12 | */ |
|
518 | protected function testRules($header, $flag) |
||
543 | |||
544 | /** |
||
545 | * Tests a rule against the given expected result. |
||
546 | * |
||
547 | * @param string $rule |
||
548 | * @param string|array $params |
||
549 | * |
||
550 | * @return boolean |
||
551 | 4 | */ |
|
552 | protected function testSingleRule($rule, $params) |
||
571 | } |
||
572 |