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 | 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 | private function build($data) |
||
62 | |||
63 | /** |
||
64 | * Build DTO from provided data |
||
65 | * @param array $data |
||
66 | */ |
||
67 | private function buildFromData($data) |
||
79 | |||
80 | /** |
||
81 | * Get custom iterator |
||
82 | * @return DTOIterator |
||
83 | */ |
||
84 | public function getIterator() |
||
88 | |||
89 | /** |
||
90 | * Check if offset exists |
||
91 | * @param string $offset |
||
92 | * @return bool |
||
93 | */ |
||
94 | 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 | 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 | public function offsetGet($offset) |
||
124 | |||
125 | /** |
||
126 | * Set data at offset |
||
127 | * @param string $offset |
||
128 | * @param mixed $value |
||
129 | */ |
||
130 | public function offsetSet($offset, $value) |
||
134 | |||
135 | /** |
||
136 | * Remove data at offset |
||
137 | * @param string $offset |
||
138 | */ |
||
139 | public function offsetUnset($offset) |
||
143 | |||
144 | /** |
||
145 | * Count data elements |
||
146 | * @return int |
||
147 | */ |
||
148 | 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 | private function getDefault($offset) |
||
167 | |||
168 | public function __get($key) |
||
172 | |||
173 | public function __set($key, $value) |
||
177 | |||
178 | public function __isset($key) |
||
182 | |||
183 | /** |
||
184 | * Get nested values using "dot" notation |
||
185 | * @param $offset |
||
186 | * @return mixed |
||
187 | * @throws \InvalidArgumentException |
||
188 | */ |
||
189 | public function get($offset) |
||
209 | |||
210 | /** |
||
211 | * Converts data to string |
||
212 | * @return string |
||
213 | */ |
||
214 | public function __toString() |
||
218 | |||
219 | /** |
||
220 | * Serializes the data using serializer |
||
221 | * @return string |
||
222 | */ |
||
223 | private function serialize() |
||
231 | |||
232 | /** |
||
233 | * @return DTOSerializerInterface |
||
234 | */ |
||
235 | public function getSerializer() |
||
239 | |||
240 | /** |
||
241 | * @param DTOSerializerInterface $serializer |
||
242 | * @return DTOBase |
||
243 | */ |
||
244 | public function setSerializer(DTOSerializerInterface $serializer) |
||
250 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: