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