Complex classes like BaseTrait 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 BaseTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | trait BaseTrait |
||
25 | { |
||
26 | /** |
||
27 | * @var array default items |
||
28 | */ |
||
29 | protected static $_defaults = []; |
||
30 | |||
31 | /** |
||
32 | * @var array items |
||
33 | */ |
||
34 | protected $_items = []; |
||
35 | |||
36 | /** |
||
37 | * Straight put an item. |
||
38 | * @param string $name item name |
||
39 | * @param array $value item value |
||
40 | */ |
||
41 | 4 | public function putItem($name, $value = null) |
|
49 | |||
50 | /** |
||
51 | * Get raw item. |
||
52 | * @param string $name item name |
||
53 | * @return mixed item value |
||
54 | */ |
||
55 | public function rawItem($name, $default = null) |
||
59 | |||
60 | /** |
||
61 | * Adds an item. Doesn't touch if already exists. |
||
62 | * @param string $name item name |
||
63 | * @param array $value item value |
||
64 | * @param string|array $where where to put, @see setItem |
||
65 | * @return $this for chaining |
||
66 | */ |
||
67 | public function addItem($name, $value = null, $where = '') |
||
75 | |||
76 | /** |
||
77 | * Sets an item. Silently resets if already exists and mov. |
||
78 | * @param string $name item name |
||
79 | * @param array $value item value |
||
80 | * @param string|array $where where to put, can be empty, first, last and array of before and after |
||
81 | */ |
||
82 | 8 | public function setItem($name, $value = null, $where = '') |
|
90 | |||
91 | /** |
||
92 | * Returns item by name. |
||
93 | * @param string $name item name |
||
94 | * @param mixed $default default value |
||
95 | * @return mixed item value or default |
||
96 | */ |
||
97 | 5 | public function getItem($name, $default = null) |
|
101 | |||
102 | /** |
||
103 | * Check collection has the item. |
||
104 | * @param string $name item name |
||
105 | * @return bool whether item exist |
||
106 | */ |
||
107 | 10 | public function hasItem($name) |
|
111 | |||
112 | public function mergeItem($name, array $value) |
||
118 | |||
119 | /** |
||
120 | * Check is item set. |
||
121 | * @param string $name item name |
||
122 | * @return bool whether item is set |
||
123 | */ |
||
124 | 1 | public function issetItem($name) |
|
128 | |||
129 | /** |
||
130 | * Delete an item. |
||
131 | * @param $name |
||
132 | */ |
||
133 | 2 | public function unsetItem($name) |
|
137 | |||
138 | /** |
||
139 | * Get specified items as array. |
||
140 | * @param mixed $keys specification |
||
141 | * @return array list of items |
||
142 | */ |
||
143 | 2 | public function getItems($keys = null) |
|
147 | |||
148 | /** |
||
149 | * Straight put items. |
||
150 | * @param array $items list of items |
||
151 | * @see setItem |
||
152 | */ |
||
153 | public function putItems(array $items) |
||
159 | |||
160 | /** |
||
161 | * Adds items to specified place. |
||
162 | * @param array $items list of items |
||
163 | * @param mixed $where |
||
164 | * @see setItem() |
||
165 | */ |
||
166 | 4 | public function setItems($items, $where = '') |
|
181 | |||
182 | /** |
||
183 | * Adds items to specified place. |
||
184 | * Does not touch those items that already exists. |
||
185 | * @param array $items array of items |
||
186 | * @param string|array $where where to add. See [[setItem()]] |
||
187 | * @return $this for chaining |
||
188 | * @see setItem() |
||
189 | */ |
||
190 | public function addItems(array $items, $where = '') |
||
203 | |||
204 | public function mergeItems(array $items) |
||
208 | |||
209 | /** |
||
210 | * Unset specified items. |
||
211 | * @param mixed $keys specification |
||
212 | * @return array list of items |
||
213 | */ |
||
214 | 2 | public function unsetItems($keys = null) |
|
226 | |||
227 | 1 | public function resetItems(array $items) |
|
231 | |||
232 | /** |
||
233 | * Get keys. |
||
234 | * @return array for chaining |
||
235 | */ |
||
236 | 9 | public function keys() |
|
240 | |||
241 | /** |
||
242 | * The default implementation of this method returns [[attributes()]] indexed by the same attribute names. |
||
243 | * @return array the list of field names or field definitions |
||
244 | * @see toArray() |
||
245 | */ |
||
246 | public function fields() |
||
252 | |||
253 | /** |
||
254 | * Returns number of items in the collection. |
||
255 | * @return int |
||
256 | */ |
||
257 | public function count() |
||
261 | |||
262 | /** |
||
263 | * Returns the element at the specified offset. |
||
264 | * This method is required by the SPL interface `ArrayAccess`. |
||
265 | * It is implicitly called when you use something like `$value = $collection[$offset];`. |
||
266 | * @param mixed $offset the offset to retrieve element |
||
267 | * @return mixed the element at the offset, null if no element is found at the offset |
||
268 | */ |
||
269 | public function offsetGet($offset) |
||
273 | |||
274 | /** |
||
275 | * Sets the element at the specified offset. |
||
276 | * This method is required by the SPL interface `ArrayAccess`. |
||
277 | * It is implicitly called when you use something like `$collection[$offset] = $value;`. |
||
278 | * @param int $offset the offset to set element |
||
279 | * @param mixed $value the element value |
||
280 | */ |
||
281 | public function offsetSet($offset, $value) |
||
285 | |||
286 | /** |
||
287 | * Returns whether there is an element at the specified offset. |
||
288 | * This method is required by the SPL interface `ArrayAccess`. |
||
289 | * It is implicitly called when you use something like `isset($collection[$offset])`. |
||
290 | * @param mixed $offset the offset to check on |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function offsetExists($offset) |
||
297 | |||
298 | /** |
||
299 | * Sets the element value at the specified offset to null. |
||
300 | * This method is required by the SPL interface ArrayAccess. |
||
301 | * It is implicitly called when you use something like `unset($collection[$offset])`. |
||
302 | * @param mixed $offset the offset to unset element |
||
303 | */ |
||
304 | public function offsetUnset($offset) |
||
308 | |||
309 | /** |
||
310 | * Method for IteratorAggregate interface. |
||
311 | * Enables foreach'ing the object. |
||
312 | * @return ArrayIterator |
||
313 | */ |
||
314 | 1 | public function getIterator() |
|
318 | } |
||
319 |