| Total Complexity | 58 | 
| Total Lines | 322 | 
| 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  | 
            ||
| 13 | final class Node  | 
            ||
| 14 | { | 
            ||
| 15 | /** @var int */  | 
            ||
| 16 | public $indent = -1;  | 
            ||
| 17 | /** @var int */  | 
            ||
| 18 | public $line;  | 
            ||
| 19 | /** @var int */  | 
            ||
| 20 | public $type;  | 
            ||
| 21 | /** @var null|string|boolean */  | 
            ||
| 22 | public $identifier;  | 
            ||
| 23 | /** @var Node|NodeList|null|string */  | 
            ||
| 24 | public $value = null;  | 
            ||
| 25 | |||
| 26 | /** @var null|Node */  | 
            ||
| 27 | private $parent;  | 
            ||
| 28 | |||
| 29 | /**  | 
            ||
| 30 | * Create the Node object and parses $nodeString IF not null (else assume a root type Node)  | 
            ||
| 31 | *  | 
            ||
| 32 | * @param string|null $nodeString The node string  | 
            ||
| 33 | * @param int|null $line The line  | 
            ||
| 34 | */  | 
            ||
| 35 | public function __construct($nodeString = null, $line = 0)  | 
            ||
| 36 |     { | 
            ||
| 37 | $this->line = (int) $line;  | 
            ||
| 38 |         if (is_null($nodeString)) { | 
            ||
| 39 | $this->type = Y::ROOT;  | 
            ||
| 40 |         } else { | 
            ||
| 41 | $this->parse($nodeString);  | 
            ||
| 42 | }  | 
            ||
| 43 | }  | 
            ||
| 44 | |||
| 45 | /**  | 
            ||
| 46 | * Sets the parent of the current Node  | 
            ||
| 47 | *  | 
            ||
| 48 | * @param Node $node The node  | 
            ||
| 49 | *  | 
            ||
| 50 | * @return Node|self The currentNode  | 
            ||
| 51 | */  | 
            ||
| 52 | public function setParent(Node $node):Node  | 
            ||
| 53 |     { | 
            ||
| 54 | $this->parent = $node;  | 
            ||
| 55 | return $this;  | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Gets the ancestor with specified $indent or the direct $parent OR the current Node itself  | 
            ||
| 60 | *  | 
            ||
| 61 | * @param int|null $indent The indent  | 
            ||
| 62 | *  | 
            ||
| 63 | * @return Node|self The parent.  | 
            ||
| 64 | */  | 
            ||
| 65 | public function getParent(int $indent = null, $type = 0):Node  | 
            ||
| 66 |     { | 
            ||
| 67 |         if ($this->type === Y::ROOT) { | 
            ||
| 68 | return $this;  | 
            ||
| 69 | }  | 
            ||
| 70 | if (!is_int($indent)) return $this->parent ?? $this;  | 
            ||
| 71 | $cursor = $this;  | 
            ||
| 72 |         while ($cursor instanceof Node && $cursor->indent >= $indent) { | 
            ||
| 73 |             if ($cursor->indent === $indent && $cursor->type !== $type) { | 
            ||
| 74 | $cursor = $cursor->parent;  | 
            ||
| 75 | break;  | 
            ||
| 76 | }  | 
            ||
| 77 | $cursor = $cursor->parent;  | 
            ||
| 78 | }  | 
            ||
| 79 | return $cursor;  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * Set the value for the current Node :  | 
            ||
| 84 | * - if value is null , then value = $child (Node)  | 
            ||
| 85 | * - if value is Node, then value is a NodeList with (previous value AND $child)  | 
            ||
| 86 | * - if value is a NodeList, simply push $child into  | 
            ||
| 87 | *  | 
            ||
| 88 | * @param Node $child The child  | 
            ||
| 89 | */  | 
            ||
| 90 | public function add(Node $child)  | 
            ||
| 91 |     { | 
            ||
| 92 |         if ($this->type & (Y::SCALAR|Y::QUOTED)) { | 
            ||
| 93 | $this->getParent()->add($child);  | 
            ||
| 94 | return;  | 
            ||
| 95 | }  | 
            ||
| 96 | $child->setParent($this);  | 
            ||
| 97 | $current = $this->value;  | 
            ||
| 98 |         if (is_null($current)) { | 
            ||
| 99 | $this->value = $child;  | 
            ||
| 100 |         } else { | 
            ||
| 101 |             if ($current instanceof Node) { | 
            ||
| 102 | $this->value = new NodeList();  | 
            ||
| 103 |                 if ($current->type & Y::LITTERALS) { | 
            ||
| 104 | $this->value->type = $current->type;  | 
            ||
| 105 |                 } else { | 
            ||
| 106 | $this->value->push($current);  | 
            ||
| 107 | }  | 
            ||
| 108 | //modify type according to child  | 
            ||
| 109 | if ($current->type & Y::SET_KEY) $this->value->type = Y::SET;  | 
            ||
| 110 | if ($current->type & Y::KEY) $this->value->type = Y::MAPPING;  | 
            ||
| 111 | if ($current->type & Y::ITEM) $this->value->type = Y::SEQUENCE;  | 
            ||
| 112 | }  | 
            ||
| 113 | $this->value->push($child);  | 
            ||
| 114 | |||
| 115 | if ($this->type & Y::LITTERALS) $this->value->type = $this->type;  | 
            ||
| 116 | }  | 
            ||
| 117 | }  | 
            ||
| 118 | |||
| 119 | /**  | 
            ||
| 120 | * Gets the deepest node.  | 
            ||
| 121 | *  | 
            ||
| 122 | * @return Node|self The deepest node.  | 
            ||
| 123 | */  | 
            ||
| 124 | public function getDeepestNode():Node  | 
            ||
| 125 |     { | 
            ||
| 126 | $cursor = $this;  | 
            ||
| 127 |         while ($cursor->value instanceof Node || $cursor->value instanceof NodeList) { | 
            ||
| 128 |             if ($cursor->value instanceof NodeList) { | 
            ||
| 129 |                 if ($cursor->value->count() === 1) { | 
            ||
| 130 | $cursor = $cursor->value->OffsetGet(0);  | 
            ||
| 131 |                 } else { | 
            ||
| 132 | $cursor = $cursor;  | 
            ||
| 133 | break;  | 
            ||
| 134 | }  | 
            ||
| 135 |             } else { | 
            ||
| 136 | $cursor = $cursor->value;  | 
            ||
| 137 | }  | 
            ||
| 138 | }  | 
            ||
| 139 | return $cursor;  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * Parses the string (assumed to be a line from a valid YAML)  | 
            ||
| 144 | *  | 
            ||
| 145 | * @param string $nodeString The node string  | 
            ||
| 146 | *  | 
            ||
| 147 | * @return Node|self  | 
            ||
| 148 | */  | 
            ||
| 149 | public function parse(string $nodeString):Node  | 
            ||
| 150 |     { | 
            ||
| 151 |         $nodeValue = preg_replace("/^\t+/m", " ", $nodeString); //permissive to tabs but replacement | 
            ||
| 152 | $this->indent = strspn($nodeValue, ' ');  | 
            ||
| 153 | $nodeValue = ltrim($nodeValue);  | 
            ||
| 154 |         if ($nodeValue === '') { | 
            ||
| 155 | $this->type = Y::BLANK;  | 
            ||
| 156 |         } elseif (substr($nodeValue, 0, 3) === '...') {//TODO: can have something on same line ? | 
            ||
| 157 | $this->type = Y::DOC_END;  | 
            ||
| 158 |         } elseif (preg_match(R::KEY, $nodeValue, $matches)) { | 
            ||
| 159 | $this->onKey($matches);  | 
            ||
| 160 |         } else { | 
            ||
| 161 | $this->identify($nodeValue);  | 
            ||
| 162 | }  | 
            ||
| 163 | return $this;  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Set the type and value according to first character  | 
            ||
| 168 | *  | 
            ||
| 169 | * @param string $nodeValue The node value  | 
            ||
| 170 | */  | 
            ||
| 171 | private function identify($nodeValue)  | 
            ||
| 172 |     { | 
            ||
| 173 | $v = substr($nodeValue, 1);  | 
            ||
| 174 | $first = $nodeValue[0];  | 
            ||
| 175 |         if (in_array($first, ['"', "'"])) { | 
            ||
| 176 | $this->type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL;  | 
            ||
| 177 | $this->value = $nodeValue;  | 
            ||
| 178 | return;  | 
            ||
| 179 | }  | 
            ||
| 180 |         if (in_array($first, ['{', '['])) { | 
            ||
| 181 | $this->onCompact($nodeValue);  | 
            ||
| 182 | return;  | 
            ||
| 183 | }  | 
            ||
| 184 |         if (in_array($first, ['!', '&', '*'])) { | 
            ||
| 185 | $this->onNodeAction($nodeValue);  | 
            ||
| 186 | return;  | 
            ||
| 187 | }  | 
            ||
| 188 | // Note : php don't like '?' as an array key -_-'  | 
            ||
| 189 |         if(in_array($first, ['?', ':'])) { | 
            ||
| 190 | $this->type = $first === '?' ? Y::SET_KEY : Y::SET_VALUE;  | 
            ||
| 191 |             if (!empty(trim($v))) { | 
            ||
| 192 | $this->value = new NodeList;  | 
            ||
| 193 | $this->add((new Node(ltrim($v), $this->line))->setParent($this));  | 
            ||
| 194 | }  | 
            ||
| 195 | return;  | 
            ||
| 196 | }  | 
            ||
| 197 |         if ($first === "-") { | 
            ||
| 198 | $this->onHyphen($nodeValue);  | 
            ||
| 199 | return;  | 
            ||
| 200 | }  | 
            ||
| 201 | $characters = [ '#' => [Y::COMMENT, ltrim($v)],  | 
            ||
| 202 | '%' => [Y::DIRECTIVE, ltrim($v)],  | 
            ||
| 203 | '>' => [Y::LITT_FOLDED, null],  | 
            ||
| 204 | '|' => [Y::LITT, null]  | 
            ||
| 205 | ];  | 
            ||
| 206 |         if (isset($characters[$first])) { | 
            ||
| 207 | $this->type = $characters[$first][0];  | 
            ||
| 208 | $this->value = $characters[$first][1];  | 
            ||
| 209 |         } else { | 
            ||
| 210 | $this->type = Y::SCALAR;  | 
            ||
| 211 | $this->value = $nodeValue;  | 
            ||
| 212 | }  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | /**  | 
            ||
| 216 | * Process when a "key: value" syntax is found in the parsed string  | 
            ||
| 217 | * Note : key is match 1, value is match 2 as per regex from R::KEY  | 
            ||
| 218 | *  | 
            ||
| 219 | * @param array $matches The matches provided by 'preg_match' function in Node::parse  | 
            ||
| 220 | */  | 
            ||
| 221 | private function onKey(array $matches)  | 
            ||
| 222 |     { | 
            ||
| 223 | $this->type = Y::KEY;  | 
            ||
| 224 | $this->identifier = trim($matches[1], '"\' ');  | 
            ||
| 225 | $value = $matches[2] ? trim($matches[2]) : null;  | 
            ||
| 226 |         if (!empty($value)) { | 
            ||
| 227 | $hasComment = strpos($value, ' #');  | 
            ||
| 228 |             if (!is_int($hasComment)) { | 
            ||
| 229 | $n = new Node($value, $this->line);  | 
            ||
| 230 |             } else { | 
            ||
| 231 | $n = new Node(trim(substr($value, 0, $hasComment)), $this->line);  | 
            ||
| 232 |                 if ($tmpNode->type !== Y::PARTIAL) { | 
            ||
| 233 | $comment = new Node(trim(substr($value, $hasComment + 1)), $this->line);  | 
            ||
| 234 | $comment->identifier = true; //to specify it is NOT a fullline comment  | 
            ||
| 235 | $this->add($comment->setParent($this));  | 
            ||
| 236 | }  | 
            ||
| 237 | }  | 
            ||
| 238 | $n->indent = $this->indent + strlen($this->identifier);  | 
            ||
| 239 | $this->add($n->setParent($this));  | 
            ||
| 240 | }  | 
            ||
| 241 | }  | 
            ||
| 242 | |||
| 243 | /**  | 
            ||
| 244 | * Determines the correct type and value when a short object/array syntax is found  | 
            ||
| 245 | *  | 
            ||
| 246 |      * @param string $value The value assumed to start with { or ( or characters | 
            ||
| 247 | *  | 
            ||
| 248 | * @see Node::identify  | 
            ||
| 249 | */  | 
            ||
| 250 | private function onCompact($value)  | 
            ||
| 251 |     { | 
            ||
| 252 | $this->value = $value;  | 
            ||
| 253 | json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);  | 
            ||
| 254 |         if (json_last_error() === JSON_ERROR_NONE){ | 
            ||
| 255 | $this->type = Y::JSON;  | 
            ||
| 256 | return;  | 
            ||
| 257 | }  | 
            ||
| 258 |         if (preg_match(R::MAPPING, $value)){ | 
            ||
| 259 | $this->type = Y::COMPACT_MAPPING;  | 
            ||
| 260 | return;  | 
            ||
| 261 | }  | 
            ||
| 262 |         if (preg_match(R::SEQUENCE, $value)){ | 
            ||
| 263 | $this->type = Y::COMPACT_SEQUENCE;  | 
            ||
| 264 | return;  | 
            ||
| 265 | }  | 
            ||
| 266 | $this->type = Y::PARTIAL;  | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * Determines type and value when an hyphen "-" is found  | 
            ||
| 271 | *  | 
            ||
| 272 | * @param string $nodeValue The node value  | 
            ||
| 273 | *  | 
            ||
| 274 | * @see Node::identify  | 
            ||
| 275 | */  | 
            ||
| 276 | private function onHyphen($nodeValue)  | 
            ||
| 299 | }  | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 | * Determines the type and value according to $nodeValue when one of these characters is found : !,&,*  | 
            ||
| 303 | *  | 
            ||
| 304 | * @param string $nodeValue The node value  | 
            ||
| 305 | *  | 
            ||
| 306 | * @see Node::identify  | 
            ||
| 307 | * @todo handle tags like <tag:clarkevans.com,2002:invoice>  | 
            ||
| 308 | */  | 
            ||
| 309 | private function onNodeAction($nodeValue)  | 
            ||
| 321 | }  | 
            ||
| 322 | }  | 
            ||
| 323 | |||
| 324 | /**  | 
            ||
| 325 | * PHP internal function for debugging purpose : simplify output provided by 'var_dump'  | 
            ||
| 326 | *  | 
            ||
| 327 | * @return array the Node properties and respective values displayed by 'var_dump'  | 
            ||
| 328 | */  | 
            ||
| 329 | public function __debugInfo():array  | 
            ||
| 337 |