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 extends Node implements \Serializable |
||
19 | { |
||
20 | public $nodeValue = null; |
||
21 | private $parentNode = null; |
||
22 | private $childNodes = null; |
||
23 | private $nodeName = ''; |
||
24 | |||
25 | 16 | public function __construct($nodeName='', $parentNode = null, $childNodes = null, $nodeValue = null) |
|
32 | |||
33 | 16 | public function __get($name) |
|
34 | { |
||
35 | 16 | switch ($name) { |
|
36 | 16 | case 'nodeName': |
|
37 | 10 | return $this->nodeName; |
|
38 | break; |
||
39 | 16 | case 'childNodes': |
|
40 | 14 | return $this->childNodes; |
|
41 | break; |
||
42 | 16 | case 'parentNode': |
|
43 | 16 | return $this->parentNode; |
|
44 | break; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | 2 | public function __set($name, $value) |
|
49 | { |
||
50 | 2 | switch ($name) { |
|
51 | 2 | case 'nodeName': |
|
52 | $this->_setNodeName( $value ); |
||
53 | break; |
||
54 | 2 | case 'childNodes': |
|
55 | // make sure nodelists aren't shared between namednodes. |
||
56 | $this->childNodes = new NamedNodeList( (array) $value, $this ); |
||
57 | break; |
||
58 | 2 | case 'parentNode': |
|
59 | 2 | $this->_setParentNode( $value ); |
|
60 | 2 | break; |
|
61 | } |
||
62 | 2 | } |
|
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 | 2 | private function _setParentNode($node) |
|
75 | { |
||
76 | 2 | if ($node instanceof NamedNode) { |
|
77 | 2 | $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 | 2 | } elseif ($this->parentNode) { |
|
81 | 2 | $this->parentNode->removeChild( $this->nodeName ); |
|
82 | } |
||
83 | 2 | } |
|
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 | 2 | public function __clone() |
|
110 | |||
111 | 4 | public function __toString() |
|
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 | 14 | public function appendChild($nodeName, $child=null) |
|
136 | { |
||
137 | 14 | if ( !( $child instanceof \arc\tree\NamedNode )) { |
|
138 | 12 | $child = new \arc\tree\NamedNode( $nodeName, $this, null, $child ); |
|
139 | } |
||
140 | 14 | if ( $child->parentNode !== $this) { |
|
141 | 2 | if ( isset($child->parentNode)) { |
|
142 | $child->parentNode->removeChild( $child->nodeName ); |
||
143 | } |
||
144 | 2 | if (isset( $this->childNodes[ $nodeName ] )) { |
|
145 | 2 | $oldChild = $this->childNodes[ $nodeName ]; |
|
146 | 2 | $oldChild->parentNode = null; |
|
147 | } |
||
148 | 2 | $child->parentNode = $this; |
|
149 | } |
||
150 | 14 | $this->childNodes[ $nodeName ] = $child; |
|
151 | |||
152 | 14 | 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 | 4 | public function removeChild($nodeName) |
|
172 | |||
173 | /** |
||
174 | * Returns the absolute path of the node. |
||
175 | * @return string the absolute path of the node |
||
176 | */ |
||
177 | 4 | public function getPath() |
|
186 | |||
187 | /** |
||
188 | * Returns the root node object of this tree. |
||
189 | * @return \arc\tree\Node the root node |
||
190 | */ |
||
191 | 6 | public function getRootNode() |
|
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 | 8 | public function cd($path) |
|
210 | { |
||
211 | 8 | if (\arc\path::isAbsolute( $path )) { |
|
212 | 6 | $node = $this->getRootNode(); |
|
213 | } else { |
||
214 | 4 | $node = $this; |
|
215 | } |
||
216 | 8 | $result = \arc\path::reduce( $path, function ($node, $name) { |
|
217 | 8 | switch ($name) { |
|
218 | 8 | case '..': |
|
219 | return ( isset( $node->parentNode ) ? $node->parentNode : $node ); |
||
220 | break; |
||
221 | 8 | case '.': |
|
222 | 8 | case '': |
|
223 | return $node; |
||
224 | break; |
||
225 | default: |
||
226 | 8 | if ( !isset( $node->childNodes[ $name ] ) ) { |
|
227 | 2 | return $node->appendChild( $name ); |
|
228 | } else { |
||
229 | 6 | return $node->childNodes[ $name ]; |
|
230 | } |
||
231 | break; |
||
232 | } |
||
233 | 8 | }, $node); |
|
234 | |||
235 | 8 | 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 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.