Conditions | 10 |
Paths | 22 |
Total Lines | 46 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
137 | public function replaceNode($sourceNode, $newNode) |
||
138 | { |
||
139 | $this->update(); |
||
140 | |||
141 | $hash = spl_object_hash($sourceNode); |
||
142 | |||
143 | if (array_key_exists($hash, $this->parentChain)) { |
||
144 | $parents = [$this->parentChain[$hash]]; |
||
145 | } else { |
||
146 | $parents = $this->tree; |
||
147 | } |
||
148 | |||
149 | foreach ($parents as $key => $parent) { |
||
150 | if ($parent === $sourceNode) { |
||
151 | $parent[$key] = $newNode; |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | foreach ($parent->getSubNodeNames() as $subNodeName) { |
||
156 | $nodes = &$parent->{$subNodeName}; |
||
157 | |||
158 | if (!is_array($nodes)) { |
||
159 | if ($nodes === $sourceNode) { |
||
160 | $parent->{$subNodeName} = $newNode; |
||
161 | return; |
||
162 | } |
||
163 | |||
164 | continue; |
||
165 | } |
||
166 | |||
167 | $foundKey = false; |
||
168 | foreach ($nodes as $key => $node) { |
||
169 | if ($node === $sourceNode) { |
||
170 | $foundKey = $key; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if (false !== $foundKey) { |
||
175 | $nodes[$foundKey] = $newNode; |
||
176 | return; |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | throw new NodeNotFoundException('Node not found, replace not possible'); |
||
182 | } |
||
183 | } |
||
184 |
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: