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 |
||
15 | class YamlTransformer implements TransformerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @const string |
||
19 | */ |
||
20 | const BEFORE_FLAG = 'before'; |
||
21 | |||
22 | /** |
||
23 | * @const string |
||
24 | */ |
||
25 | const AFTER_FLAG = 'after'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | const ONLY_FLAG = 'only'; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | const EXCEPT_FLAG = 'except'; |
||
36 | |||
37 | /** |
||
38 | * A list of files. Real, full path. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $files = []; |
||
43 | |||
44 | /** |
||
45 | * Store the yaml document content as an array. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $documents = []; |
||
50 | |||
51 | /** |
||
52 | * @var ConfigCollectionInterface |
||
53 | */ |
||
54 | protected $collection; |
||
55 | |||
56 | /** |
||
57 | * Base directory used to find yaml files. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $baseDirectory; |
||
62 | |||
63 | /** |
||
64 | * A list of closures to be used in only/except rules. |
||
65 | * |
||
66 | * @var Closure[] |
||
67 | */ |
||
68 | protected $rules = []; |
||
69 | |||
70 | /** |
||
71 | * A list of ignored before/after statements. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $ignoreRules = []; |
||
76 | |||
77 | /** |
||
78 | * @param string $baseDir directory to scan for yaml files |
||
79 | * @param Finder $finder |
||
80 | * @param ConfigCollectionInterface $collection |
||
81 | */ |
||
82 | 13 | public function __construct($baseDir, Finder $finder, ConfigCollectionInterface $collection) |
|
91 | |||
92 | /** |
||
93 | * This is responsible for parsing a single yaml file and returning it into a format |
||
94 | * that Config can understand. Config will then be responsible for turning thie |
||
95 | * output into the final merged config. |
||
96 | * |
||
97 | * @return ConfigCollectionInterface |
||
98 | */ |
||
99 | 13 | public function transform() |
|
127 | |||
128 | /** |
||
129 | * This allows external rules to be added to only/except checks. Config is only |
||
130 | * supposed to be setup once, so adding rules is a one-way system. You cannot |
||
131 | * remove rules after being set. This also prevent built-in rules from being |
||
132 | * removed. |
||
133 | * |
||
134 | * @param string $rule |
||
135 | * @param Closure $func |
||
136 | */ |
||
137 | 5 | public function addRule($rule, Closure $func) |
|
142 | |||
143 | /** |
||
144 | * Checks to see if a rule is present |
||
145 | * |
||
146 | * @var string |
||
147 | * |
||
148 | * @return boolean |
||
149 | */ |
||
150 | 4 | protected function hasRule($rule) |
|
155 | |||
156 | /** |
||
157 | * This allows config to ignore only/except rules that have been set. This enables |
||
158 | * apps to ignore built-in rules without causing errors where a rule is undefined. |
||
159 | * This, is a one-way system and is only meant to be configured once. When you |
||
160 | * ignore a rule, you cannot un-ignore it. |
||
161 | * |
||
162 | * @param string $rule |
||
163 | */ |
||
164 | 1 | public function ignoreRule($rule) |
|
169 | |||
170 | /** |
||
171 | * Checks to see if a rule is ignored |
||
172 | * |
||
173 | * @param string $rule |
||
174 | * |
||
175 | * @return boolean |
||
176 | */ |
||
177 | 5 | protected function isRuleIgnored($rule) |
|
183 | |||
184 | /** |
||
185 | * Returns an array of YAML documents keyed by name. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 13 | protected function getNamedYamlDocuments() |
|
222 | |||
223 | /** |
||
224 | * Because multiple documents aren't supported in symfony/yaml, we have to manually |
||
225 | * split the files up into their own documents before running them through the parser. |
||
226 | * Note: This is not a complete implementation of multi-document YAML parsing. There |
||
227 | * are valid yaml cases where this will fail, however they don't match our use-case. |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 13 | protected function splitYamlDocuments() |
|
285 | |||
286 | /** |
||
287 | * This generates an array of all document depndencies, keyed by document name. |
||
288 | * |
||
289 | * @param array $documents |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | 13 | protected function calculateDependencies($documents) |
|
323 | |||
324 | /** |
||
325 | * Incapsulates the logic for adding before/after dependencies. |
||
326 | * |
||
327 | * @param array $header |
||
328 | * @param string $flag |
||
329 | * @param array $dependencies |
||
330 | * @param array $documents |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | 12 | protected function addDependencies($header, $flag, $dependencies, $documents) |
|
369 | |||
370 | /** |
||
371 | * This returns an array of documents which match the given pattern. The pattern is |
||
372 | * expected to come from before/after blocks of yaml (eg. framwork/*). |
||
373 | * |
||
374 | * @param string $pattern |
||
375 | * @param array |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | 6 | protected function getMatchingDocuments($pattern, $documents) |
|
435 | |||
436 | /** |
||
437 | * We need this to make the path relative from the base directory. We can't use realpath |
||
438 | * or relative path in Finder because we use a virtual filesystem in testing which |
||
439 | * doesn't support these methods. |
||
440 | * |
||
441 | * @param string $filename |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | 1 | protected function makeRelative($filename) |
|
454 | |||
455 | /** |
||
456 | * This method gets all headers and all yaml documents and stores them respectively. |
||
457 | * |
||
458 | * @return array a list of sorted yaml documents |
||
459 | */ |
||
460 | 13 | protected function getSortedYamlDocuments() |
|
480 | |||
481 | /** |
||
482 | * This filteres out any yaml documents which don't pass their only |
||
483 | * or except statement tests. |
||
484 | * |
||
485 | * @return array |
||
486 | */ |
||
487 | 13 | protected function filterByOnlyAndExcept() |
|
507 | |||
508 | /** |
||
509 | * Tests the only except rules for a header. |
||
510 | * |
||
511 | * @param array $header |
||
512 | * @param string $flag |
||
513 | * |
||
514 | * @return boolean |
||
515 | */ |
||
516 | 12 | protected function testRules($header, $flag) |
|
541 | |||
542 | /** |
||
543 | * Tests a rule against the given expected result. |
||
544 | * |
||
545 | * @param string $rule |
||
546 | * @param string|array $params |
||
547 | * |
||
548 | * @return boolean |
||
549 | */ |
||
550 | 4 | protected function testSingleRule($rule, $params) |
|
569 | } |
||
570 |