Complex classes like DTOBase 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 DTOBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class DTOBase implements ArrayAccess, IteratorAggregate, Countable |
||
| 10 | { |
||
| 11 | protected $data; |
||
| 12 | protected $default = []; |
||
| 13 | private $serializer = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * DTO constructor. |
||
| 17 | * @param array $default |
||
| 18 | * @param array|object|string $data |
||
| 19 | * @param DTOSerializerInterface $serializer |
||
| 20 | * @throws \InvalidArgumentException |
||
| 21 | */ |
||
| 22 | 38 | public function __construct($default = [], $data = [], DTOSerializerInterface $serializer = null) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Build DTO from given type of data |
||
| 35 | * @param $data |
||
| 36 | * @throws \InvalidArgumentException |
||
| 37 | */ |
||
| 38 | 38 | private function build($data) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Build DTO from provided data |
||
| 65 | * @param object|array $data |
||
| 66 | */ |
||
| 67 | 34 | private function buildFromData($data) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get custom iterator |
||
| 82 | * @return DTOIterator |
||
| 83 | */ |
||
| 84 | 2 | public function getIterator() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Check if offset exists |
||
| 91 | * @param string $offset |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | 2 | public function offsetExists($offset) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Get data at scalar offset or default value instead |
||
| 101 | * @param string $offset |
||
| 102 | * @return mixed |
||
| 103 | * @throws \InvalidArgumentException |
||
| 104 | */ |
||
| 105 | 21 | private function offsetGetScalar($offset) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get data at offset or default value instead |
||
| 116 | * @param string $offset |
||
| 117 | * @return mixed |
||
| 118 | * @throws \InvalidArgumentException |
||
| 119 | */ |
||
| 120 | 9 | public function offsetGet($offset) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Set data at offset |
||
| 127 | * @param string $offset |
||
| 128 | * @param mixed $value |
||
| 129 | */ |
||
| 130 | 4 | public function offsetSet($offset, $value) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Remove data at offset |
||
| 137 | * @param string $offset |
||
| 138 | */ |
||
| 139 | 2 | public function offsetUnset($offset) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Count data elements |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | 2 | public function count() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Get default value at offset if set |
||
| 155 | * @param string $offset |
||
| 156 | * @return mixed |
||
| 157 | * @throws \InvalidArgumentException |
||
| 158 | */ |
||
| 159 | 4 | private function getDefault($offset) |
|
| 167 | |||
| 168 | 5 | public function __get($key) |
|
| 172 | |||
| 173 | 2 | public function __set($key, $value) |
|
| 177 | |||
| 178 | 2 | public function __isset($key) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Get nested values using "dot" notation |
||
| 185 | * @param string $offset |
||
| 186 | * @return mixed |
||
| 187 | * @throws \InvalidArgumentException |
||
| 188 | */ |
||
| 189 | 23 | public function get($offset) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Converts data to string |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 3 | public function __toString() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Serializes the data using serializer |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 3 | private function serialize() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @return DTOSerializerInterface |
||
| 238 | */ |
||
| 239 | 4 | public function getSerializer() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param DTOSerializerInterface $serializer |
||
| 246 | * @return DTOBase |
||
| 247 | */ |
||
| 248 | 1 | public function setSerializer(DTOSerializerInterface $serializer) |
|
| 254 | } |