Complex classes like HierarchicalTrait 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 HierarchicalTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait HierarchicalTrait |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The object's parent, if any, in the hierarchy. |
||
| 18 | * |
||
| 19 | * @var string|null |
||
| 20 | */ |
||
| 21 | protected $master; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Store a copy of the object's ancestry. |
||
| 25 | * |
||
| 26 | * @var HierarchicalInterface[]|null |
||
| 27 | */ |
||
| 28 | private $hierarchy = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Store a copy of the object's descendants. |
||
| 32 | * |
||
| 33 | * @var HierarchicalInterface[]|null |
||
| 34 | */ |
||
| 35 | private $children; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Store a copy of the object's siblings. |
||
| 39 | * |
||
| 40 | * @var HierarchicalInterface[]|null |
||
| 41 | */ |
||
| 42 | private $siblings; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The object's parent object, if any, in the hierarchy. |
||
| 46 | * |
||
| 47 | * @var HierarchicalInterface|null |
||
| 48 | */ |
||
| 49 | private $masterObject; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * A store of cached objects. |
||
| 53 | * |
||
| 54 | * @var ModelInterface[] $objectCache |
||
| 55 | */ |
||
| 56 | public static $objectCache = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Reset this object's hierarchy. |
||
| 60 | * |
||
| 61 | * The object's hierarchy can be rebuilt with {@see self::hierarchy()}. |
||
| 62 | * |
||
| 63 | * @return HierarchicalInterface Chainable |
||
| 64 | */ |
||
| 65 | public function resetHierarchy() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set this object's immediate parent. |
||
| 74 | * |
||
| 75 | * @param mixed $master The object's parent (or master). |
||
| 76 | * @return HierarchicalInterface Chainable |
||
| 77 | */ |
||
| 78 | public function setMaster($master) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Retrieve this object's immediate parent. |
||
| 87 | * |
||
| 88 | * @return string|null |
||
| 89 | */ |
||
| 90 | public function getMaster() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Retrieve this object's immediate parent as object. |
||
| 97 | * @return HierarchicalInterface|null |
||
| 98 | * @throws UnexpectedValueException The current object cannot be its own parent. |
||
| 99 | */ |
||
| 100 | public function getMasterObject() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Determine if this object has a direct parent. |
||
| 124 | * |
||
| 125 | * @return boolean |
||
| 126 | */ |
||
| 127 | public function hasMaster() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Determine if this object is the head (top-level) of its hierarchy. |
||
| 134 | * |
||
| 135 | * Top-level objects do not have a parent (master). |
||
| 136 | * |
||
| 137 | * @return boolean |
||
| 138 | */ |
||
| 139 | public function isTopLevel() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Determine if this object is the tail (last-level) of its hierarchy. |
||
| 146 | * |
||
| 147 | * Last-level objects do not have a children. |
||
| 148 | * |
||
| 149 | * @return boolean |
||
| 150 | */ |
||
| 151 | public function isLastLevel() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Retrieve this object's position (level) in its hierarchy. |
||
| 158 | * |
||
| 159 | * Starts at "1" (top-level). |
||
| 160 | * |
||
| 161 | * The level is calculated by loading all ancestors with {@see self::hierarchy()}. |
||
| 162 | * |
||
| 163 | * @return integer |
||
| 164 | */ |
||
| 165 | public function hierarchyLevel() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Retrieve the top-level ancestor of this object. |
||
| 177 | * |
||
| 178 | * @return HierarchicalInterface|null |
||
| 179 | */ |
||
| 180 | public function toplevelMaster() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Determine if this object has any ancestors. |
||
| 192 | * |
||
| 193 | * @return boolean |
||
| 194 | */ |
||
| 195 | public function hasParents() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Retrieve this object's ancestors (from immediate parent to top-level). |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function hierarchy() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Retrieve this object's ancestors, inverted from top-level to immediate. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function invertedHierarchy() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Determine if the object is the parent of the given object. |
||
| 235 | * |
||
| 236 | * @param mixed $child The child (or ID) to match against. |
||
| 237 | * @return boolean |
||
| 238 | */ |
||
| 239 | public function isMasterOf($child) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Determine if the object is a parent/ancestor of the given object. |
||
| 248 | * |
||
| 249 | * @param mixed $child The child (or ID) to match against. |
||
| 250 | * @return boolean |
||
| 251 | * @todo Implementation needed. |
||
| 252 | */ |
||
| 253 | public function recursiveIsMasterOf($child) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get wether the object has any children at all |
||
| 262 | * @return boolean |
||
| 263 | */ |
||
| 264 | public function hasChildren() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the number of children directly under this object. |
||
| 273 | * @return integer |
||
| 274 | */ |
||
| 275 | public function numChildren() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the total number of children in the entire hierarchy. |
||
| 284 | * This method counts all children and sub-children, unlike `numChildren()` which only count 1 level. |
||
| 285 | * @return integer |
||
| 286 | */ |
||
| 287 | public function recursiveNumChildren() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $children The children to set. |
||
| 295 | * @return HierarchicalInterface Chainable |
||
| 296 | */ |
||
| 297 | public function setChildren(array $children) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param mixed $child The child object (or ident) to add. |
||
| 309 | * @return HierarchicalInterface Chainable |
||
| 310 | * @throws UnexpectedValueException The current object cannot be its own child. |
||
| 311 | */ |
||
| 312 | public function addChild($child) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the children directly under this object. |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function children() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | abstract public function loadChildren(); |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param mixed $master The master object (or ident) to check against. |
||
| 352 | * @return boolean |
||
| 353 | */ |
||
| 354 | public function isChildOf($master) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param mixed $master The master object (or ident) to check against. |
||
| 366 | * @return boolean |
||
| 367 | */ |
||
| 368 | public function recursiveIsChildOf($master) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return boolean |
||
| 383 | */ |
||
| 384 | public function hasSiblings() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return integer |
||
| 393 | */ |
||
| 394 | public function numSiblings() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get all the objects on the same level as this one. |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | public function siblings() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param mixed $sibling The sibling to check. |
||
| 425 | * @return boolean |
||
| 426 | */ |
||
| 427 | public function isSiblingOf($sibling) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param mixed $ident The ident. |
||
| 436 | * @return HierarchicalInterface|null |
||
| 437 | * @throws InvalidArgumentException If the identifier is not a scalar value. |
||
| 438 | */ |
||
| 439 | private function objFromIdent($ident) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Retrieve an object from the storage source by its ID. |
||
| 478 | * |
||
| 479 | * @param mixed $id The object id. |
||
| 480 | * @return null|HierarchicalInterface |
||
| 481 | */ |
||
| 482 | private function loadObjectFromSource($id) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Retrieve an object from the cache store by its ID. |
||
| 496 | * |
||
| 497 | * @param mixed $id The object id. |
||
| 498 | * @return null|HierarchicalInterface |
||
| 499 | */ |
||
| 500 | private function loadObjectFromCache($id) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Add an object to the cache store. |
||
| 512 | * |
||
| 513 | * @param ModelInterface $obj The object to store. |
||
| 514 | * @return HierarchicalInterface Chainable |
||
| 515 | */ |
||
| 516 | private function addObjectToCache(ModelInterface $obj) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Retrieve the object model factory. |
||
| 525 | * |
||
| 526 | * @return \Charcoal\Factory\FactoryInterface |
||
| 527 | */ |
||
| 528 | abstract public function modelFactory(); |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | abstract public function id(); |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | abstract public function objType(); |
||
| 539 | } |
||
| 540 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.