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) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Build DTO from provided data |
||
| 55 | * @param object|array $data |
||
| 56 | */ |
||
| 57 | 34 | private function buildFromData($data) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get custom iterator |
||
| 72 | * @return DTOIterator |
||
| 73 | */ |
||
| 74 | 2 | public function getIterator() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Check if offset exists |
||
| 81 | * @param string $offset |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 2 | public function offsetExists($offset) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get data at scalar offset or default value instead |
||
| 91 | * @param string $offset |
||
| 92 | * @return mixed |
||
| 93 | * @throws \InvalidArgumentException |
||
| 94 | */ |
||
| 95 | 21 | private function offsetGetScalar($offset) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get data at offset or default value instead |
||
| 106 | * @param string $offset |
||
| 107 | * @return mixed |
||
| 108 | * @throws \InvalidArgumentException |
||
| 109 | */ |
||
| 110 | 9 | public function offsetGet($offset) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Set data at offset |
||
| 117 | * @param string $offset |
||
| 118 | * @param mixed $value |
||
| 119 | */ |
||
| 120 | 4 | public function offsetSet($offset, $value) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Remove data at offset |
||
| 127 | * @param string $offset |
||
| 128 | */ |
||
| 129 | 2 | public function offsetUnset($offset) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Count data elements |
||
| 136 | * @return int |
||
| 137 | */ |
||
| 138 | 2 | public function count() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get default value at offset if set |
||
| 145 | * @param string $offset |
||
| 146 | * @return mixed |
||
| 147 | * @throws \InvalidArgumentException |
||
| 148 | */ |
||
| 149 | 4 | private function getDefault($offset) |
|
| 157 | |||
| 158 | 5 | public function __get($key) |
|
| 162 | |||
| 163 | 2 | public function __set($key, $value) |
|
| 167 | |||
| 168 | 2 | public function __isset($key) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get nested values using "dot" notation |
||
| 175 | * @param string $offset |
||
| 176 | * @return mixed |
||
| 177 | * @throws \InvalidArgumentException |
||
| 178 | */ |
||
| 179 | 23 | public function get($offset) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Converts data to string |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 3 | public function __toString() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Serializes the data using serializer |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 3 | private function serialize() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return DTOSerializerInterface |
||
| 224 | */ |
||
| 225 | 4 | public function getSerializer() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param DTOSerializerInterface $serializer |
||
| 232 | * @return DTOBase |
||
| 233 | */ |
||
| 234 | 1 | public function setSerializer(DTOSerializerInterface $serializer) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Try to build from provided string as JSON |
||
| 243 | * @param string $data |
||
| 244 | * @throws \InvalidArgumentException |
||
| 245 | */ |
||
| 246 | 6 | private function buildFromJson($data) |
|
| 258 | } |