| Total Complexity | 70 |
| Total Lines | 363 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 Properties |
||
| 60 | // ========================================================================= |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var bool If the class itself should be parsed for complete items |
||
| 64 | */ |
||
| 65 | public $parseClass = true; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool If any ServiceLocator components should be parsed for complete items |
||
| 69 | */ |
||
| 70 | public $parseComponents = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool If the class properties should be parsed for complete items |
||
| 74 | */ |
||
| 75 | public $parseProperties = true; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool If the class methods should be parsed for complete items |
||
| 79 | */ |
||
| 80 | public $parseMethods = true; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool If the class behaviors should be parsed for complete items |
||
| 84 | */ |
||
| 85 | public $parseBehaviors = true; |
||
| 86 | |||
| 87 | // Public Methods |
||
| 88 | // ========================================================================= |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inerhitdoc |
||
| 92 | */ |
||
| 93 | public function parseObject(string $name, $object, int $recursionDepth, string $path = ''): void |
||
| 94 | { |
||
| 95 | // Only recurse `RECURSION_DEPTH_LIMIT` deep |
||
| 96 | if ($recursionDepth > self::RECURSION_DEPTH_LIMIT) { |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | $recursionDepth++; |
||
| 100 | // Create the docblock factory |
||
| 101 | $factory = DocBlockFactory::createInstance(); |
||
| 102 | |||
| 103 | $path = trim(implode('.', [$path, $name]), '.'); |
||
| 104 | // The class itself |
||
| 105 | if ($this->parseClass) { |
||
| 106 | $this->getClassCompletion($object, $factory, $name, $path); |
||
| 107 | } |
||
| 108 | // ServiceLocator Components |
||
| 109 | if ($this->parseComponents) { |
||
| 110 | $this->getComponentCompletion($object, $recursionDepth, $path); |
||
| 111 | } |
||
| 112 | // Class properties |
||
| 113 | if ($this->parseProperties) { |
||
| 114 | $this->getPropertyCompletion($object, $factory, $recursionDepth, $path); |
||
| 115 | } |
||
| 116 | // Class methods |
||
| 117 | if ($this->parseMethods) { |
||
| 118 | $this->getMethodCompletion($object, $factory, $path); |
||
| 119 | } |
||
| 120 | // Behavior properties |
||
| 121 | if ($this->parseBeaviors) { |
||
| 122 | $this->getBehaviorCompletion($object, $factory, $recursionDepth, $path); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // Protected Methods |
||
| 127 | // ========================================================================= |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param $object |
||
| 131 | * @param DocBlockFactory $factory |
||
| 132 | * @param string $name |
||
| 133 | * @param $path |
||
| 134 | */ |
||
| 135 | protected function getClassCompletion($object, DocBlockFactory $factory, string $name, $path): void |
||
| 136 | { |
||
| 137 | try { |
||
| 138 | $reflectionClass = new ReflectionClass($object); |
||
| 139 | } catch (ReflectionException $e) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | // Information on the class itself |
||
| 143 | $className = $reflectionClass->getName(); |
||
| 144 | $docs = $this->getDocs($reflectionClass, $factory); |
||
| 145 | CompleteItem::create() |
||
| 146 | ->detail((string)$className) |
||
| 147 | ->documentation((string)$docs) |
||
| 148 | ->kind(CompleteItemKind::ClassKind) |
||
| 149 | ->label((string)$name) |
||
| 150 | ->insertText((string)$name) |
||
| 151 | ->add($this, $path); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $object |
||
| 156 | * @param $recursionDepth |
||
| 157 | * @param $path |
||
| 158 | */ |
||
| 159 | protected function getComponentCompletion($object, $recursionDepth, $path): void |
||
| 160 | { |
||
| 161 | if ($object instanceof ServiceLocator) { |
||
| 162 | foreach ($object->getComponents() as $key => $value) { |
||
| 163 | $componentObject = null; |
||
| 164 | try { |
||
| 165 | $componentObject = $object->get($key); |
||
| 166 | } catch (InvalidConfigException $e) { |
||
| 167 | // That's okay |
||
| 168 | } |
||
| 169 | if ($componentObject) { |
||
| 170 | $this->parseObject($key, $componentObject, $recursionDepth, $path); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param $object |
||
| 178 | * @param DocBlockFactory $factory |
||
| 179 | * @param $recursionDepth |
||
| 180 | * @param string $path |
||
| 181 | */ |
||
| 182 | protected function getPropertyCompletion($object, DocBlockFactory $factory, $recursionDepth, string $path): void |
||
| 183 | { |
||
| 184 | try { |
||
| 185 | $reflectionClass = new ReflectionClass($object); |
||
| 186 | } catch (ReflectionException $e) { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | $reflectionProperties = $reflectionClass->getProperties(); |
||
| 190 | $customField = false; |
||
| 191 | if ($object instanceof Behavior) { |
||
| 192 | $customField = true; |
||
| 193 | } |
||
| 194 | $sortPrefix = $customField ? self::CUSTOM_PROPERTY_SORT_PREFIX : self::PROPERTY_SORT_PREFIX; |
||
| 195 | foreach ($reflectionProperties as $reflectionProperty) { |
||
| 196 | $propertyName = $reflectionProperty->getName(); |
||
| 197 | // Exclude some properties |
||
| 198 | $propertyAllowed = true; |
||
| 199 | foreach (self::EXCLUDED_PROPERTY_REGEXES as $excludePattern) { |
||
| 200 | $pattern = '`' . $excludePattern . '`i'; |
||
| 201 | if (preg_match($pattern, $propertyName) === 1) { |
||
| 202 | $propertyAllowed = false; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | if (in_array($propertyName, self::EXCLUDED_PROPERTY_NAMES, true)) { |
||
| 206 | $propertyAllowed = false; |
||
| 207 | } |
||
| 208 | if ($customField && in_array($propertyName, self::EXCLUDED_BEHAVIOR_NAMES, true)) { |
||
| 209 | $propertyAllowed = false; |
||
| 210 | } |
||
| 211 | // Process the property |
||
| 212 | if ($propertyAllowed && $reflectionProperty->isPublic()) { |
||
| 213 | $detail = "Property"; |
||
| 214 | $docblock = null; |
||
| 215 | $docs = $reflectionProperty->getDocComment(); |
||
| 216 | if ($docs) { |
||
| 217 | $docblock = $factory->create($docs); |
||
| 218 | $docs = ''; |
||
| 219 | $summary = $docblock->getSummary(); |
||
| 220 | if (!empty($summary)) { |
||
| 221 | $docs = $summary; |
||
| 222 | } |
||
| 223 | $description = $docblock->getDescription()->render(); |
||
| 224 | if (!empty($description)) { |
||
| 225 | $docs = $description; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | // Figure out the type |
||
| 229 | if ($docblock) { |
||
| 230 | $tag = $docblock->getTagsByName('var'); |
||
| 231 | if ($tag && isset($tag[0])) { |
||
| 232 | $docs = $tag[0]; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | if (preg_match('/@var\s+([^\s]+)/', $docs, $matches)) { |
||
| 236 | list(, $type) = $matches; |
||
| 237 | $detail = $type; |
||
| 238 | } |
||
| 239 | if ($detail === "Property") { |
||
| 240 | if ((PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION >= 8)) { |
||
| 241 | if ($reflectionProperty->hasType()) { |
||
| 242 | $reflectionType = $reflectionProperty->getType(); |
||
| 243 | if ($reflectionType instanceof ReflectionNamedType) { |
||
| 244 | $type = $reflectionType->getName(); |
||
| 245 | $detail = $type; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | if ((PHP_MAJOR_VERSION >= 8) && $reflectionProperty->hasDefaultValue()) { |
||
| 249 | $value = $reflectionProperty->getDefaultValue(); |
||
| 250 | if (is_array($value)) { |
||
| 251 | $value = json_encode($value); |
||
| 252 | } |
||
| 253 | if (!empty($value)) { |
||
| 254 | $detail = (string)$value; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | $thisPath = trim(implode('.', [$path, $propertyName]), '.'); |
||
| 260 | $label = $propertyName; |
||
| 261 | CompleteItem::create() |
||
| 262 | ->detail((string)$detail) |
||
| 263 | ->documentation((string)$docs) |
||
| 264 | ->kind($customField ? CompleteItemKind::FieldKind : CompleteItemKind::PropertyKind) |
||
| 265 | ->label((string)$label) |
||
| 266 | ->insertText((string)$label) |
||
| 267 | ->sortText((string)$sortPrefix . (string)$label) |
||
| 268 | ->add($this, $thisPath); |
||
| 269 | // Recurse through if this is an object |
||
| 270 | if (isset($object->$propertyName) && is_object($object->$propertyName)) { |
||
| 271 | if (!$customField && !in_array($propertyName, self::EXCLUDED_PROPERTY_NAMES, true)) { |
||
| 272 | $this->parseObject($propertyName, $object->$propertyName, $recursionDepth, $path); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $object |
||
| 281 | * @param DocBlockFactory $factory |
||
| 282 | * @param string $path |
||
| 283 | */ |
||
| 284 | protected function getMethodCompletion($object, DocBlockFactory $factory, string $path): void |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param $object |
||
| 356 | * @param DocBlockFactory $factory |
||
| 357 | * @param $recursionDepth |
||
| 358 | * @param string $path |
||
| 359 | */ |
||
| 360 | protected function getBehaviorCompletion($object, DocBlockFactory $factory, $recursionDepth, string $path): void |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Try to get the best documentation block we can |
||
| 372 | * |
||
| 373 | * @param ReflectionClass|ReflectionMethod $reflection |
||
| 374 | * @param DocBlockFactory $factory |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | protected function getDocs($reflection, DocBlockFactory $factory): string |
||
| 395 |