Complex classes like ObjectHydrator 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 ObjectHydrator, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
21 | class ObjectHydrator implements Hydrator |
||
22 | { |
||
23 | /** |
||
24 | * @var AnySchema |
||
25 | */ |
||
26 | private $anySchema; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $is32Bit; |
||
32 | |||
33 | /** |
||
34 | * @var DateTimeSerializer |
||
35 | */ |
||
36 | private $dateTimeSerializer; |
||
37 | |||
38 | /** |
||
39 | * @var ClassNameResolver |
||
40 | */ |
||
41 | private $classNameResolver; |
||
42 | |||
43 | /** |
||
44 | * ObjectHydrator constructor. |
||
45 | * |
||
46 | * @param ClassNameResolver $classNameResolver |
||
47 | * @param DateTimeSerializer $dateTimeSerializer |
||
48 | * @param bool $is32Bit |
||
49 | */ |
||
50 | public function __construct( |
||
60 | |||
61 | /** |
||
62 | * @param mixed $data |
||
63 | * @param Schema $schema |
||
64 | * |
||
65 | * @return mixed |
||
66 | */ |
||
67 | public function hydrate($data, Schema $schema) |
||
71 | |||
72 | /** |
||
73 | * @param mixed $data |
||
74 | * @param Schema $schema |
||
75 | * |
||
76 | * @return mixed |
||
77 | */ |
||
78 | public function dehydrate($data, Schema $schema) |
||
82 | |||
83 | /** |
||
84 | * @param mixed $node |
||
85 | * @param Schema $schema |
||
86 | * |
||
87 | * @return mixed |
||
88 | */ |
||
89 | private function hydrateNode($node, Schema $schema) |
||
155 | |||
156 | /** |
||
157 | * @param mixed $node |
||
158 | * @param Schema $schema |
||
159 | * |
||
160 | * @return mixed |
||
161 | */ |
||
162 | private function dehydrateNode($node, Schema $schema) |
||
203 | |||
204 | /** |
||
205 | * Cast a scalar value using the schema. |
||
206 | * |
||
207 | * @param mixed $value |
||
208 | * @param ScalarSchema $schema |
||
209 | * |
||
210 | * @return float|int|string|\DateTimeInterface |
||
211 | * @throws UnsupportedException |
||
212 | */ |
||
213 | private function castScalarValue($value, ScalarSchema $schema) |
||
233 | |||
234 | /** |
||
235 | * @param mixed $node |
||
236 | * @param Schema $schema |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | private function shouldTreatAsObject($node, Schema $schema): bool |
||
244 | |||
245 | /** |
||
246 | * @param mixed $node |
||
247 | * @param Schema $schema |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | private function shouldTreatAsArray($node, Schema $schema): bool |
||
255 | |||
256 | /** |
||
257 | * @param mixed $value |
||
258 | * @param Schema $schema |
||
259 | * @return bool |
||
260 | */ |
||
261 | private function isAllowedNull($value, Schema $schema): bool |
||
265 | |||
266 | /** |
||
267 | * @param mixed $node |
||
268 | * @param Schema $schema |
||
269 | * |
||
270 | * @return mixed |
||
271 | */ |
||
272 | private function applyDefaults($node, Schema $schema) |
||
285 | |||
286 | /** |
||
287 | * @param $node |
||
288 | * @return \stdClass |
||
289 | */ |
||
290 | private function extractValuesFromTypedObject($node): array |
||
302 | } |
||
303 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.