Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | final class Builder |
||
15 | { |
||
16 | private static $_root; |
||
|
|||
17 | private static $_debug; |
||
18 | |||
19 | const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
||
20 | const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence"; |
||
21 | |||
22 | /** |
||
23 | * Builds a file. check multiple documents & split if more than one documents |
||
24 | * |
||
25 | * @param Node $_root The root node : Node with Node->type === YAML::ROOT |
||
26 | * @param int $_debug the level of debugging requested |
||
27 | * |
||
28 | * @return array|YamlObject list of documents or just one. |
||
29 | * @todo implement splitting on YAML::DOC_END also |
||
30 | */ |
||
31 | public static function buildContent(Node $_root, int $_debug) |
||
62 | |||
63 | /** |
||
64 | * Generic function to distinguish between Node and NodeList |
||
65 | * |
||
66 | * @param Node|NodeList $node The node. |
||
67 | * @param mixed $parent The parent |
||
68 | * |
||
69 | * @return mixed ( description_of_the_return_value ) |
||
70 | */ |
||
71 | private static function build(object $node, &$parent = null) |
||
76 | |||
77 | /** |
||
78 | * Builds a node list. |
||
79 | * |
||
80 | * @param NodeList $node The node |
||
81 | * @param mixed $parent The parent |
||
82 | * |
||
83 | * @return mixed The parent (object|array) or a string representing the NodeList. |
||
84 | */ |
||
85 | private static function buildNodeList(NodeList $node, &$parent=null) |
||
118 | |||
119 | /** |
||
120 | * Builds a node. |
||
121 | * |
||
122 | * @param Node $node The node of any Node->type |
||
123 | * @param mixed $parent The parent |
||
124 | * |
||
125 | * @return mixed The node value as scalar, array or object or null to otherwise. |
||
126 | */ |
||
127 | private static function buildNode(Node $node, &$parent) |
||
155 | |||
156 | /** |
||
157 | * Builds a key and set the property + value to the given parent |
||
158 | * |
||
159 | * @param Node $node The node with type YAML::KEY |
||
160 | * @param object|array $parent The parent |
||
161 | * |
||
162 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
||
163 | * @return null |
||
164 | */ |
||
165 | private static function buildKey(Node $node, &$parent=null) |
||
200 | |||
201 | /** |
||
202 | * Builds an item. Adds the item value to the parent array|Iterator |
||
203 | * |
||
204 | * @param Node $node The node with type YAML::ITEM |
||
205 | * @param array|\Iterator $parent The parent |
||
206 | * |
||
207 | * @throws \Exception if parent is another type than array or object Iterator |
||
208 | * @return null |
||
209 | */ |
||
210 | private static function buildItem(Node $node, &$parent) |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Builds a litteral (folded or not) or any NodeList that has YAML::RAW type (like a multiline value) |
||
240 | * |
||
241 | * @param NodeList $children The children |
||
242 | * @param integer $type The type |
||
243 | * |
||
244 | * @return string The litteral. |
||
245 | * @todo : Example 6.1. Indentation Spaces spaces must be considered as content |
||
246 | */ |
||
247 | private static function buildLitteral(NodeList $list, int $type):string |
||
276 | |||
277 | /** |
||
278 | * Builds a set key. |
||
279 | * |
||
280 | * @param Node $node The node of type YAML::SET_KEY. |
||
281 | * @param object $parent The parent |
||
282 | * |
||
283 | * @throws \Exception if a problem occurs during serialisation (json format) of the key |
||
284 | */ |
||
285 | private function buildSetKey(Node $node, &$parent) |
||
293 | |||
294 | /** |
||
295 | * Builds a set value. |
||
296 | * |
||
297 | * @param Node $node The node of type YAML::SET_VALUE |
||
298 | * @param object $parent The parent (the document object or any previous object created through a mapping key) |
||
299 | */ |
||
300 | private function buildSetValue(Node $node, &$parent) |
||
312 | |||
313 | /** |
||
314 | * Builds a tag and its value (also built) and encapsulates them in a Tag object. |
||
315 | * |
||
316 | * @param Node $node The node of type YAML::TAG |
||
317 | * @param mixed $parent The parent |
||
318 | * |
||
319 | * @return Tag|null The tag object of class Dallgoot\Yaml\Tag. |
||
320 | */ |
||
321 | private static function buildTag(Node $node, &$parent) |
||
346 | |||
347 | /** |
||
348 | * Builds a directive. NOT IMPLEMENTED YET |
||
349 | * |
||
350 | * @param Node $node The node |
||
351 | * @param mixed $parent The parent |
||
352 | * @todo implement if requested |
||
353 | */ |
||
354 | private function buildDirective(Node $node, $parent) |
||
358 | } |
||
359 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.