Total Complexity | 43 |
Total Lines | 383 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like FieldResolver 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 FieldResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class FieldResolver |
||
19 | { |
||
20 | use GetSetSearchResolverTrait; |
||
21 | /** |
||
22 | * @var array Class Ancestry |
||
23 | */ |
||
24 | protected static $ancestry = []; |
||
25 | /** |
||
26 | * @var array Class Hierarchy, could be replaced with Ancestry |
||
27 | */ |
||
28 | protected static $hierarchy = []; |
||
29 | |||
30 | /** |
||
31 | * Check if class is subclass of (a) the class in $instanceOf, or (b) any of the classes in the array $instanceOf |
||
32 | * |
||
33 | * @param string $class Name of the class to test |
||
34 | * @param array|string $instanceOf Class ancestry it should be in |
||
35 | * @return bool |
||
36 | * @todo remove in favour of DataObjectSchema |
||
37 | * @static |
||
38 | */ |
||
39 | public static function isSubclassOf($class, $instanceOf): bool |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Resolve a field ancestry |
||
50 | * |
||
51 | * @param $field |
||
52 | * @return array |
||
53 | * @throws Exception |
||
54 | * |
||
55 | */ |
||
56 | public function resolveField($field) |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get the sources to build in to a Solr field |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getBuildSources(): array |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get the next lookup item from the buildSources |
||
98 | * |
||
99 | * @param array $buildSources |
||
100 | * @param $lookup |
||
101 | * @return array |
||
102 | * @throws Exception |
||
103 | */ |
||
104 | protected function getNext(array $buildSources, $lookup): array |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Resolve relations if possible |
||
120 | * |
||
121 | * @param string $source |
||
122 | * @param $lookup |
||
123 | * @param array $next |
||
124 | * @param array $options |
||
125 | * @return array |
||
126 | * @throws ReflectionException |
||
127 | * @throws Exception |
||
128 | */ |
||
129 | protected function resolveRelation($source, $lookup, array $next, array &$options): array |
||
130 | { |
||
131 | $source = $this->getSourceName($source); |
||
132 | |||
133 | foreach (self::getHierarchy($source) as $dataClass) { |
||
134 | $schema = DataObject::getSchema(); |
||
135 | $options['multi_valued'] = false; |
||
136 | |||
137 | $class = $this->getRelationData($lookup, $schema, $dataClass, $options); |
||
138 | |||
139 | if (is_string($class) && $class) { |
||
140 | if (!isset($options['origin'])) { |
||
141 | $options['origin'] = $source; |
||
142 | } |
||
143 | |||
144 | // we add suffix here to prevent the relation to be overwritten by other instances |
||
145 | // all sources lookups must clean the source name before reading it via getSourceName() |
||
146 | $next[$class . '|xkcd|' . $dataClass] = $options; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return $next; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * This is used to clean the source name from suffix |
||
155 | * suffixes are needed to support multiple relations with the same name on different page types |
||
156 | * |
||
157 | * @param string $source |
||
158 | * @return string |
||
159 | */ |
||
160 | private function getSourceName($source) |
||
161 | { |
||
162 | $explodedSource = explode('|xkcd|', $source); |
||
163 | |||
164 | return $explodedSource[0]; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get all the classes involved in a DataObject hierarchy - both super and optionally subclasses |
||
169 | * |
||
170 | * @static |
||
171 | * @param string $class - The class to query |
||
172 | * @param bool $includeSubclasses - True to return subclasses as well as super classes |
||
173 | * @param bool $dataOnly - True to only return classes that have tables |
||
174 | * @return array - Integer keys, String values as classes sorted by depth (most super first) |
||
175 | * @throws ReflectionException |
||
176 | */ |
||
177 | public static function getHierarchy($class, $includeSubclasses = true, $dataOnly = false): array |
||
178 | { |
||
179 | // Generate the unique key for this class and it's call type |
||
180 | // It's a short-lived cache key for the duration of the request |
||
181 | $cacheKey = sprintf('%s-%s-%s', $class, $includeSubclasses ? 'sc' : 'an', $dataOnly ? 'do' : 'al'); |
||
182 | |||
183 | if (!isset(self::$hierarchy[$cacheKey])) { |
||
184 | $classes = self::getHierarchyClasses($class, $includeSubclasses); |
||
185 | |||
186 | if ($dataOnly) { |
||
187 | $classes = array_filter($classes, static function ($class) { |
||
188 | return DataObject::getSchema()->classHasTable($class); |
||
189 | }); |
||
190 | } |
||
191 | |||
192 | self::$hierarchy[$cacheKey] = array_values($classes); |
||
193 | |||
194 | return array_values($classes); |
||
195 | } |
||
196 | |||
197 | return self::$hierarchy[$cacheKey]; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get the hierarchy for a class |
||
202 | * |
||
203 | * @param $class |
||
204 | * @param $includeSubclasses |
||
205 | * @return array |
||
206 | * @throws ReflectionException |
||
207 | * @todo clean this up to be more compatible with PHP features |
||
208 | */ |
||
209 | protected static function getHierarchyClasses($class, $includeSubclasses): array |
||
210 | { |
||
211 | $classes = array_values(ClassInfo::ancestry($class)); |
||
212 | $classes = self::getSubClasses($class, $includeSubclasses, $classes); |
||
213 | |||
214 | $classes = array_unique($classes); |
||
215 | $classes = self::excludeDataObjectIDx($classes); |
||
216 | |||
217 | return $classes; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get the subclasses for the given class |
||
222 | * Should be replaced with PHP native methods |
||
223 | * |
||
224 | * @param $class |
||
225 | * @param $includeSubclasses |
||
226 | * @param array $classes |
||
227 | * @return array |
||
228 | * @throws ReflectionException |
||
229 | */ |
||
230 | private static function getSubClasses($class, $includeSubclasses, array $classes): array |
||
231 | { |
||
232 | if ($includeSubclasses) { |
||
233 | $subClasses = ClassInfo::subclassesFor($class); |
||
234 | $classes = array_merge($classes, array_values($subClasses)); |
||
235 | } |
||
236 | |||
237 | return $classes; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Objects to exclude from the index |
||
242 | * |
||
243 | * @param array $classes |
||
244 | * @return array |
||
245 | */ |
||
246 | private static function excludeDataObjectIDx(array $classes): array |
||
247 | { |
||
248 | // Remove all classes below DataObject from the list |
||
249 | $idx = array_search(DataObject::class, $classes, true); |
||
250 | if ($idx !== false) { |
||
251 | array_splice($classes, 0, $idx + 1); |
||
252 | } |
||
253 | |||
254 | return $classes; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Relational data |
||
259 | * |
||
260 | * @param $lookup |
||
261 | * @param DataObjectSchema $schema |
||
262 | * @param $className |
||
263 | * @param array $options |
||
264 | * @return string|array|null |
||
265 | * @throws Exception |
||
266 | */ |
||
267 | protected function getRelationData($lookup, DataObjectSchema $schema, $className, array &$options) |
||
268 | { |
||
269 | if ($hasOne = $schema->hasOneComponent($className, $lookup)) { |
||
270 | return $hasOne; |
||
271 | } |
||
272 | $options['multi_valued'] = true; |
||
273 | if ($hasMany = $schema->hasManyComponent($className, $lookup)) { |
||
274 | return $hasMany; |
||
275 | } |
||
276 | if ($key = $schema->manyManyComponent($className, $lookup)) { |
||
277 | return $key['childClass']; |
||
278 | } |
||
279 | |||
280 | return null; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Create field options for the given index field |
||
285 | * |
||
286 | * @param $field |
||
287 | * @param array $sources |
||
288 | * @param string $fullfield |
||
289 | * @param array $found |
||
290 | * @return array |
||
291 | * @throws ReflectionException |
||
292 | */ |
||
293 | protected function getFieldOptions($field, array $sources, $fullfield, array $found): array |
||
294 | { |
||
295 | foreach ($sources as $class => $fieldOptions) { |
||
296 | $class = $this->getSourceName($class); |
||
297 | $dataclasses = self::getHierarchy($class); |
||
298 | |||
299 | $fields = DataObject::getSchema()->databaseFields($class); |
||
300 | while ($dataclass = array_shift($dataclasses)) { |
||
301 | $type = $this->getType($fields, $field, $dataclass); |
||
302 | |||
303 | if ($type) { |
||
304 | // Don't search through child classes of a class we matched on. |
||
305 | $dataclasses = array_diff($dataclasses, array_values(ClassInfo::subclassesFor($dataclass))); |
||
306 | // Trim arguments off the type string |
||
307 | if (preg_match('/^(\w+)\(/', $type, $match)) { |
||
308 | $type = $match[1]; |
||
309 | } |
||
310 | |||
311 | $found = $this->getFoundOriginData($field, $fullfield, $fieldOptions, $dataclass, $type, $found); |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | |||
316 | return $found; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Get the type of this field |
||
321 | * |
||
322 | * @param array $fields |
||
323 | * @param string $field |
||
324 | * @param string $dataclass |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function getType($fields, $field, $dataclass): string |
||
328 | { |
||
329 | if (!empty($fields[$field])) { |
||
330 | return $fields[$field]; |
||
331 | } |
||
332 | |||
333 | /** @var DataObject $singleton */ |
||
334 | $singleton = singleton($dataclass); |
||
335 | |||
336 | $type = $singleton->castingClass($field); |
||
337 | |||
338 | if (!$type) { |
||
339 | // @todo should this be null? |
||
340 | $type = 'String'; |
||
341 | } |
||
342 | |||
343 | return $type; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * FoundOriginData is a helper to make sure the options are properly set. |
||
348 | * |
||
349 | * @param string $field |
||
350 | * @param string $fullField |
||
351 | * @param array $fieldOptions |
||
352 | * @param string $dataclass |
||
353 | * @param string $type |
||
354 | * @param array $found |
||
355 | * @return array |
||
356 | */ |
||
357 | private function getFoundOriginData( |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param array $next |
||
383 | * @param array|string $class |
||
384 | * @param array $options |
||
385 | * @param string $dataClass |
||
386 | * @return array |
||
387 | */ |
||
388 | protected function getNextOption(array $next, $class, array $options, $dataClass): array |
||
401 | } |
||
402 | } |
||
403 |