Complex classes like ArrayAccessTrait 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 ArrayAccessTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait ArrayAccessTrait |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The internal state that this object represents |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $internalState = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Sets an internal key with a value. |
||
| 21 | * |
||
| 22 | * @param string $offset |
||
| 23 | * @param mixed $value |
||
| 24 | */ |
||
| 25 | 5 | public function offsetSet($offset, $value) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Checks whether an internal key exists. |
||
| 36 | * |
||
| 37 | * @param string $offset |
||
| 38 | * |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | 3 | public function offsetExists($offset) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Unsets an internal key. |
||
| 48 | * |
||
| 49 | * @param string $offset |
||
| 50 | */ |
||
| 51 | 1 | public function offsetUnset($offset) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Retrieves an internal key. |
||
| 58 | * |
||
| 59 | * @param string $offset |
||
| 60 | * |
||
| 61 | * @return mixed|null |
||
| 62 | */ |
||
| 63 | 2 | public function offsetGet($offset) |
|
| 67 | } |
||
| 68 |