Total Complexity | 65 |
Total Lines | 325 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ObjectParserAutocomplete 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 ObjectParserAutocomplete, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | abstract class ObjectParserAutocomplete extends Autocomplete implements ObjectParserInterface |
||
31 | { |
||
32 | // Constants |
||
33 | // ========================================================================= |
||
34 | |||
35 | const EXCLUDED_PROPERTY_NAMES = [ |
||
36 | 'controller', |
||
37 | 'Controller', |
||
38 | 'CraftEdition', |
||
39 | 'CraftSolo', |
||
40 | 'CraftPro', |
||
41 | ]; |
||
42 | const EXCLUDED_BEHAVIOR_NAMES = [ |
||
43 | 'fieldHandles', |
||
44 | 'hasMethods', |
||
45 | 'owner', |
||
46 | ]; |
||
47 | const EXCLUDED_PROPERTY_REGEXES = [ |
||
48 | '^_', |
||
49 | ]; |
||
50 | const EXCLUDED_METHOD_REGEXES = [ |
||
51 | '^_', |
||
52 | ]; |
||
53 | const RECURSION_DEPTH_LIMIT = 10; |
||
54 | |||
55 | const CUSTOM_PROPERTY_SORT_PREFIX = '~'; |
||
56 | const PROPERTY_SORT_PREFIX = '~~'; |
||
57 | const METHOD_SORT_PREFIX = '~~~'; |
||
58 | |||
59 | // Public Methods |
||
60 | // ========================================================================= |
||
61 | |||
62 | /** |
||
63 | * @inerhitdoc |
||
64 | */ |
||
65 | public function parseObject(string $name, $object, int $recursionDepth, string $path = ''): void |
||
66 | { |
||
67 | // Only recurse `RECURSION_DEPTH_LIMIT` deep |
||
68 | if ($recursionDepth > self::RECURSION_DEPTH_LIMIT) { |
||
69 | return; |
||
70 | } |
||
71 | $recursionDepth++; |
||
72 | // Create the docblock factory |
||
73 | $factory = DocBlockFactory::createInstance(); |
||
74 | |||
75 | $path = trim(implode('.', [$path, $name]), '.'); |
||
76 | // The class itself |
||
77 | $this->getClassCompletion($object, $factory, $name, $path); |
||
78 | // ServiceLocator Components |
||
79 | $this->getComponentCompletion($object, $recursionDepth, $path); |
||
80 | // Class properties |
||
81 | $this->getPropertyCompletion($object, $factory, $recursionDepth, $path); |
||
82 | // Class methods |
||
83 | $this->getMethodCompletion($object, $factory, $path); |
||
84 | // Behavior properties |
||
85 | $this->getBehaviorCompletion($object, $factory, $recursionDepth, $path); |
||
86 | } |
||
87 | |||
88 | // Protected Methods |
||
89 | // ========================================================================= |
||
90 | |||
91 | /** |
||
92 | * @param $object |
||
93 | * @param DocBlockFactory $factory |
||
94 | * @param string $name |
||
95 | * @param $path |
||
96 | */ |
||
97 | protected function getClassCompletion($object, DocBlockFactory $factory, string $name, $path): void |
||
98 | { |
||
99 | try { |
||
100 | $reflectionClass = new ReflectionClass($object); |
||
101 | } catch (ReflectionException $e) { |
||
102 | return; |
||
103 | } |
||
104 | // Information on the class itself |
||
105 | $className = $reflectionClass->getName(); |
||
106 | $docs = $this->getDocs($reflectionClass, $factory); |
||
107 | CompleteItem::create() |
||
108 | ->detail((string)$className) |
||
109 | ->documentation((string)$docs) |
||
110 | ->kind(CompleteItemKind::ClassKind) |
||
111 | ->label((string)$name) |
||
112 | ->insertText((string)$name) |
||
113 | ->add($this, $path); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $object |
||
118 | * @param $recursionDepth |
||
119 | * @param $path |
||
120 | */ |
||
121 | protected function getComponentCompletion($object, $recursionDepth, $path): void |
||
122 | { |
||
123 | if ($object instanceof ServiceLocator) { |
||
124 | foreach ($object->getComponents() as $key => $value) { |
||
125 | $componentObject = null; |
||
126 | try { |
||
127 | $componentObject = $object->get($key); |
||
128 | } catch (InvalidConfigException $e) { |
||
129 | // That's okay |
||
130 | } |
||
131 | if ($componentObject) { |
||
132 | $this->parseObject($key, $componentObject, $recursionDepth, $path); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param $object |
||
140 | * @param DocBlockFactory $factory |
||
141 | * @param $recursionDepth |
||
142 | * @param string $path |
||
143 | */ |
||
144 | protected function getPropertyCompletion($object, DocBlockFactory $factory, $recursionDepth, string $path): void |
||
145 | { |
||
146 | try { |
||
147 | $reflectionClass = new ReflectionClass($object); |
||
148 | } catch (ReflectionException $e) { |
||
149 | return; |
||
150 | } |
||
151 | $reflectionProperties = $reflectionClass->getProperties(); |
||
152 | $customField = false; |
||
153 | if ($object instanceof Behavior) { |
||
154 | $customField = true; |
||
155 | } |
||
156 | $sortPrefix = $customField ? self::CUSTOM_PROPERTY_SORT_PREFIX : self::PROPERTY_SORT_PREFIX; |
||
157 | foreach ($reflectionProperties as $reflectionProperty) { |
||
158 | $propertyName = $reflectionProperty->getName(); |
||
159 | // Exclude some properties |
||
160 | $propertyAllowed = true; |
||
161 | foreach (self::EXCLUDED_PROPERTY_REGEXES as $excludePattern) { |
||
162 | $pattern = '`' . $excludePattern . '`i'; |
||
163 | if (preg_match($pattern, $propertyName) === 1) { |
||
164 | $propertyAllowed = false; |
||
165 | } |
||
166 | } |
||
167 | if (in_array($propertyName, self::EXCLUDED_PROPERTY_NAMES, true)) { |
||
168 | $propertyAllowed = false; |
||
169 | } |
||
170 | if ($customField && in_array($propertyName, self::EXCLUDED_BEHAVIOR_NAMES, true)) { |
||
171 | $propertyAllowed = false; |
||
172 | } |
||
173 | // Process the property |
||
174 | if ($propertyAllowed && $reflectionProperty->isPublic()) { |
||
175 | $detail = "Property"; |
||
176 | $docblock = null; |
||
177 | $docs = $reflectionProperty->getDocComment(); |
||
178 | if ($docs) { |
||
179 | $docblock = $factory->create($docs); |
||
180 | $docs = ''; |
||
181 | $summary = $docblock->getSummary(); |
||
182 | if (!empty($summary)) { |
||
183 | $docs = $summary; |
||
184 | } |
||
185 | $description = $docblock->getDescription()->render(); |
||
186 | if (!empty($description)) { |
||
187 | $docs = $description; |
||
188 | } |
||
189 | } |
||
190 | // Figure out the type |
||
191 | if ($docblock) { |
||
192 | $tag = $docblock->getTagsByName('var'); |
||
193 | if ($tag && isset($tag[0])) { |
||
194 | $docs = $tag[0]; |
||
195 | } |
||
196 | } |
||
197 | if (preg_match('/@var\s+([^\s]+)/', $docs, $matches)) { |
||
198 | list(, $type) = $matches; |
||
199 | $detail = $type; |
||
200 | } |
||
201 | if ($detail === "Property") { |
||
202 | if ((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION >= 8)) { |
||
203 | if ($reflectionProperty->hasType()) { |
||
204 | $reflectionType = $reflectionProperty->getType(); |
||
205 | if ($reflectionType instanceof ReflectionNamedType) { |
||
206 | $type = $reflectionType->getName(); |
||
207 | $detail = $type; |
||
208 | } |
||
209 | } |
||
210 | if ((PHP_MAJOR_VERSION >= 8) && $reflectionProperty->hasDefaultValue()) { |
||
211 | $value = $reflectionProperty->getDefaultValue(); |
||
212 | if (is_array($value)) { |
||
213 | $value = json_encode($value); |
||
214 | } |
||
215 | if (!empty($value)) { |
||
216 | $detail = (string)$value; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | $thisPath = trim(implode('.', [$path, $propertyName]), '.'); |
||
222 | $label = $propertyName; |
||
223 | CompleteItem::create() |
||
224 | ->detail((string)$detail) |
||
225 | ->documentation((string)$docs) |
||
226 | ->kind($customField ? CompleteItemKind::FieldKind : CompleteItemKind::PropertyKind) |
||
227 | ->label((string)$label) |
||
228 | ->insertText((string)$label) |
||
229 | ->sortText((string)$sortPrefix . (string)$label) |
||
230 | ->add($this, $thisPath); |
||
231 | // Recurse through if this is an object |
||
232 | if (isset($object->$propertyName) && is_object($object->$propertyName)) { |
||
233 | if (!$customField && !in_array($propertyName, self::EXCLUDED_PROPERTY_NAMES, true)) { |
||
234 | $this->parseObject($propertyName, $object->$propertyName, $recursionDepth, $path); |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param $object |
||
243 | * @param DocBlockFactory $factory |
||
244 | * @param string $path |
||
245 | */ |
||
246 | protected function getMethodCompletion($object, DocBlockFactory $factory, string $path): void |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @param $object |
||
318 | * @param DocBlockFactory $factory |
||
319 | * @param $recursionDepth |
||
320 | * @param string $path |
||
321 | */ |
||
322 | protected function getBehaviorCompletion($object, DocBlockFactory $factory, $recursionDepth, string $path): void |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Try to get the best documentation block we can |
||
334 | * |
||
335 | * @param ReflectionClass|ReflectionMethod $reflection |
||
336 | * @param DocBlockFactory $factory |
||
337 | * @return string |
||
338 | */ |
||
339 | protected function getDocs($reflection, DocBlockFactory $factory): string |
||
357 |