Complex classes like NamedNode 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 NamedNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class NamedNode implements \Serializable |
||
| 19 | { |
||
| 20 | public $nodeValue = null; |
||
| 21 | private $parentNode = null; |
||
| 22 | private $childNodes = null; |
||
| 23 | private $nodeName = ''; |
||
| 24 | |||
| 25 | public function __construct($nodeName='', $parentNode = null, $childNodes = null, $nodeValue = null) |
||
| 26 | { |
||
| 27 | $this->nodeName = $nodeName; |
||
| 28 | $this->parentNode = $parentNode; |
||
| 29 | $this->childNodes = new NamedNodeList( (array) $childNodes, $this ); |
||
| 30 | $this->nodeValue = $nodeValue; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function __get($name) |
||
| 34 | { |
||
| 35 | switch ($name) { |
||
| 36 | case 'nodeName': |
||
| 37 | return $this->nodeName; |
||
| 38 | break; |
||
| 39 | case 'childNodes': |
||
| 40 | return $this->childNodes; |
||
| 41 | break; |
||
| 42 | case 'parentNode': |
||
| 43 | return $this->parentNode; |
||
| 44 | break; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | public function __set($name, $value) |
||
| 49 | { |
||
| 50 | switch ($name) { |
||
| 51 | case 'nodeName': |
||
| 52 | $this->_setNodeName( $value ); |
||
| 53 | break; |
||
| 54 | case 'childNodes': |
||
| 55 | // make sure nodelists aren't shared between namednodes. |
||
| 56 | $this->childNodes = new NamedNodeList( (array) $value, $this ); |
||
| 57 | break; |
||
| 58 | case 'parentNode': |
||
| 59 | $this->_setParentNode( $value ); |
||
| 60 | break; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | private function _setNodeName($name) |
||
| 65 | { |
||
| 66 | if ($this->parentNode) { |
||
| 67 | if ($this->parentNode->childNodes[$name] !== $this) { |
||
| 68 | $this->parentNode->childNodes[$name] = $this; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | $this->nodeName = $name; |
||
| 72 | } |
||
| 73 | |||
| 74 | private function _setParentNode($node) |
||
| 75 | { |
||
| 76 | if ($node instanceof NamedNode) { |
||
| 77 | $node->appendChild( $this->nodeName, $this ); |
||
| 78 | } elseif (isset($node)) { |
||
| 79 | throw new \arc\UnknownError( 'parentNode is not a \arc\tree\NamedNode', \arc\exceptions::ILLEGAL_ARGUMENT ); |
||
| 80 | } elseif ($this->parentNode) { |
||
| 81 | $this->parentNode->removeChild( $this->nodeName ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | public function __isset($name) |
||
| 86 | { |
||
| 87 | switch ($name) { |
||
| 88 | case 'nodeName': |
||
| 89 | case 'childNodes': |
||
| 90 | return true; // these are always _set_, but may be empty |
||
| 91 | break; |
||
| 92 | case 'parentNode': |
||
| 93 | return isset( $this->parentNode ); |
||
| 94 | break; |
||
| 95 | default: |
||
| 96 | return isset( $this->childNodes[ $name ] ); |
||
| 97 | break; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /* The tree itself must always be deep cloned, a single node cannot have two parentNodes. |
||
| 102 | * The nodeValue may be whatever - so if it is an object, that object will not be cloned. |
||
| 103 | */ |
||
| 104 | public function __clone() |
||
| 105 | { |
||
| 106 | $this->parentNode = null; |
||
| 107 | $this->childNodes = clone $this->childNodes; |
||
| 108 | $this->childNodes->parentNode = $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function __toString() |
||
| 112 | { |
||
| 113 | return (string) $this->nodeValue; |
||
| 114 | } |
||
| 115 | |||
| 116 | // \Serializable interface |
||
| 117 | public function serialize() |
||
| 121 | |||
| 122 | public function unserialize($data) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Adds a new child element to this node with the given name as index in the child list. |
||
| 129 | * If an existing child has the same name, that child will be discarded. |
||
| 130 | * @param string $name The index name of the child |
||
| 131 | * @param mixed $data The data for the new child. If $data is not an instance of \arc\tree\NamedNode |
||
| 132 | * a new instance will be constructed with $data as its nodeValue. |
||
| 133 | * @return \arc\tree\NamedNode The new child node. |
||
| 134 | */ |
||
| 135 | public function appendChild($nodeName, $child=null) |
||
| 136 | { |
||
| 137 | if ( !( $child instanceof \arc\tree\NamedNode )) { |
||
| 138 | $child = new \arc\tree\NamedNode( $nodeName, $this, null, $child ); |
||
| 139 | } |
||
| 140 | if ( $child->parentNode !== $this) { |
||
| 141 | if ( isset($child->parentNode)) { |
||
| 142 | $child->parentNode->removeChild( $child->nodeName ); |
||
| 143 | } |
||
| 144 | if (isset( $this->childNodes[ $nodeName ] )) { |
||
| 145 | $oldChild = $this->childNodes[ $nodeName ]; |
||
| 146 | $oldChild->parentNode = null; |
||
| 147 | } |
||
| 148 | $child->parentNode = $this; |
||
| 149 | } |
||
| 150 | $this->childNodes[ $nodeName ] = $child; |
||
| 151 | |||
| 152 | return $child; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Removes an existing child with the given name from this node. |
||
| 157 | * @param string $nodeName The index name of the child |
||
| 158 | * @return \arc\tree\NamedNode The removed child or null. |
||
| 159 | */ |
||
| 160 | public function removeChild($nodeName) |
||
| 161 | { |
||
| 162 | if ( isset( $this->childNodes[ $nodeName ] )) { |
||
| 163 | $child = $this->childNodes[ $nodeName ]; |
||
| 164 | unset( $this->childNodes[ $nodeName ] ); |
||
| 165 | $child->parentNode = null; |
||
| 166 | |||
| 167 | return $child; |
||
| 168 | } else { |
||
| 169 | return null; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the absolute path of the node. |
||
| 175 | * @return string the absolute path of the node |
||
| 176 | */ |
||
| 177 | public function getPath() |
||
| 178 | { |
||
| 179 | return \arc\tree::parents( |
||
| 180 | $this, |
||
| 181 | function ($node, $result) { |
||
| 182 | return $result . $node->nodeName . '/'; |
||
| 183 | } |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the root node object of this tree. |
||
| 189 | * @return \arc\tree\Node the root node |
||
| 190 | */ |
||
| 191 | public function getRootNode() |
||
| 192 | { |
||
| 193 | $result = \arc\tree::dive( |
||
| 194 | $this, |
||
| 195 | function ($node) { |
||
| 196 | return isset( $node->parentNode ) ? null : $node; |
||
| 197 | } |
||
| 198 | ); |
||
| 199 | |||
| 200 | return $result; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns the node with the given path, relative to this node. If the path |
||
| 205 | * does not exist, missing nodes will be created automatically. |
||
| 206 | * @param string $path The path to change to |
||
| 207 | * @return \arc\tree\NamedNode The target node corresponding with the given path. |
||
| 208 | */ |
||
| 209 | public function cd($path) |
||
| 210 | { |
||
| 211 | if (\arc\path::isAbsolute( $path )) { |
||
| 212 | $node = $this->getRootNode(); |
||
| 213 | } else { |
||
| 214 | $node = $this; |
||
| 215 | } |
||
| 216 | $result = \arc\path::reduce( $path, function ($node, $name) { |
||
| 217 | switch ($name) { |
||
| 218 | case '..': |
||
| 219 | return ( isset( $node->parentNode ) ? $node->parentNode : $node ); |
||
| 220 | break; |
||
| 221 | case '.': |
||
| 222 | case '': |
||
| 223 | return $node; |
||
| 224 | break; |
||
| 225 | default: |
||
| 226 | if ( !isset( $node->childNodes[ $name ] ) ) { |
||
| 227 | return $node->appendChild( $name ); |
||
| 228 | } else { |
||
| 229 | return $node->childNodes[ $name ]; |
||
| 230 | } |
||
| 231 | break; |
||
| 232 | } |
||
| 233 | }, $node); |
||
| 234 | |||
| 235 | return $result; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Calls a callback method on each child of this node, returns an array with name => result pairs. |
||
| 240 | * The callback method must accept two parameters, the name of the child and the child node itself. |
||
| 241 | * @param callable $callback The callback method to run on each child. |
||
| 242 | * @return array An array of result values with the name of each child as key. |
||
| 243 | */ |
||
| 244 | public function ls($callback) |
||
| 248 | |||
| 249 | } |
||
| 250 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: