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