Complex classes like HasChildrenTrait 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 HasChildrenTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait HasChildrenTrait |
||
10 | { |
||
11 | /** |
||
12 | * List of the child element |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $elements = array(); |
||
17 | |||
18 | /** |
||
19 | * Value to keep track of the order the elements were added |
||
20 | * |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $elementsIndex = PHP_INT_MAX; |
||
24 | |||
25 | /** |
||
26 | * Generates the actual name that will be used to identify the element in the input object |
||
27 | * For input objects the name of the child is the same as the name provided, |
||
28 | * For field-sets the name of the child is prefixed/name-spaced with the name of the field-set |
||
29 | * For collections the name of the child is prefixed with the name of the collection and an index placeholder |
||
30 | * |
||
31 | * @param string $name |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | 12 | protected function getFullChildName($name) |
|
39 | |||
40 | /** |
||
41 | * Add an element to the children list |
||
42 | * |
||
43 | * @param string|\Sirius\Input\Element $nameOrElement |
||
44 | * @param array $specs |
||
45 | * |
||
46 | * @throws \RuntimeException |
||
47 | * @return $this |
||
48 | */ |
||
49 | 19 | function addElement($nameOrElement, $specs = array()) |
|
71 | |||
72 | /** |
||
73 | * |
||
74 | */ |
||
75 | 9 | protected function createChildren() |
|
85 | |||
86 | /** |
||
87 | * Retrieve an element by name |
||
88 | * |
||
89 | * @param string $name |
||
90 | * |
||
91 | * @return \Sirius\Input\Element |
||
92 | */ |
||
93 | 9 | function getElement($name) |
|
97 | |||
98 | /** |
||
99 | * Removes an element from the children list |
||
100 | * |
||
101 | * @param string $name |
||
102 | * |
||
103 | * @throws \RuntimeException |
||
104 | * @return $this |
||
105 | */ |
||
106 | 3 | function removeElement($name) |
|
114 | |||
115 | /** |
||
116 | * Returns whether an element exist in the children list |
||
117 | * |
||
118 | * @param string $name |
||
119 | * |
||
120 | * @return boolean |
||
121 | */ |
||
122 | 3 | function hasElement($name) |
|
126 | |||
127 | /** |
||
128 | * Input comparator callback |
||
129 | * |
||
130 | * @param \ArrayObject $childA |
||
131 | * @param \ArrayObject $childB |
||
132 | * |
||
133 | * @return number |
||
134 | */ |
||
135 | 7 | protected function childComparator($childA, $childB) |
|
155 | |||
156 | /** |
||
157 | * Returns the list of the elements organized by priority |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | 17 | function getElements() |
|
168 | |||
169 | |||
170 | /** |
||
171 | * Unset the group property for elements without a valid group (ie: existing group) |
||
172 | */ |
||
173 | 11 | protected function cleanUpMissingGroups() |
|
182 | |||
183 | } |
||
184 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.