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 type is returned |
||
| 66 | * |
||
| 67 | * @return Node|self The parent. |
||
| 68 | */ |
||
| 69 | public function getParent(int $indent = null, $type = 0):Node |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set the value for the current Node : |
||
| 88 | * - if value is null , then value = $child (Node) |
||
| 89 | * - if value is Node, then value is a NodeList with (previous value AND $child) |
||
| 90 | * - if value is a NodeList, push $child into and set NodeList type accordingly |
||
| 91 | * |
||
| 92 | * @param Node $child The child |
||
| 93 | */ |
||
| 94 | public function add(Node $child) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Gets the deepest node. |
||
| 126 | * |
||
| 127 | * @return Node|self The deepest node. |
||
| 128 | */ |
||
| 129 | public function getDeepestNode():Node |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Parses the string (assumed to be a line from a valid YAML) |
||
| 149 | * |
||
| 150 | * @param string $nodeString The node string |
||
| 151 | * |
||
| 152 | * @return Node|self |
||
| 153 | */ |
||
| 154 | public function parse(string $nodeString):Node |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set the type and value according to first character |
||
| 173 | * |
||
| 174 | * @param string $nodeValue The node value |
||
| 175 | */ |
||
| 176 | private function identify($nodeValue) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Process when a "key: value" syntax is found in the parsed string |
||
| 222 | * Note : key is match 1, value is match 2 as per regex from R::KEY |
||
| 223 | * |
||
| 224 | * @param array $matches The matches provided by 'preg_match' function in Node::parse |
||
| 225 | */ |
||
| 226 | private function onKey(array $matches) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Determines the correct type and value when a compact object/array syntax is found |
||
| 251 | * |
||
| 252 | * @param string $value The value assumed to start with { or [ or characters |
||
| 253 | * |
||
| 254 | * @see Node::identify |
||
| 255 | */ |
||
| 256 | private function onCompact($value) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Determines type and value when an hyphen "-" is found |
||
| 295 | * |
||
| 296 | * @param string $nodeValue The node value |
||
| 297 | * |
||
| 298 | * @see Node::identify |
||
| 299 | */ |
||
| 300 | private function onHyphen($nodeValue) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Determines the type and value according to $nodeValue when one of these characters is found : !,&,* |
||
| 327 | * |
||
| 328 | * @param string $nodeValue The node value |
||
| 329 | * |
||
| 330 | * @see Node::identify |
||
| 331 | * @todo handle tags like <tag:clarkevans.com,2002:invoice> |
||
| 332 | */ |
||
| 333 | private function onNodeAction($nodeValue) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * PHP internal function for debugging purpose : simplify output provided by 'var_dump' |
||
| 350 | * |
||
| 351 | * @return array the Node properties and respective values displayed by 'var_dump' |
||
| 352 | */ |
||
| 353 | public function __debugInfo():array |
||
| 360 | } |
||
| 361 |