Total Complexity | 75 |
Total Lines | 252 |
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 |
||
7 | class Node |
||
8 | { |
||
9 | /** @var int */ |
||
2 ignored issues
–
show
|
|||
10 | public $indent = -1; |
||
11 | /** @var int */ |
||
2 ignored issues
–
show
|
|||
12 | public $line; |
||
13 | /** @var int */ |
||
2 ignored issues
–
show
|
|||
14 | public $type; |
||
15 | /** @var null|string|boolean */ |
||
2 ignored issues
–
show
|
|||
16 | public $identifier; |
||
17 | /** @var Node|DLL|null|string */ |
||
2 ignored issues
–
show
|
|||
18 | public $value; |
||
19 | |||
20 | /** @var null|Node */ |
||
2 ignored issues
–
show
|
|||
21 | private $parent; |
||
22 | |||
23 | public function __construct($nodeString = null, $line = null) |
||
24 | { |
||
25 | $this->line = $line; |
||
26 | if (is_null($nodeString)) { |
||
27 | $this->type = T::ROOT; |
||
28 | } else { |
||
29 | $this->parse($nodeString); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | public function setParent(Node $node):Node |
||
34 | { |
||
35 | $this->parent = $node; |
||
36 | return $this; |
||
37 | } |
||
38 | |||
39 | public function getParent($indent = null):Node |
||
47 | } |
||
48 | |||
49 | public function add(Node $child):void |
||
50 | { |
||
51 | $child->setParent($this); |
||
52 | $current = $this->value; |
||
53 | if (in_array($this->type, T::$LITTERALS)) $child->type = T::SCALAR; |
||
1 ignored issue
–
show
|
|||
54 | if (is_null($current)) { |
||
55 | $this->value = $child; |
||
56 | return; |
||
57 | } elseif ($current instanceof Node) { |
||
58 | $this->value = new DLL(); |
||
59 | $this->value->setIteratorMode(DLL::IT_MODE_KEEP); |
||
60 | $this->value->push($current); |
||
61 | } |
||
62 | $this->value->push($child); |
||
63 | //modify type according to child |
||
64 | if ($this->value instanceof DLL && !property_exists($this->value, "type")) { |
||
65 | switch ($child->type) { |
||
66 | case T::KEY: $this->value->type = T::MAPPING;break; |
||
1 ignored issue
–
show
|
|||
67 | case T::ITEM: $this->value->type = T::SEQUENCE;break; |
||
1 ignored issue
–
show
|
|||
68 | case T::SCALAR: $this->value->type = $this->type;break; |
||
1 ignored issue
–
show
|
|||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | public function getDeepestNode():Node |
||
74 | { |
||
75 | $cursor = $this; |
||
76 | while ($cursor->value instanceof Node) { |
||
77 | $cursor = $cursor->value; |
||
78 | } |
||
79 | return $cursor; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * CAUTION : the types assumed here are NOT FINAL : they CAN be adjusted according to parent |
||
84 | */ |
||
85 | public function parse(String $nodeString):Node |
||
86 | { |
||
87 | $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement |
||
88 | $this->indent = strspn($nodeValue, ' '); |
||
89 | $nodeValue = ltrim($nodeValue); |
||
90 | if ($nodeValue === '') { |
||
91 | $this->type = T::EMPTY; |
||
92 | // $this->indent = 0; // remove if no bugs |
||
93 | } elseif (substr($nodeValue, 0, 3) === '...') {//TODO: can have something on same line ? |
||
94 | $this->type = T::DOC_END; |
||
95 | } elseif (preg_match(R::KEY, $nodeValue, $matches)) { |
||
96 | $this->onKey($matches); |
||
97 | } else {//NOTE: can be of another type according to parent |
||
98 | list($this->type, $value) = $this->define($nodeValue); |
||
99 | is_object($value) ? $this->add($value) : $this->value = $value; |
||
100 | } |
||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Set the type and value according to first character |
||
106 | * |
||
107 | * @param string $nodeValue The node value |
||
3 ignored issues
–
show
|
|||
108 | * @return array contains [node->type, node->value] |
||
1 ignored issue
–
show
|
|||
109 | */ |
||
110 | private function define($nodeValue):array |
||
111 | { |
||
112 | $v = substr($nodeValue, 1); |
||
113 | if (in_array($nodeValue[0], ['"', "'"])) { |
||
114 | $type = R::isProperlyQuoted($nodeValue) ? T::QUOTED : T::PARTIAL; |
||
115 | return [$type, $nodeValue]; |
||
116 | } |
||
117 | if (in_array($nodeValue[0], ['{', '['])) return $this->onObject($nodeValue); |
||
1 ignored issue
–
show
|
|||
118 | if (in_array($nodeValue[0], ['!', '&', '*'])) return $this->onNodeAction($nodeValue); |
||
1 ignored issue
–
show
|
|||
119 | switch ($nodeValue[0]) { |
||
120 | case '#': return [T::COMMENT, ltrim($v)]; |
||
1 ignored issue
–
show
|
|||
121 | case "-": return $this->onHyphen($nodeValue); |
||
1 ignored issue
–
show
|
|||
122 | case '%': return [T::DIRECTIVE, ltrim($v)]; |
||
1 ignored issue
–
show
|
|||
123 | case '?': return [T::SET_KEY, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
||
1 ignored issue
–
show
|
|||
124 | case ':': return [T::SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)]; |
||
1 ignored issue
–
show
|
|||
125 | case '>': return [T::LITTERAL_FOLDED, null]; |
||
1 ignored issue
–
show
|
|||
126 | case '|': return [T::LITTERAL, null]; |
||
1 ignored issue
–
show
|
|||
127 | default: |
||
1 ignored issue
–
show
|
|||
128 | return [T::SCALAR, $nodeValue]; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | private function onKey($matches):void |
||
133 | { |
||
134 | $this->type = T::KEY; |
||
135 | $this->identifier = trim($matches[1]); |
||
136 | $keyValue = isset($matches[2]) ? trim($matches[2]) : null; |
||
137 | if (!empty($keyValue)) { |
||
138 | $n = new Node($keyValue, $this->line); |
||
139 | $hasComment = strpos($keyValue, ' #'); |
||
140 | if (!is_bool($hasComment)) { |
||
141 | $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line); |
||
142 | if ($tmpNode->type !== T::PARTIAL) { |
||
143 | $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line); |
||
144 | //TODO: modify "identifier" to specify if fullline comment or not |
||
145 | $this->add($comment); |
||
146 | $n = $tmpNode; |
||
147 | } |
||
148 | } |
||
149 | $n->indent = $this->indent + strlen($this->identifier); |
||
150 | $this->add($n); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | private function onObject($value):array |
||
1 ignored issue
–
show
|
|||
155 | { |
||
156 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
157 | if (json_last_error() === JSON_ERROR_NONE) return [T::JSON, $value]; |
||
1 ignored issue
–
show
|
|||
158 | if (preg_match(R::MAPPING, $value)) return [T::MAPPING_SHORT, $value]; |
||
1 ignored issue
–
show
|
|||
159 | if (preg_match(R::SEQUENCE, $value)) return [T::SEQUENCE_SHORT, $value]; |
||
1 ignored issue
–
show
|
|||
160 | return [T::PARTIAL, $value]; |
||
161 | } |
||
162 | |||
163 | private function onHyphen($nodeValue):array |
||
180 | } |
||
181 | |||
182 | private function onNodeAction($nodeValue):array |
||
183 | { |
||
184 | // TODO: handle tags like <tag:clarkevans.com,2002:invoice> |
||
185 | $v = substr($nodeValue, 1); |
||
186 | $type = ['!' => T::TAG, '&' => T::REF_DEF, '*' => T::REF_CALL][$nodeValue[0]]; |
||
187 | $pos = strpos($v, ' '); |
||
188 | $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true); |
||
189 | $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this); |
||
190 | return [$type, $n]; |
||
191 | } |
||
192 | |||
193 | public function getPhpValue() |
||
194 | { |
||
195 | $v = $this->value; |
||
196 | if (is_null($v)) return null; |
||
1 ignored issue
–
show
|
|||
197 | switch ($this->type) { |
||
198 | case T::JSON: return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR); |
||
1 ignored issue
–
show
|
|||
199 | case T::QUOTED: return substr($v, 1, -1); |
||
1 ignored issue
–
show
|
|||
200 | case T::RAW: return strval($v); |
||
1 ignored issue
–
show
|
|||
201 | case T::REF_CALL://fall through |
||
1 ignored issue
–
show
|
|||
202 | case T::SCALAR: return self::getScalar($v); |
||
1 ignored issue
–
show
|
|||
203 | case T::MAPPING_SHORT: return self::getShortMapping(substr($v, 1, -1)); |
||
1 ignored issue
–
show
|
|||
204 | //TODO : that's not robust enough, improve it |
||
205 | case T::SEQUENCE_SHORT: |
||
1 ignored issue
–
show
|
|||
206 | $f = function ($e) { return self::getScalar(trim($e));}; |
||
207 | return array_map($f, explode(",", substr($v, 1, -1))); |
||
208 | default: |
||
1 ignored issue
–
show
|
|||
209 | trigger_error("Error can not get PHP type for ".T::getName($this->type), E_USER_WARNING); |
||
210 | return null; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | private static function getScalar($v) |
||
215 | { |
||
216 | $types = ['yes' => true, |
||
217 | 'no' => false, |
||
218 | 'true' => true, |
||
219 | 'false' => false, |
||
220 | 'null' => null, |
||
221 | '.inf' => INF, |
||
222 | '-.inf' => -INF, |
||
223 | '.nan' => NAN |
||
224 | ]; |
||
225 | if (in_array(strtolower($v), array_keys($types))) return $types[strtolower($v)]; |
||
1 ignored issue
–
show
|
|||
226 | if (R::isDate($v)) return date_create($v); |
||
1 ignored issue
–
show
|
|||
227 | if (R::isNumber($v)) return self::getNumber($v); |
||
1 ignored issue
–
show
|
|||
228 | return strval($v); |
||
229 | } |
||
230 | |||
231 | private static function getNumber($v) |
||
238 | } |
||
239 | |||
240 | //TODO : that's not robust enough, improve it |
||
241 | private static function getShortMapping($mappingString):object |
||
242 | { |
||
243 | $out = new \StdClass(); |
||
244 | foreach (explode(',', $mappingString) as $value) { |
||
245 | list($keyName, $keyValue) = explode(':', $value); |
||
246 | $out->{trim($keyName)} = self::getScalar(trim($keyValue)); |
||
247 | } |
||
248 | return $out; |
||
249 | } |
||
250 | |||
251 | public function __debugInfo():array |
||
259 | } |
||
260 | } |
||
261 |