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 |
||
| 21 | trait BaseTrait |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var array default items |
||
| 25 | */ |
||
| 26 | protected static $_defaults = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array items |
||
| 30 | */ |
||
| 31 | protected $_items = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Straight put an item. |
||
| 35 | * |
||
| 36 | * @param string $name item name. |
||
| 37 | * @param array $value item value. |
||
| 38 | */ |
||
| 39 | 4 | public function putItem($name, $value = null) |
|
| 40 | { |
||
| 41 | if (is_null($name) || is_int($name)) { |
||
| 42 | $this->_items[] = $value; |
||
| 43 | } else { |
||
| 44 | 4 | $this->_items[$name] = $value; |
|
| 45 | } |
||
| 46 | 4 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Get raw item. |
||
| 50 | * |
||
| 51 | * @param string $name item name. |
||
| 52 | * |
||
| 53 | * @return mixed item value. |
||
| 54 | */ |
||
| 55 | public function rawItem($name, $default = null) |
||
| 56 | { |
||
| 57 | return isset($this->_items[$name]) ? $this->_items[$name] : $default; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Adds an item. Doesn't touch if already exists. |
||
| 62 | * |
||
| 63 | * @param string $name item name. |
||
| 64 | * @param array $value item value. |
||
| 65 | * @param string|array $where where to put, @see setItem |
||
| 66 | * |
||
| 67 | * @return $this for chaining |
||
| 68 | */ |
||
| 69 | public function addItem($name, $value = null, $where = '') |
||
| 70 | { |
||
| 71 | if (!$this->hasItem($name)) { |
||
| 72 | $this->setItem($name, $value, $where); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $this; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sets an item. Silently resets if already exists and mov. |
||
| 80 | * |
||
| 81 | * @param string $name item name. |
||
| 82 | * @param array $value item value. |
||
| 83 | * @param string|array $where where to put, can be empty, first, last and array of before and after |
||
| 84 | */ |
||
| 85 | 8 | public function setItem($name, $value = null, $where = '') |
|
| 86 | { |
||
| 87 | 8 | if ($name === null || $where === '') { |
|
| 88 | $this->putItem($name, $value); |
||
| 89 | } else { |
||
| 90 | $this->setItems([$name => $value], $where); |
||
| 91 | 4 | } |
|
| 92 | 6 | } |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Returns item by name. |
||
| 96 | * |
||
| 97 | * @param string $name item name. |
||
| 98 | * |
||
| 99 | * @return mixed item value. |
||
| 100 | */ |
||
| 101 | 5 | public function getItem($name) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Check collection has the item. |
||
| 108 | * |
||
| 109 | * @param string $name item name. |
||
| 110 | * |
||
| 111 | * @return bool whether item exist. |
||
| 112 | */ |
||
| 113 | public function hasItem($name) |
||
| 114 | { |
||
| 115 | return array_key_exists($name, $this->_items); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function mergeItem($name, array $value) |
||
| 119 | { |
||
| 120 | if (!is_null($name)) { |
||
| 121 | $this->_items[$name] = ArrayHelper::merge($this->_items[$name], $value); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Check is item set. |
||
| 127 | * |
||
| 128 | * @param string $name item name. |
||
| 129 | * |
||
| 130 | * @return bool whether item is set. |
||
| 131 | */ |
||
| 132 | public function issetItem($name) |
||
| 133 | { |
||
| 134 | return isset($this->_items[$name]); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Delete an item. |
||
| 139 | * |
||
| 140 | * @param $name |
||
| 141 | */ |
||
| 142 | 1 | public function unsetItem($name) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Get specified items as array. |
||
| 149 | * |
||
| 150 | * @param mixed $keys specification |
||
| 151 | * |
||
| 152 | * @return array list of items |
||
| 153 | */ |
||
| 154 | 2 | public function getItems($keys = null) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Straight put items. |
||
| 161 | * @param array $items list of items |
||
| 162 | * @see setItem |
||
| 163 | */ |
||
| 164 | public function putItems(array $items) |
||
| 165 | { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Adds items to specified place. |
||
| 173 | * @param array $items list of items |
||
| 174 | * @param mixed $where |
||
| 175 | * @see setItem() |
||
| 176 | */ |
||
| 177 | 4 | public function setItems($items, $where = '') |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Adds items to specified place. |
||
| 195 | * Does not touch those items that already exists. |
||
| 196 | * @param array $items array of items. |
||
| 197 | * @param string|array $where where to add. See [[setItem()]] |
||
| 198 | * @return $this for chaining |
||
| 199 | * |
||
| 200 | * @see setItem() |
||
| 201 | */ |
||
| 202 | public function addItems(array $items, $where = '') |
||
| 215 | |||
| 216 | public function mergeItems(array $items) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Unset specified items. |
||
| 223 | * @param mixed $keys specification |
||
| 224 | * @return array list of items |
||
| 225 | */ |
||
| 226 | 2 | public function unsetItems($keys = null) |
|
| 238 | |||
| 239 | 1 | public function resetItems(array $items) |
|
| 243 | /** |
||
| 244 | * Get keys. |
||
| 245 | * |
||
| 246 | * @return array for chaining |
||
| 247 | */ |
||
| 248 | public function keys() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * The default implementation of this method returns [[attributes()]] indexed by the same attribute names. |
||
| 255 | * |
||
| 256 | * @return array the list of field names or field definitions. |
||
| 257 | * |
||
| 258 | * @see toArray() |
||
| 259 | */ |
||
| 260 | public function fields() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns number of items in the collection. |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function count() |
||
| 276 | /** |
||
| 277 | * Returns the element at the specified offset. |
||
| 278 | * This method is required by the SPL interface `ArrayAccess`. |
||
| 279 | * It is implicitly called when you use something like `$value = $collection[$offset];`. |
||
| 280 | * |
||
| 281 | * @param mixed $offset the offset to retrieve element. |
||
| 282 | * |
||
| 283 | * @return mixed the element at the offset, null if no element is found at the offset |
||
| 284 | */ |
||
| 285 | public function offsetGet($offset) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Sets the element at the specified offset. |
||
| 292 | * This method is required by the SPL interface `ArrayAccess`. |
||
| 293 | * It is implicitly called when you use something like `$collection[$offset] = $value;`. |
||
| 294 | * |
||
| 295 | * @param int $offset the offset to set element |
||
| 296 | * @param mixed $value the element value |
||
| 297 | */ |
||
| 298 | public function offsetSet($offset, $value) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns whether there is an element at the specified offset. |
||
| 305 | * This method is required by the SPL interface `ArrayAccess`. |
||
| 306 | * It is implicitly called when you use something like `isset($collection[$offset])`. |
||
| 307 | * |
||
| 308 | * @param mixed $offset the offset to check on |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | public function offsetExists($offset) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Sets the element value at the specified offset to null. |
||
| 319 | * This method is required by the SPL interface ArrayAccess. |
||
| 320 | * It is implicitly called when you use something like `unset($collection[$offset])`. |
||
| 321 | * |
||
| 322 | * @param mixed $offset the offset to unset element |
||
| 323 | */ |
||
| 324 | public function offsetUnset($offset) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Method for IteratorAggregate interface. |
||
| 331 | * Enables foreach'ing the object. |
||
| 332 | * |
||
| 333 | * @return ArrayIterator |
||
| 334 | */ |
||
| 335 | public function getIterator() |
||
| 339 | } |
||
| 340 |