Total Complexity | 74 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Node 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 Node, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | final class Node |
||
12 | { |
||
13 | /** @var int */ |
||
2 ignored issues
–
show
|
|||
14 | public $indent = -1; |
||
15 | /** @var int */ |
||
2 ignored issues
–
show
|
|||
16 | public $line; |
||
17 | /** @var int */ |
||
2 ignored issues
–
show
|
|||
18 | public $type; |
||
19 | /** @var null|string|boolean */ |
||
2 ignored issues
–
show
|
|||
20 | public $identifier; |
||
21 | /** @var Node|NodeList|null|string */ |
||
2 ignored issues
–
show
|
|||
22 | public $value; |
||
23 | |||
24 | /** @var null|Node */ |
||
2 ignored issues
–
show
|
|||
25 | private $parent; |
||
26 | |||
27 | public function __construct($nodeString = null, $line = null) |
||
28 | { |
||
29 | $this->line = $line; |
||
30 | if (is_null($nodeString)) { |
||
31 | $this->type = Y\ROOT; |
||
32 | } else { |
||
33 | $this->parse($nodeString); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Sets the parent of the current Node |
||
39 | * @param Node $node The node |
||
3 ignored issues
–
show
|
|||
40 | * |
||
41 | * @return Node|self The currentNode |
||
1 ignored issue
–
show
|
|||
42 | */ |
||
43 | public function setParent(Node $node):Node |
||
44 | { |
||
45 | $this->parent = $node; |
||
46 | return $this; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Gets the ancestor with specified $indent or the direct $parent OR the current Node itself |
||
51 | * |
||
52 | * @param integer $indent The indent |
||
3 ignored issues
–
show
|
|||
53 | * |
||
54 | * @return Node|self The parent. |
||
1 ignored issue
–
show
|
|||
55 | */ |
||
56 | public function getParent(int $indent = null):Node |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Set the value for the current Node : |
||
68 | * - if value is null , then value = $child (Node) |
||
69 | * - if value is Node, then value is a NodeList with (previous value AND $child) |
||
70 | * - if value is a NodeList, simply push $child into |
||
71 | * |
||
72 | * @param Node $child The child |
||
3 ignored issues
–
show
|
|||
73 | */ |
||
74 | public function add(Node $child):void |
||
75 | { |
||
76 | $child->setParent($this); |
||
77 | $current = $this->value; |
||
78 | if (is_null($current)) { |
||
79 | $this->value = $child; |
||
80 | return; |
||
81 | } else { |
||
82 | if ($current instanceof Node) { |
||
83 | $this->value = new NodeList(); |
||
84 | $this->value->type = Y\LITT_FOLDED; |
||
85 | $this->value->setIteratorMode(NodeList::IT_MODE_KEEP); |
||
86 | $this->value->push($current); |
||
87 | } |
||
88 | $this->value->push($child); |
||
89 | //modify type according to child |
||
90 | switch ($child->type) { |
||
91 | case Y\COMMENT: //fall through |
||
1 ignored issue
–
show
|
|||
92 | case Y\KEY: $this->value->type = Y\MAPPING;break; |
||
2 ignored issues
–
show
|
|||
93 | case Y\ITEM: $this->value->type = Y\SEQUENCE;break; |
||
1 ignored issue
–
show
|
|||
94 | // case Y\COMMENT: $this->value->type = Y\RAW;break; |
||
95 | } |
||
96 | } |
||
97 | // if ($this->type & Y\LITTERALS) $child->type = Y\SCALAR; |
||
98 | } |
||
99 | |||
100 | public function getDeepestNode():Node |
||
101 | { |
||
102 | $cursor = $this; |
||
103 | while ($cursor->value instanceof Node) { |
||
104 | $cursor = $cursor->value; |
||
105 | } |
||
106 | return $cursor; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Parses the string (assumed to be a line from a valid YAML) |
||
111 | * |
||
112 | * @param string $nodeString The node string |
||
3 ignored issues
–
show
|
|||
113 | * |
||
114 | * @return Node|self ( description_of_the_return_value ) |
||
1 ignored issue
–
show
|
|||
115 | */ |
||
116 | public function parse(string $nodeString):Node |
||
117 | { |
||
118 | $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
||
119 | $this->indent = strspn($nodeValue, ' '); |
||
120 | $nodeValue = ltrim($nodeValue); |
||
121 | if ($nodeValue === '') { |
||
122 | $this->type = Y\BLANK; |
||
1 ignored issue
–
show
|
|||
123 | // $this->indent = 0; // remove if no bugs |
||
124 | } elseif (substr($nodeValue, 0, 3) === '...') {//TODO: can have something on same line ? |
||
125 | $this->type = Y\DOC_END; |
||
126 | } elseif (preg_match(R::KEY, $nodeValue, $matches)) { |
||
127 | $this->onKey($matches); |
||
128 | } else {//NOTE: can be of another type according to parent |
||
129 | list($this->type, $value) = $this->define($nodeValue); |
||
130 | is_object($value) ? $this->add($value) : $this->value = $value; |
||
131 | } |
||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Set the type and value according to first character |
||
137 | * |
||
138 | * @param string $nodeValue The node value |
||
3 ignored issues
–
show
|
|||
139 | * @return array contains [node->type, node->value] |
||
1 ignored issue
–
show
|
|||
140 | */ |
||
141 | private function define($nodeValue):array |
||
142 | { |
||
143 | $v = substr($nodeValue, 1); |
||
144 | $first = $nodeValue[0]; |
||
145 | if (in_array($first, ['"', "'"])) { |
||
146 | $type = R::isProperlyQuoted($nodeValue) ? Y\QUOTED : Y\PARTIAL; |
||
147 | return [$type, $nodeValue]; |
||
148 | } |
||
149 | if (in_array($first, ['{', '['])) return $this->onObject($nodeValue); |
||
1 ignored issue
–
show
|
|||
150 | if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
||
1 ignored issue
–
show
|
|||
151 | switch ($first) { |
||
152 | case '#': return [Y\COMMENT, ltrim($v)]; |
||
1 ignored issue
–
show
|
|||
153 | case "-": return $this->onHyphen($nodeValue); |
||
1 ignored issue
–
show
|
|||
154 | case '%': return [Y\DIRECTIVE, ltrim($v)]; |
||
1 ignored issue
–
show
|
|||
155 | case '?': return [Y\SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
||
1 ignored issue
–
show
|
|||
156 | case ':': return [Y\SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
||
1 ignored issue
–
show
|
|||
157 | case '>': return [Y\LITT_FOLDED, null]; |
||
1 ignored issue
–
show
|
|||
158 | case '|': return [Y\LITT, null]; |
||
1 ignored issue
–
show
|
|||
159 | default: |
||
1 ignored issue
–
show
|
|||
160 | return [Y\SCALAR, $nodeValue]; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Process when a "key: value" syntax is found in the parsed string |
||
166 | * Note : key is match 1, value is match 2 as per regex from R::KEY |
||
167 | * @param array $matches The matches provided by 'preg_match' function |
||
3 ignored issues
–
show
|
|||
168 | */ |
||
169 | private function onKey(array $matches):void |
||
170 | { |
||
171 | $this->type = Y\KEY; |
||
1 ignored issue
–
show
|
|||
172 | $this->identifier = trim($matches[1]); |
||
173 | $keyValue = isset($matches[2]) ? trim($matches[2]) : null; |
||
174 | if (!empty($keyValue)) { |
||
175 | $n = new Node($keyValue, $this->line); |
||
176 | $hasComment = strpos($keyValue, ' #'); |
||
177 | if (!is_bool($hasComment)) { |
||
178 | $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
||
179 | if ($tmpNode->type !== Y\PARTIAL) { |
||
180 | $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
||
181 | //TODO: modify "identifier" to specify if fullline comment or not |
||
182 | $this->add($comment); |
||
183 | $n = $tmpNode; |
||
184 | } |
||
185 | } |
||
186 | $n->indent = $this->indent + strlen($this->identifier); |
||
187 | $this->add($n); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Determines the correct type and value when a short object/array syntax is found |
||
193 | * |
||
194 | * @param string $value The value assumed to start with { or ( or characters |
||
3 ignored issues
–
show
|
|||
195 | * |
||
196 | * @return array array with the type and $value (unchanged for now) |
||
1 ignored issue
–
show
|
|||
197 | * @see self:define |
||
1 ignored issue
–
show
|
|||
198 | */ |
||
199 | private function onObject($value):array |
||
1 ignored issue
–
show
|
|||
200 | { |
||
201 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
202 | if (json_last_error() === JSON_ERROR_NONE) return [Y\JSON, $value]; |
||
1 ignored issue
–
show
|
|||
203 | if (preg_match(R::MAPPING, $value)) return [Y\COMPACT_MAPPING, $value]; |
||
1 ignored issue
–
show
|
|||
204 | if (preg_match(R::SEQUENCE, $value)) return [Y\COMPACT_SEQUENCE, $value]; |
||
1 ignored issue
–
show
|
|||
205 | return [Y\PARTIAL, $value]; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Determines type and value when an hyphen "-" is found |
||
210 | * |
||
211 | * @param string $nodeValue The node value |
||
2 ignored issues
–
show
|
|||
212 | * |
||
213 | * @return array array with the type and $value |
||
1 ignored issue
–
show
|
|||
214 | */ |
||
215 | private function onHyphen($nodeValue):array |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Determines the type and value according to $nodeValue when one of these characters is found : !,&,* |
||
236 | * |
||
237 | * @param string $nodeValue The node value |
||
3 ignored issues
–
show
|
|||
238 | * |
||
239 | * @return array array with the type and $value |
||
1 ignored issue
–
show
|
|||
240 | * @see self::define |
||
1 ignored issue
–
show
|
|||
241 | */ |
||
242 | private function onNodeAction($nodeValue):array |
||
243 | { |
||
244 | // TODO: handle tags like <tag:clarkevans.com,2002:invoice> |
||
245 | $v = substr($nodeValue, 1); |
||
246 | $type = ['!' => Y\TAG, '&' => Y\REF_DEF, '*' => Y\REF_CALL][$nodeValue[0]]; |
||
247 | $pos = strpos($v, ' '); |
||
248 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
||
249 | $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
||
250 | return [$type, $n]; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Returns the correct PHP datatype for the value of the current Node |
||
255 | * (simple value Node assumed -> $value is a scalar) |
||
256 | * |
||
257 | * @return mixed The value as PHP type. |
||
1 ignored issue
–
show
|
|||
258 | */ |
||
259 | public function getPhpValue() |
||
260 | { |
||
261 | $v = $this->value; |
||
262 | if (is_null($v)) return null; |
||
1 ignored issue
–
show
|
|||
263 | if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar($v); |
||
1 ignored issue
–
show
|
|||
264 | if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type); |
||
1 ignored issue
–
show
|
|||
265 | switch ($this->type) { |
||
266 | case Y\JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
||
1 ignored issue
–
show
|
|||
267 | case Y\QUOTED: return substr($v, 1, -1); |
||
1 ignored issue
–
show
|
|||
268 | case Y\RAW: return strval($v); |
||
1 ignored issue
–
show
|
|||
269 | default: |
||
1 ignored issue
–
show
|
|||
270 | trigger_error("Error can not get PHP type for ".Y\getName($this->type), E_USER_WARNING); |
||
271 | return null; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Returns the correct PHP type according to the string value |
||
277 | * |
||
278 | * @param string $v a string value |
||
3 ignored issues
–
show
|
|||
279 | * |
||
280 | * @return mixed The scalar value with appropriate PHP type |
||
1 ignored issue
–
show
|
|||
281 | */ |
||
282 | private static function getScalar(string $v) |
||
283 | { |
||
284 | $types = ['yes' => true, |
||
285 | 'no' => false, |
||
286 | 'true' => true, |
||
287 | 'false' => false, |
||
288 | 'null' => null, |
||
289 | '.inf' => INF, |
||
290 | '-.inf' => -INF, |
||
291 | '.nan' => NAN |
||
292 | ]; |
||
293 | if (isset($types[strtolower($v)])) return $types[strtolower($v)]; |
||
1 ignored issue
–
show
|
|||
294 | if (R::isDate($v)) return date_create($v); |
||
1 ignored issue
–
show
|
|||
295 | if (R::isNumber($v)) return self::getNumber($v); |
||
1 ignored issue
–
show
|
|||
296 | return strval($v); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Returns the correct PHP type according to the string value |
||
301 | * |
||
302 | * @param string $v a string value |
||
3 ignored issues
–
show
|
|||
303 | * |
||
304 | * @return int|float The scalar value with appropriate PHP type |
||
1 ignored issue
–
show
|
|||
305 | */ |
||
306 | private static function getNumber(string $v) |
||
314 | } |
||
315 | |||
316 | private static function getCompact(string $mappingOrSeqString, int $type):object |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * PHP internal function for debugging purpose : simplify output provided by 'var_dump' |
||
338 | * |
||
339 | * @return array the Node properties and respective values displayed by 'var_dump' |
||
1 ignored issue
–
show
|
|||
340 | */ |
||
341 | public function __debugInfo():array |
||
342 | { |
||
349 |