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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | /** @var null|string */ |
||
| 26 | public $raw; |
||
| 27 | |||
| 28 | /** @var null|Node */ |
||
| 29 | private $parent; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create the Node object and parses $nodeString IF not null (else assume a root type Node) |
||
| 33 | * |
||
| 34 | * @param string|null $nodeString The node string |
||
| 35 | * @param int|null $line The line |
||
|
|
|||
| 36 | */ |
||
| 37 | public function __construct($nodeString = null, $line = 0) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Sets the parent of the current Node |
||
| 50 | * |
||
| 51 | * @param Node $node The node |
||
| 52 | * |
||
| 53 | * @return Node|self The currentNode |
||
| 54 | */ |
||
| 55 | public function setParent(Node $node):Node |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Gets the ancestor with specified $indent or the direct $parent OR the current Node itself |
||
| 63 | * |
||
| 64 | * @param int|null $indent The indent |
||
| 65 | * @param int $type first ancestor of this YAML::type is returned |
||
| 66 | * |
||
| 67 | * @return Node|self The parent. |
||
| 68 | */ |
||
| 69 | public function getParent(int $indent = null, $type = 0):Node |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set the value for the current Node : |
||
| 86 | * - if value is null , then value = $child (Node) |
||
| 87 | * - if value is Node, then value is a NodeList with (previous value AND $child) |
||
| 88 | * - if value is a NodeList, push $child into and set NodeList type accordingly |
||
| 89 | * |
||
| 90 | * @param Node $child The child |
||
| 91 | * @todo refine the conditions when Y::LITTERALS |
||
| 92 | */ |
||
| 93 | public function add(Node $child) |
||
| 117 | |||
| 118 | //modify value type according to child |
||
| 119 | // private function adjustValueType(Node $child) |
||
| 120 | // { |
||
| 121 | // if ($child->type & Y::SET_KEY) $this->value->type = Y::SET; |
||
| 122 | // if ($child->type & Y::KEY) $this->value->type = Y::MAPPING; |
||
| 123 | // if ($child->type & Y::ITEM) $this->value->type = Y::SEQUENCE; |
||
| 124 | // } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Gets the deepest node. |
||
| 128 | * |
||
| 129 | * @return Node|self The deepest node. |
||
| 130 | */ |
||
| 131 | public function getDeepestNode():Node |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Parses the string (assumed to be a line from a valid YAML) |
||
| 151 | * |
||
| 152 | * @param string $nodeString The node string |
||
| 153 | * |
||
| 154 | * @return Node|self |
||
| 155 | */ |
||
| 156 | public function parse(string $nodeString):Node |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the type and value according to first character |
||
| 175 | * |
||
| 176 | * @param string $nodeValue The node value |
||
| 177 | */ |
||
| 178 | private function identify($nodeValue) |
||
| 202 | |||
| 203 | private function onQuoted($nodeValue) |
||
| 208 | |||
| 209 | private function onSetElement($nodeValue) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Process when a "key: value" syntax is found in the parsed string |
||
| 221 | * Note : key is match 1, value is match 2 as per regex from R::KEY |
||
| 222 | * |
||
| 223 | * @param array $matches The matches provided by 'preg_match' function in Node::parse |
||
| 224 | */ |
||
| 225 | private function onKey(array $matches) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Determines the correct type and value when a compact object/array syntax is found |
||
| 249 | * |
||
| 250 | * @param string $value The value assumed to start with { or [ or characters |
||
| 251 | * |
||
| 252 | * @see Node::identify |
||
| 253 | */ |
||
| 254 | private function onCompact($value) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Determines type and value when an hyphen "-" is found |
||
| 293 | * |
||
| 294 | * @param string $nodeValue The node value |
||
| 295 | * |
||
| 296 | * @see Node::identify |
||
| 297 | */ |
||
| 298 | private function onHyphen($nodeValue) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Determines the type and value according to $nodeValue when one of these characters is found : !,&,* |
||
| 323 | * |
||
| 324 | * @param string $nodeValue The node value |
||
| 325 | * |
||
| 326 | * @see Node::identify |
||
| 327 | * @todo handle tags like <tag:clarkevans.com,2002:invoice> |
||
| 328 | */ |
||
| 329 | private function onNodeAction($nodeValue) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * PHP internal function for debugging purpose : simplify output provided by 'var_dump' |
||
| 345 | * |
||
| 346 | * @return array the Node properties and respective values displayed by 'var_dump' |
||
| 347 | */ |
||
| 348 | public function __debugInfo():array |
||
| 355 | } |
||
| 356 |