Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like HierarchicalCollection 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 HierarchicalCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class HierarchicalCollection extends CharcoalCollection |
||
23 | { |
||
24 | /** |
||
25 | * The current page (slice). |
||
26 | * |
||
27 | * @var integer |
||
28 | */ |
||
29 | protected $page = 0; |
||
30 | |||
31 | /** |
||
32 | * The number of objects per page (slice). |
||
33 | * |
||
34 | * @var integer |
||
35 | */ |
||
36 | protected $numPerPage = 0; |
||
37 | |||
38 | /** |
||
39 | * Create a new hierarchically-sorted collection. |
||
40 | * |
||
41 | * @param array|Traversable|null $objs Array of objects to pre-populate this collection. |
||
42 | * @param boolean $sort Whether to sort the collection immediately. |
||
43 | * @return void |
||
|
|||
44 | */ |
||
45 | public function __construct($objs = [], $sort = true) |
||
55 | |||
56 | /** |
||
57 | * Sort the hierarchical collection of objects. |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | public function sortTree() |
||
164 | |||
165 | /** |
||
166 | * Given an object, display the nested hierarchy of descendants. |
||
167 | * |
||
168 | * @param HierarchicalInterface $parentObj The parent object from which to append its |
||
169 | * descendants for display. |
||
170 | * @param HierarchicalInterface[] $childObjects The list of descendants by parent object ID. |
||
171 | * Passed by reference. |
||
172 | * @param integer $count The current count of objects to display, |
||
173 | * for pagination. Passed by reference. |
||
174 | * @param integer $level The level directly below the $parentObj. |
||
175 | * @param HierarchicalInterface[] $sortedObjects The list of objects to be displayed. |
||
176 | * Passed by reference. |
||
177 | * @return void |
||
178 | */ |
||
179 | private function sortDescendantObjects( |
||
280 | |||
281 | /** |
||
282 | * @param integer $page The current page. Start at 0. |
||
283 | * @throws InvalidArgumentException If the parameter is not numeric or < 0. |
||
284 | * @return Pagination (Chainable) |
||
285 | */ |
||
286 | View Code Duplication | public function setPage($page) |
|
305 | |||
306 | /** |
||
307 | * @return integer |
||
308 | */ |
||
309 | public function getPage() |
||
310 | { |
||
311 | return $this->page; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param integer $num The number of results to retrieve, per page. |
||
316 | * @throws InvalidArgumentException If the parameter is not numeric or < 0. |
||
317 | * @return Pagination (Chainable) |
||
318 | */ |
||
319 | View Code Duplication | public function setNumPerPage($num) |
|
339 | |||
340 | /** |
||
341 | * @return integer |
||
342 | */ |
||
343 | public function getNumPerPage() |
||
344 | { |
||
345 | return $this->numPerPage; |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Determine if the given value is acceptable for the collection. |
||
350 | * |
||
351 | * @param mixed $value The value being vetted. |
||
352 | * @return boolean |
||
353 | */ |
||
354 | public function isAcceptable($value) |
||
358 | } |
||
359 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.