Total Complexity | 69 |
Total Lines | 215 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 |
||
10 | final class Builder |
||
11 | { |
||
12 | private static $_root; |
||
13 | private static $_debug; |
||
14 | |||
15 | const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
||
16 | const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence"; |
||
17 | |||
18 | |||
19 | private static function build(object $node, &$parent = null) |
||
23 | } |
||
24 | |||
25 | private static function buildNodeList(NodeList $node, &$parent) |
||
26 | { |
||
27 | $type = property_exists($node, "type") ? $node->type : null; |
||
28 | if ($type & (Y\RAW | Y\LITTERALS)) { |
||
29 | return self::litteral($node, $type); |
||
30 | } |
||
31 | $p = $parent; |
||
32 | switch ($type) { |
||
33 | case Y\MAPPING: //fall through |
||
1 ignored issue
–
show
|
|||
34 | case Y\SET: $p = new \StdClass;break; |
||
1 ignored issue
–
show
|
|||
35 | case Y\SEQUENCE: $p = [];break; |
||
1 ignored issue
–
show
|
|||
36 | // case Y\KEY: $p = $parent;break; |
||
37 | } |
||
38 | $out = null; |
||
39 | foreach ($node as $child) { |
||
40 | $result = self::build($child, $p); |
||
41 | if (!is_null($result)) { |
||
42 | if (is_string($result)) { |
||
43 | $out .= $result.' '; |
||
44 | } else { |
||
45 | return $result; |
||
46 | } |
||
47 | } |
||
48 | } |
||
49 | return is_null($out) ? $p : rtrim($out); |
||
50 | } |
||
51 | |||
52 | private static function buildNode(Node $node, &$parent) |
||
53 | { |
||
54 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
||
55 | switch ($type) { |
||
56 | case Y\COMMENT: self::$_root->addComment($line, $value);return; |
||
1 ignored issue
–
show
|
|||
57 | case Y\DIRECTIVE: return;//TODO |
||
1 ignored issue
–
show
|
|||
58 | case Y\ITEM: self::buildItem($value, $parent);return; |
||
1 ignored issue
–
show
|
|||
59 | case Y\KEY: self::buildKey($node, $parent);return; |
||
2 ignored issues
–
show
|
|||
60 | case Y\REF_DEF: //fall through |
||
1 ignored issue
–
show
|
|||
61 | case Y\REF_CALL://TODO: self::build returns what ? |
||
1 ignored issue
–
show
|
|||
62 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
63 | if ($type === Y\REF_DEF) self::$_root->addReference($identifier, $tmp); |
||
1 ignored issue
–
show
|
|||
64 | return self::$_root->getReference($identifier); |
||
65 | case Y\SET_KEY: |
||
1 ignored issue
–
show
|
|||
66 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
67 | if (empty($key)) |
||
1 ignored issue
–
show
|
|||
68 | throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
||
69 | $parent->{$key} = null; |
||
70 | return; |
||
71 | case Y\SET_VALUE: |
||
1 ignored issue
–
show
|
|||
72 | $prop = array_keys(get_object_vars($parent)); |
||
73 | $key = end($prop); |
||
74 | if (property_exists($value, "type") && ($value->type & (Y\ITEM | Y\MAPPING))) { |
||
1 ignored issue
–
show
|
|||
75 | $p = $value->type === Y\ITEM ? [] : new \StdClass; |
||
76 | self::build($value, $p); |
||
77 | } else { |
||
1 ignored issue
–
show
|
|||
78 | $p = self::build($value, $parent->{$key}); |
||
79 | } |
||
1 ignored issue
–
show
|
|||
80 | $parent->{$key} = $p; |
||
81 | return; |
||
82 | case Y\TAG: |
||
1 ignored issue
–
show
|
|||
83 | if ($parent === self::$_root) { |
||
1 ignored issue
–
show
|
|||
84 | $parent->addTag($identifier);return; |
||
85 | } else {//TODO: have somewhere a list of common tags and their treatment |
||
1 ignored issue
–
show
|
|||
86 | if (in_array($identifier, ['!binary', '!str'])) { |
||
1 ignored issue
–
show
|
|||
87 | if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
||
1 ignored issue
–
show
|
|||
88 | else $value->type = Y\RAW; |
||
2 ignored issues
–
show
|
|||
89 | } |
||
1 ignored issue
–
show
|
|||
90 | $val = is_null($value) ? null : self::build($value, $node); |
||
91 | return new Tag($identifier, $val); |
||
92 | } |
||
1 ignored issue
–
show
|
|||
93 | default: |
||
1 ignored issue
–
show
|
|||
94 | return is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Builds a key and set the property + value to the parent given |
||
100 | * |
||
101 | * @param Node $node The node |
||
1 ignored issue
–
show
|
|||
102 | * @param object|array $parent The parent |
||
1 ignored issue
–
show
|
|||
103 | * |
||
104 | * @throws \ParseError if Key has no name(identifier) |
||
105 | */ |
||
106 | private static function buildKey($node, &$parent):void |
||
107 | { |
||
108 | list($identifier, $value) = [$node->identifier, $node->value]; |
||
109 | if (is_null($identifier)) { |
||
110 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
||
111 | } else { |
||
112 | if ($value instanceof Node && ($value->type & (Y\KEY | Y\ITEM))) { |
||
1 ignored issue
–
show
|
|||
113 | $parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : []; |
||
114 | self::build($value, $parent->{$identifier}); |
||
115 | } elseif (is_object($value)) { |
||
116 | $parent->{$identifier} = self::build($value, $parent->{$identifier}); |
||
117 | } else { |
||
118 | $parent->{$identifier} = $node->getPhpValue(); |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | |||
123 | private static function buildItem($value, &$parent):void |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Builds a file. check multiple documents & split if more than one documents |
||
138 | * |
||
139 | * @param Node $_root The root node |
||
1 ignored issue
–
show
|
|||
140 | * @param int $debug the level of debugging requested |
||
2 ignored issues
–
show
|
|||
141 | * |
||
142 | * @return array|YamlObject list of documents or juste one. |
||
143 | */ |
||
144 | public static function buildContent(Node $_root, int $_debug) |
||
145 | { |
||
146 | self::$_debug = $_debug; |
||
147 | $totalDocStart = 0; |
||
148 | $documents = []; |
||
149 | if ($_root->value instanceof Node) { |
||
150 | $q = new NodeList; |
||
151 | $q->push($_root->value); |
||
152 | return [self::buildDocument($q, 0)]; |
||
153 | } |
||
154 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
||
155 | foreach ($_root->value as $child) { |
||
156 | if ($child->type & Y\DOC_START) $totalDocStart++; |
||
1 ignored issue
–
show
|
|||
157 | //if 0 or 1 DOC_START = we are still in first document |
||
158 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
||
159 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
||
1 ignored issue
–
show
|
|||
160 | $documents[$currentDoc]->push($child); |
||
161 | } |
||
162 | // $_debug >= 2 && var_dump($documents);//var_dump($documents);die("documents"); |
||
163 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
||
164 | return count($content) === 1 ? $content[0] : $content; |
||
165 | } |
||
166 | |||
167 | private static function buildDocument(NodeList $list, int $key):YamlObject |
||
168 | { |
||
169 | self::$_root = new YamlObject(); |
||
170 | $childTypes = self::getChildrenTypes($list); |
||
171 | $isMapping = count(array_intersect($childTypes, [Y\KEY, Y\MAPPING])) > 0; |
||
1 ignored issue
–
show
|
|||
172 | $isSequence = in_array(Y\ITEM, $childTypes); |
||
173 | $isSet = in_array(Y\SET_VALUE, $childTypes); |
||
174 | if ($isMapping && $isSequence) { |
||
175 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
||
176 | } else { |
||
177 | switch (true) { |
||
178 | case $isSequence: $list->type = Y\SEQUENCE;break; |
||
1 ignored issue
–
show
|
|||
179 | case $isSet: $list->type = Y\SET;break; |
||
1 ignored issue
–
show
|
|||
180 | case $isMapping://fall through |
||
1 ignored issue
–
show
|
|||
181 | default: $list->type = Y\MAPPING; |
||
1 ignored issue
–
show
|
|||
182 | } |
||
183 | } |
||
184 | // self::$_debug >= 3 && var_dump(self::$_root, $list); |
||
185 | $string = ''; |
||
186 | foreach ($list as $child) { |
||
187 | $result = self::build($child, self::$_root); |
||
188 | if (is_string($result)) { |
||
189 | $string .= $result.' '; |
||
190 | } |
||
191 | } |
||
192 | if (!empty($string)) { |
||
193 | self::$_root->setText(rtrim($string)); |
||
194 | } |
||
195 | return self::$_root; |
||
196 | } |
||
197 | |||
198 | private static function litteral(NodeList $children, $type):string |
||
216 | } |
||
217 | |||
218 | private static function getChildrenTypes(NodeList $children):array |
||
219 | { |
||
220 | $types = []; |
||
225 | } |
||
226 | } |
||
227 |