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 |
||
20 | trait BaseTrait |
||
21 | { |
||
22 | /** |
||
23 | * @var array default items |
||
24 | */ |
||
25 | protected static $_defaults = []; |
||
26 | |||
27 | /** |
||
28 | * @var array items |
||
29 | */ |
||
30 | protected $_items = []; |
||
31 | |||
32 | /** |
||
33 | * Straight put an item. |
||
34 | * @param string $name item name |
||
35 | * @param array $value item value |
||
36 | */ |
||
37 | 4 | public function putItem($name, $value = null) |
|
45 | |||
46 | /** |
||
47 | * Get raw item. |
||
48 | * @param string $name item name |
||
49 | * @return mixed item value |
||
50 | */ |
||
51 | public function rawItem($name, $default = null) |
||
55 | |||
56 | /** |
||
57 | * Adds an item. Doesn't touch if already exists. |
||
58 | * @param string $name item name |
||
59 | * @param array $value item value |
||
60 | * @param string|array $where where to put, @see setItem |
||
61 | * @return $this for chaining |
||
62 | */ |
||
63 | public function addItem($name, $value = null, $where = '') |
||
71 | |||
72 | /** |
||
73 | * Sets an item. Silently resets if already exists and mov. |
||
74 | * @param string $name item name |
||
75 | * @param array $value item value |
||
76 | * @param string|array $where where to put, can be empty, first, last and array of before and after |
||
77 | */ |
||
78 | 8 | public function setItem($name, $value = null, $where = '') |
|
86 | |||
87 | /** |
||
88 | * Returns item by name. |
||
89 | * @param string $name item name |
||
90 | * @param mixed $default default value |
||
91 | * @return mixed item value or default |
||
92 | */ |
||
93 | 5 | public function getItem($name, $default = null) |
|
97 | |||
98 | /** |
||
99 | * Check collection has the item. |
||
100 | * @param string $name item name |
||
101 | * @return bool whether item exist |
||
102 | */ |
||
103 | 10 | public function hasItem($name) |
|
107 | |||
108 | public function mergeItem($name, array $value) |
||
114 | |||
115 | /** |
||
116 | * Check is item set. |
||
117 | * @param string $name item name |
||
118 | * @return bool whether item is set |
||
119 | */ |
||
120 | 1 | public function issetItem($name) |
|
124 | |||
125 | /** |
||
126 | * Delete an item. |
||
127 | * @param $name |
||
128 | */ |
||
129 | 2 | public function unsetItem($name) |
|
133 | |||
134 | /** |
||
135 | * Get specified items as array. |
||
136 | * @param mixed $keys specification |
||
137 | * @return array list of items |
||
138 | */ |
||
139 | 2 | public function getItems($keys = null) |
|
143 | |||
144 | /** |
||
145 | * Straight put items. |
||
146 | * @param array $items list of items |
||
147 | * @see setItem |
||
148 | */ |
||
149 | public function putItems(array $items) |
||
155 | |||
156 | /** |
||
157 | * Adds items to specified place. |
||
158 | * @param array $items list of items |
||
159 | * @param mixed $where |
||
160 | * @see setItem() |
||
161 | */ |
||
162 | 4 | public function setItems($items, $where = '') |
|
177 | |||
178 | /** |
||
179 | * Adds items to specified place. |
||
180 | * Does not touch those items that already exists. |
||
181 | * @param array $items array of items |
||
182 | * @param string|array $where where to add. See [[setItem()]] |
||
183 | * @return $this for chaining |
||
184 | * @see setItem() |
||
185 | */ |
||
186 | public function addItems(array $items, $where = '') |
||
199 | |||
200 | public function mergeItems(array $items) |
||
204 | |||
205 | /** |
||
206 | * Unset specified items. |
||
207 | * @param mixed $keys specification |
||
208 | * @return array list of items |
||
209 | */ |
||
210 | 2 | public function unsetItems($keys = null) |
|
222 | |||
223 | 1 | public function resetItems(array $items) |
|
227 | |||
228 | /** |
||
229 | * Get keys. |
||
230 | * @return array for chaining |
||
231 | */ |
||
232 | 9 | public function keys() |
|
236 | |||
237 | /** |
||
238 | * The default implementation of this method returns [[attributes()]] indexed by the same attribute names. |
||
239 | * @return array the list of field names or field definitions |
||
240 | * @see toArray() |
||
241 | */ |
||
242 | public function fields() |
||
248 | |||
249 | /** |
||
250 | * Returns number of items in the collection. |
||
251 | * @return int |
||
252 | */ |
||
253 | public function count() |
||
257 | |||
258 | /** |
||
259 | * Returns the element at the specified offset. |
||
260 | * This method is required by the SPL interface `ArrayAccess`. |
||
261 | * It is implicitly called when you use something like `$value = $collection[$offset];`. |
||
262 | * @param mixed $offset the offset to retrieve element |
||
263 | * @return mixed the element at the offset, null if no element is found at the offset |
||
264 | */ |
||
265 | public function offsetGet($offset) |
||
269 | |||
270 | /** |
||
271 | * Sets the element at the specified offset. |
||
272 | * This method is required by the SPL interface `ArrayAccess`. |
||
273 | * It is implicitly called when you use something like `$collection[$offset] = $value;`. |
||
274 | * @param int $offset the offset to set element |
||
275 | * @param mixed $value the element value |
||
276 | */ |
||
277 | public function offsetSet($offset, $value) |
||
281 | |||
282 | /** |
||
283 | * Returns whether there is an element at the specified offset. |
||
284 | * This method is required by the SPL interface `ArrayAccess`. |
||
285 | * It is implicitly called when you use something like `isset($collection[$offset])`. |
||
286 | * @param mixed $offset the offset to check on |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function offsetExists($offset) |
||
293 | |||
294 | /** |
||
295 | * Sets the element value at the specified offset to null. |
||
296 | * This method is required by the SPL interface ArrayAccess. |
||
297 | * It is implicitly called when you use something like `unset($collection[$offset])`. |
||
298 | * @param mixed $offset the offset to unset element |
||
299 | */ |
||
300 | public function offsetUnset($offset) |
||
304 | |||
305 | /** |
||
306 | * Method for IteratorAggregate interface. |
||
307 | * Enables foreach'ing the object. |
||
308 | * @return ArrayIterator |
||
309 | */ |
||
310 | 1 | public function getIterator() |
|
314 | } |
||
315 |