| Conditions | 20 |
| Paths | 452 |
| Total Lines | 91 |
| Code Lines | 48 |
| 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 |
||
| 89 | private function flattenInputClass( |
||
| 90 | string $className, |
||
| 91 | string $group, |
||
| 92 | string|null $groupDescription, |
||
| 93 | ): array { |
||
| 94 | $refClass = new ReflectionClass($className); |
||
| 95 | $constructor = $refClass->getConstructor(); |
||
| 96 | |||
| 97 | if (! $constructor) { |
||
| 98 | return [[], []]; |
||
| 99 | } |
||
| 100 | |||
| 101 | // Get constructor documentation using OptionsMethodDocBolck |
||
| 102 | // This already handles type conversion (int -> integer) and description extraction |
||
| 103 | [, $constructorParamDocs] = ($this->docBlock)($constructor); |
||
| 104 | |||
| 105 | $parameters = []; |
||
| 106 | $required = []; |
||
| 107 | |||
| 108 | foreach ($constructor->getParameters() as $param) { |
||
| 109 | $paramName = $param->getName(); |
||
| 110 | |||
| 111 | // Check if this is a nested Input parameter |
||
| 112 | if ($this->inputIterator->getInputAttribute($param) !== null) { |
||
| 113 | $type = $param->getType(); |
||
| 114 | if ($type instanceof ReflectionNamedType && class_exists($type->getName())) { |
||
| 115 | $nestedDescription = null; |
||
| 116 | if (isset($constructorParamDocs[$paramName]['description'])) { |
||
| 117 | $nestedDescription = $constructorParamDocs[$paramName]['description']; |
||
| 118 | } |
||
| 119 | |||
| 120 | [$nestedParams, $nestedRequired] = $this->flattenInputClass( |
||
| 121 | $type->getName(), |
||
| 122 | $paramName, |
||
| 123 | $nestedDescription, |
||
| 124 | ); |
||
| 125 | |||
| 126 | $parameters = array_merge($parameters, $nestedParams); |
||
| 127 | $required = array_merge($required, $nestedRequired); |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // Start with phpdoc information (already has type conversion) |
||
| 133 | $paramMeta = []; |
||
| 134 | if (isset($constructorParamDocs[$paramName])) { |
||
| 135 | $paramMeta = $constructorParamDocs[$paramName]; |
||
| 136 | } |
||
| 137 | |||
| 138 | // Override with reflection type if phpdoc doesn't have type |
||
| 139 | if (! isset($paramMeta['type'])) { |
||
| 140 | $type = $param->getType(); |
||
| 141 | if ($type instanceof ReflectionNamedType) { |
||
| 142 | $typeName = $type->getName(); |
||
| 143 | // Apply same conversion as OptionsMethodDocBolck |
||
| 144 | $paramMeta['type'] = $typeName === 'int' ? 'integer' : $typeName; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // Add default value |
||
| 149 | if ($param->isDefaultValueAvailable()) { |
||
| 150 | /** @var mixed $defaultValue */ |
||
| 151 | $defaultValue = $param->getDefaultValue(); |
||
| 152 | if (is_string($defaultValue)) { |
||
| 153 | $paramMeta['default'] = $defaultValue; |
||
| 154 | } elseif (is_bool($defaultValue)) { |
||
| 155 | $paramMeta['default'] = $defaultValue ? 'true' : 'false'; |
||
| 156 | } elseif (is_numeric($defaultValue)) { |
||
| 157 | $paramMeta['default'] = (string) $defaultValue; |
||
| 158 | } elseif (is_array($defaultValue)) { |
||
| 159 | $paramMeta['default'] = '[]'; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | // Add group information |
||
| 164 | $paramMeta['group'] = $group; |
||
| 165 | if ($groupDescription) { |
||
| 166 | $paramMeta['group_description'] = $groupDescription; |
||
| 167 | } |
||
| 168 | |||
| 169 | $parameters[$paramName] = $paramMeta; |
||
| 170 | |||
| 171 | // Check if required |
||
| 172 | if ($param->isDefaultValueAvailable() || $param->allowsNull()) { |
||
| 173 | continue; |
||
| 174 | } |
||
| 175 | |||
| 176 | $required[] = $paramName; |
||
| 177 | } |
||
| 178 | |||
| 179 | return [$parameters, $required]; |
||
| 180 | } |
||
| 182 |