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 | class Node |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var self |
||
| 17 | */ |
||
| 18 | private $parent; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var NodeCollection |
||
| 22 | */ |
||
| 23 | private $children; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var mixed |
||
| 27 | */ |
||
| 28 | private $entry; |
||
| 29 | |||
| 30 | 171 | public function __construct($entry) |
|
| 35 | |||
| 36 | 42 | public function equals(self $other) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param array|NodeCollection $children |
||
| 43 | * @return Node |
||
| 44 | */ |
||
| 45 | 129 | public function addChildren($children): self |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @return NodeCollection |
||
| 60 | */ |
||
| 61 | 93 | public function children(): NodeCollection |
|
| 65 | |||
| 66 | 75 | public function entry($key = null) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Replace entire entry value |
||
| 80 | * |
||
| 81 | * @param $entry |
||
| 82 | */ |
||
| 83 | 3 | public function replaceEntry($entry) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param Node $parent |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | 126 | public function parent(self $parent = null) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Remove this node or (detaches) a child node. |
||
| 104 | * This detaches self from parent or when node is passed, it deletes that child node from any depth in the graph |
||
| 105 | * Also removes the entire children tree from that node! |
||
| 106 | * |
||
| 107 | * @param Node $node |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | 33 | public function remove(self $node = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Move a child node to a different parent |
||
| 127 | * |
||
| 128 | * @param Node $parent |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | 21 | public function move(self $parent) |
|
| 135 | |||
| 136 | 3 | public function moveToRoot() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * At which depth does this node resides inside the entire tree |
||
| 143 | * |
||
| 144 | * @return int |
||
| 145 | */ |
||
| 146 | 3 | public function depth(): int |
|
| 152 | |||
| 153 | /** |
||
| 154 | * count of all direct child nodes |
||
| 155 | * |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | 6 | public function count(): int |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Total of all child nodes |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | 12 | public function total(): int |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | 27 | public function isLeaf(): bool |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | 48 | public function isRoot(): bool |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param $key |
||
| 195 | * @param array $values |
||
| 196 | * @return NodeCollection |
||
| 197 | */ |
||
| 198 | 3 | public function findMany($key, array $values): NodeCollection |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @param $key |
||
| 205 | * @param $value |
||
| 206 | * @return Node |
||
| 207 | */ |
||
| 208 | 3 | public function find($key, $value): Node |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param null $depth |
||
| 215 | * @return NodeCollection |
||
| 216 | */ |
||
| 217 | 9 | public function ancestors($depth = null): NodeCollection |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get flat array of plucked values from child nodes |
||
| 224 | * |
||
| 225 | * @param $key |
||
| 226 | * @param null $value |
||
| 227 | * @param bool $down |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 12 | public function pluck($key, $value = null, $down = true): array |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get flat array of plucked values from child nodes |
||
| 237 | * |
||
| 238 | * @param $key |
||
| 239 | * @param null $value |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | 3 | public function pluckAncestors($key, $value = null): array |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Get a copy of this node |
||
| 249 | * |
||
| 250 | * @param null|int $depth |
||
| 251 | * @return Node |
||
| 252 | */ |
||
| 253 | 33 | public function copy($depth = null): self |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Copy of this node without its parent / children relationships |
||
| 262 | * |
||
| 263 | * @return Node |
||
| 264 | */ |
||
| 265 | 33 | public function isolatedCopy(): self |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Reduce collection to the nodes that pass the callback |
||
| 272 | * Shaking a collection will keep the ancestor structure |
||
| 273 | * |
||
| 274 | * @param callable $callback |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | 9 | public function shake(Callable $callback): self |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Same as shaking except that it will not keep the ancestor structure |
||
| 284 | * |
||
| 285 | * @param callable $callback |
||
| 286 | * @return self |
||
| 287 | */ |
||
| 288 | 12 | public function prune(Callable $callback): self |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param $children |
||
| 295 | * @return NodeCollection |
||
| 296 | */ |
||
| 297 | 129 | private function transformToNodeCollection($children): NodeCollection |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Fetch entry data via a direct call to Node. |
||
| 312 | * E.g. $node->name resolves to $node->entry('name') |
||
| 313 | * |
||
| 314 | * @param $name |
||
| 315 | * @return mixed|null|NodeCollection |
||
| 316 | */ |
||
| 317 | 15 | public function __get($name) |
|
| 323 | } |
||
| 324 |
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: