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|integer|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) |
||
| 79 | { |
||
| 80 | $this->master = $master; |
||
| 81 | |||
| 82 | $this->resetHierarchy(); |
||
| 83 | |||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Retrieve this object's immediate parent. |
||
| 89 | * |
||
| 90 | * @return string|null |
||
| 91 | */ |
||
| 92 | public function getMaster() |
||
| 93 | { |
||
| 94 | return $this->master; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Retrieve this object's immediate parent as object. |
||
| 99 | * @return HierarchicalInterface|null |
||
| 100 | * @throws UnexpectedValueException The current object cannot be its own parent. |
||
| 101 | */ |
||
| 102 | public function getMasterObject() |
||
| 103 | { |
||
| 104 | if (!$this->masterObject && $this->hasMaster()) { |
||
| 105 | $master = $this->objFromIdent($this->getMaster()); |
||
| 106 | |||
| 107 | if ($master instanceof ModelInterface) { |
||
| 108 | if ($master->id() === $this->id()) { |
||
| 109 | throw new UnexpectedValueException(sprintf( |
||
| 110 | 'Can not be ones own parent: %s', |
||
| 111 | $master->id() |
||
| 112 | )); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->masterObject = $master; |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->masterObject; |
||
| 120 | } |
||
| 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() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Retrieve the top-level ancestor of this object. |
||
| 175 | * |
||
| 176 | * @return HierarchicalInterface|null |
||
| 177 | */ |
||
| 178 | public function toplevelMaster() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Determine if this object has any ancestors. |
||
| 190 | * |
||
| 191 | * @return boolean |
||
| 192 | */ |
||
| 193 | public function hasParents() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Retrieve this object's ancestors (from immediate parent to top-level). |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function hierarchy() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Retrieve this object's ancestors, inverted from top-level to immediate. |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function invertedHierarchy() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Determine if the object is the parent of the given object. |
||
| 233 | * |
||
| 234 | * @param mixed $child The child (or ID) to match against. |
||
| 235 | * @return boolean |
||
| 236 | */ |
||
| 237 | public function isMasterOf($child) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Determine if the object is a parent/ancestor of the given object. |
||
| 246 | * |
||
| 247 | * @param mixed $child The child (or ID) to match against. |
||
| 248 | * @return boolean |
||
| 249 | * @todo Implementation needed. |
||
| 250 | */ |
||
| 251 | public function recursiveIsMasterOf($child) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get wether the object has any children at all |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | public function hasChildren() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the number of children directly under this object. |
||
| 271 | * @return integer |
||
| 272 | */ |
||
| 273 | public function numChildren() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the total number of children in the entire hierarchy. |
||
| 282 | * This method counts all children and sub-children, unlike `numChildren()` which only count 1 level. |
||
| 283 | * @return integer |
||
| 284 | */ |
||
| 285 | public function recursiveNumChildren() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param array $children The children to set. |
||
| 293 | * @return HierarchicalInterface Chainable |
||
| 294 | */ |
||
| 295 | public function setChildren(array $children) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param mixed $child The child object (or ident) to add. |
||
| 307 | * @return HierarchicalInterface Chainable |
||
| 308 | * @throws UnexpectedValueException The current object cannot be its own child. |
||
| 309 | */ |
||
| 310 | public function addChild($child) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the children directly under this object. |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function children() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | abstract public function loadChildren(); |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param mixed $master The master object (or ident) to check against. |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | public function isChildOf($master) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param mixed $master The master object (or ident) to check against. |
||
| 364 | * @return boolean |
||
| 365 | */ |
||
| 366 | public function recursiveIsChildOf($master) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return boolean |
||
| 381 | */ |
||
| 382 | public function hasSiblings() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return integer |
||
| 391 | */ |
||
| 392 | public function numSiblings() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get all the objects on the same level as this one. |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function siblings() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param mixed $sibling The sibling to check. |
||
| 423 | * @return boolean |
||
| 424 | */ |
||
| 425 | public function isSiblingOf($sibling) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param mixed $ident The ident. |
||
| 434 | * @return HierarchicalInterface|null |
||
| 435 | * @throws InvalidArgumentException If the identifier is not a scalar value. |
||
| 436 | */ |
||
| 437 | private function objFromIdent($ident) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Retrieve an object from the storage source by its ID. |
||
| 476 | * |
||
| 477 | * @param mixed $id The object id. |
||
| 478 | * @return null|HierarchicalInterface |
||
| 479 | */ |
||
| 480 | private function loadObjectFromSource($id) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Retrieve an object from the cache store by its ID. |
||
| 494 | * |
||
| 495 | * @param mixed $id The object id. |
||
| 496 | * @return null|HierarchicalInterface |
||
| 497 | */ |
||
| 498 | private function loadObjectFromCache($id) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Add an object to the cache store. |
||
| 510 | * |
||
| 511 | * @param ModelInterface $obj The object to store. |
||
| 512 | * @return HierarchicalInterface Chainable |
||
| 513 | */ |
||
| 514 | private function addObjectToCache(ModelInterface $obj) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Retrieve the object model factory. |
||
| 523 | * |
||
| 524 | * @return \Charcoal\Factory\FactoryInterface |
||
| 525 | */ |
||
| 526 | abstract public function modelFactory(); |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | abstract public function id(); |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | abstract public function objType(); |
||
| 537 | } |
||
| 538 |
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.