Total Complexity | 43 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like EntityHydrator 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.
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 EntityHydrator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class EntityHydrator extends DoctrineObject |
||
23 | { |
||
24 | /** |
||
25 | * @var \ReflectionClass<object>|null |
||
26 | */ |
||
27 | private ?\ReflectionClass $reflectionClass = null; |
||
28 | |||
29 | /** |
||
30 | * @param object $object |
||
31 | * @param mixed $collectionName |
||
32 | * @param class-string<object> $target |
||
|
|||
33 | * @param array<mixed>|null $values |
||
34 | * |
||
35 | * @return void |
||
36 | * |
||
37 | * @throws RuntimeException |
||
38 | * @throws InvalidArgumentException |
||
39 | * @throws \InvalidArgumentException |
||
40 | * @throws \ReflectionException |
||
41 | */ |
||
42 | protected function toMany($object, $collectionName, $target, $values): void |
||
43 | { |
||
44 | if (!is_iterable($values)) { |
||
45 | $values = (array)$values; |
||
46 | } |
||
47 | |||
48 | $metadata = $this->objectManager->getClassMetadata($target); |
||
49 | $identifier = $metadata->getIdentifier(); |
||
50 | $collection = []; |
||
51 | |||
52 | // If the collection contains identifiers, fetch the objects from database |
||
53 | foreach ($values as $value) { |
||
54 | if ($value instanceof $target) { |
||
55 | // Assumes modifications have already taken place in object |
||
56 | $collection[] = $value; |
||
57 | continue; |
||
58 | } |
||
59 | |||
60 | if (empty($value)) { |
||
61 | // Assumes no id and retrieves new $target |
||
62 | $collection[] = $this->find($value, $target); |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $find = $this->getFindCriteria($identifier, $value); |
||
67 | |||
68 | if (!empty($find) && $found = $this->find($find, $target)) { |
||
69 | $collection[] = is_array($value) ? $this->hydrate($value, $found) : $found; |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | $newTarget = $this->createTargetEntity($target); |
||
74 | $collection[] = is_array($value) ? $this->hydrate($value, $newTarget) : $newTarget; |
||
75 | } |
||
76 | |||
77 | $collection = array_filter( |
||
78 | $collection, |
||
79 | static fn ($item) => null !== $item |
||
80 | ); |
||
81 | |||
82 | /** @var AbstractCollectionStrategy $collectionStrategy */ |
||
83 | $collectionStrategy = $this->getStrategy($collectionName); |
||
84 | $collectionStrategy->setObject($object); |
||
85 | |||
86 | $this->hydrateValue($collectionName, $collection, $values); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $className |
||
91 | * |
||
92 | * @return object |
||
93 | * |
||
94 | * @throws RuntimeException |
||
95 | * @throws \ReflectionException |
||
96 | */ |
||
97 | private function createTargetEntity(string $className): object |
||
98 | { |
||
99 | return $this->createReflectionClass($className)->newInstanceWithoutConstructor(); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Copied from parent to check for isInitialisedFieldName() |
||
104 | * |
||
105 | * @param object $object |
||
106 | * |
||
107 | * @return array<string, mixed> |
||
108 | * |
||
109 | * @throws RuntimeException |
||
110 | * @throws \ReflectionException |
||
111 | */ |
||
112 | public function extractByValue($object): array |
||
113 | { |
||
114 | $fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames()); |
||
115 | $methods = get_class_methods($object); |
||
116 | $filter = $object instanceof FilterProviderInterface |
||
117 | ? $object->getFilter() |
||
118 | : $this->filterComposite; |
||
119 | |||
120 | $data = []; |
||
121 | foreach ($fieldNames as $fieldName) { |
||
122 | if (!$this->isInitialisedFieldName($object, $fieldName)) { |
||
123 | continue; |
||
124 | } |
||
125 | if ($filter && !$filter->filter($fieldName)) { |
||
126 | continue; |
||
127 | } |
||
128 | |||
129 | $getter = 'get' . ucfirst($fieldName); |
||
130 | $isser = 'is' . ucfirst($fieldName); |
||
131 | |||
132 | $dataFieldName = $this->computeExtractFieldName($fieldName); |
||
133 | if (in_array($getter, $methods, true)) { |
||
134 | $data[$dataFieldName] = $this->extractValue($fieldName, $object->$getter(), $object); |
||
135 | } elseif (in_array($isser, $methods, true)) { |
||
136 | $data[$dataFieldName] = $this->extractValue($fieldName, $object->$isser(), $object); |
||
137 | } elseif ( |
||
138 | str_starts_with($fieldName, 'is') |
||
139 | && in_array($fieldName, $methods, true) |
||
140 | && ctype_upper($fieldName[2]) |
||
141 | ) { |
||
142 | $data[$dataFieldName] = $this->extractValue($fieldName, $object->$fieldName(), $object); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return $data; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Check if the provided $fieldName is initialised for the given $object |
||
151 | * |
||
152 | * @throws RuntimeException |
||
153 | */ |
||
154 | protected function isInitialisedFieldName(object $object, string $fieldName): bool |
||
155 | { |
||
156 | if ($object instanceof Proxy) { |
||
157 | return true; |
||
158 | } |
||
159 | return $this->getReflectionProperty($object, $fieldName)->isInitialized($object); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param object $object |
||
164 | * @param string $fieldName |
||
165 | * |
||
166 | * @return \ReflectionProperty |
||
167 | * |
||
168 | * @throws RuntimeException |
||
169 | */ |
||
170 | private function getReflectionProperty(object $object, string $fieldName): \ReflectionProperty |
||
171 | { |
||
172 | $className = get_class($object); |
||
173 | $reflectionClass = $this->getReflectionClass($className); |
||
174 | |||
175 | if (!$reflectionClass->hasProperty($fieldName)) { |
||
176 | throw new RuntimeException( |
||
177 | sprintf( |
||
178 | 'The hydration property \'%s\' could not be found for class \'%s\'', |
||
179 | $fieldName, |
||
180 | $className |
||
181 | ) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | try { |
||
186 | $property = $reflectionClass->getProperty($fieldName); |
||
187 | } catch (\Throwable $e) { |
||
188 | throw new RuntimeException( |
||
189 | sprintf( |
||
190 | 'The hydration property \'%s\' could not be loaded for class \'%s\': %s', |
||
191 | $fieldName, |
||
192 | $className, |
||
193 | $e->getMessage() |
||
194 | ), |
||
195 | $e->getCode(), |
||
196 | $e |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | return $property; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param string $className |
||
205 | * |
||
206 | * @return \ReflectionClass<object> |
||
207 | * |
||
208 | * @throws RuntimeException |
||
209 | */ |
||
210 | private function getReflectionClass(string $className): \ReflectionClass |
||
211 | { |
||
212 | if (null !== $this->reflectionClass && $this->reflectionClass->getName() === $className) { |
||
213 | return $this->reflectionClass; |
||
214 | } |
||
215 | $this->reflectionClass = $this->createReflectionClass($className); |
||
216 | return $this->reflectionClass; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param array<string|object|array|mixed> $identifier |
||
221 | * @param mixed $value |
||
222 | * |
||
223 | * @return array<string|int, mixed> |
||
224 | */ |
||
225 | protected function getFindCriteria(array $identifier, mixed $value): array |
||
226 | { |
||
227 | $find = []; |
||
228 | foreach ($identifier as $field) { |
||
229 | if (is_object($value)) { |
||
230 | $getter = 'get' . ucfirst($field); |
||
231 | |||
232 | if (is_callable([$value, $getter])) { |
||
233 | $find[$field] = $value->$getter(); |
||
234 | } elseif (property_exists($value, $field)) { |
||
235 | $find[$field] = $value->{$field}; |
||
236 | } |
||
237 | continue; |
||
238 | } |
||
239 | |||
240 | if (is_array($value)) { |
||
241 | if (isset($value[$field])) { |
||
242 | $find[$field] = $value[$field]; |
||
243 | unset($value[$field]); |
||
244 | } |
||
245 | continue; |
||
246 | } |
||
247 | |||
248 | $find[$field] = $value; |
||
249 | } |
||
250 | |||
251 | return $find; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param string $className |
||
256 | * |
||
257 | * @return \ReflectionClass<object> |
||
258 | * |
||
259 | * @throws RuntimeException |
||
260 | */ |
||
261 | private function createReflectionClass(string $className): \ReflectionClass |
||
262 | { |
||
263 | if (!class_exists($className)) { |
||
264 | throw new RuntimeException( |
||
265 | sprintf( |
||
266 | 'The hydrator was unable to create a reflection instance for class \'%s\': %s', |
||
267 | 'The class could not be found', |
||
268 | $className, |
||
269 | ) |
||
270 | ); |
||
271 | } |
||
272 | |||
273 | return new \ReflectionClass($className); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param mixed $value |
||
278 | * @param string $typeOfField |
||
279 | */ |
||
280 | protected function handleTypeConversions(mixed $value, $typeOfField): mixed |
||
291 | } |
||
292 | } |
||
293 |