| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 91 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 357 |