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