| Total Complexity | 40 |
| Total Lines | 186 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class Parser |
||
| 23 | { |
||
| 24 | public function __construct( |
||
| 25 | private readonly ReaderInterface $reader = new AttributeReader() |
||
| 26 | ) { |
||
| 27 | } |
||
| 28 | |||
| 29 | public function hasCommandAttribute(\ReflectionClass $reflection): bool |
||
| 30 | { |
||
| 31 | return $this->reader->firstClassMetadata($reflection, AsCommand::class) !== null; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function parse(\ReflectionClass $reflection): Result |
||
| 35 | { |
||
| 36 | /** @var AsCommand $attribute */ |
||
| 37 | $attribute = $this->reader->firstClassMetadata($reflection, AsCommand::class); |
||
| 38 | |||
| 39 | return new Result( |
||
| 40 | name: $attribute->name, |
||
| 41 | arguments: $this->parseArguments($reflection), |
||
| 42 | options: $this->parseOptions($reflection), |
||
| 43 | description: $attribute->description, |
||
| 44 | help: $attribute->help |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function fillProperties(Command $command, InputInterface $input): void |
||
| 49 | { |
||
| 50 | $reflection = new \ReflectionClass($command); |
||
| 51 | |||
| 52 | foreach ($reflection->getProperties() as $property) { |
||
| 53 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
||
| 54 | if ($attribute === null) { |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($input->hasArgument($attribute->name ?? $property->getName())) { |
||
| 59 | $property->setValue( |
||
| 60 | $command, |
||
| 61 | $this->typecast($input->getArgument($attribute->name ?? $property->getName()), $property) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | foreach ($reflection->getProperties() as $property) { |
||
| 67 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
||
| 68 | if ($attribute === null) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | if ($input->hasOption($attribute->name ?? $property->getName())) { |
||
| 73 | $property->setValue( |
||
| 74 | $command, |
||
| 75 | $this->typecast($input->getOption($attribute->name ?? $property->getName()), $property) |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | private function parseArguments(\ReflectionClass $reflection): array |
||
| 82 | { |
||
| 83 | $result = []; |
||
| 84 | $arrayArgument = null; |
||
| 85 | foreach ($reflection->getProperties() as $property) { |
||
| 86 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
||
| 87 | if ($attribute === null) { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | $type = $this->getPropertyType($property); |
||
| 92 | |||
| 93 | $isOptional = $property->hasDefaultValue() || $type->allowsNull(); |
||
| 94 | $isArray = $type->getName() === 'array'; |
||
| 95 | $mode = match (true) { |
||
| 96 | $isArray && !$isOptional => InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
||
| 97 | $isArray && $isOptional => InputArgument::IS_ARRAY | InputArgument::OPTIONAL, |
||
| 98 | $isOptional => InputArgument::OPTIONAL, |
||
| 99 | default => InputArgument::REQUIRED |
||
| 100 | }; |
||
| 101 | |||
| 102 | $argument = new InputArgument( |
||
| 103 | name: $attribute->name ?? $property->getName(), |
||
| 104 | mode: $mode, |
||
| 105 | description: (string) $attribute->description, |
||
| 106 | default: $property->hasDefaultValue() ? $property->getDefaultValue() : null, |
||
| 107 | suggestedValues: $attribute->suggestedValues |
||
| 108 | ); |
||
| 109 | |||
| 110 | if ($arrayArgument !== null && $isArray) { |
||
| 111 | throw new ConfiguratorException('There must be only one array argument!'); |
||
| 112 | } |
||
| 113 | |||
| 114 | // It must be used at the end of the argument list. |
||
| 115 | if ($isArray) { |
||
| 116 | $arrayArgument = $argument; |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | $result[] = $argument; |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($arrayArgument !== null) { |
||
| 123 | $result[] = $arrayArgument; |
||
| 124 | } |
||
| 125 | |||
| 126 | return $result; |
||
| 127 | } |
||
| 128 | |||
| 129 | private function parseOptions(\ReflectionClass $reflection): array |
||
| 161 | } |
||
| 162 | |||
| 163 | private function typecast(mixed $value, \ReflectionProperty $property): mixed |
||
| 164 | { |
||
| 165 | $type = $property->hasType() ? $property->getType() : null; |
||
| 166 | |||
| 167 | if (!$type instanceof \ReflectionNamedType) { |
||
| 168 | return $value; |
||
| 169 | } |
||
| 170 | |||
| 171 | return match ($type->getName()) { |
||
| 172 | 'int' => (int) $value, |
||
| 173 | 'string' => (string) $value, |
||
| 174 | 'bool' => (bool) $value, |
||
| 175 | 'float' => (float) $value, |
||
| 176 | 'array' => (array) $value, |
||
| 177 | default => $value |
||
| 178 | }; |
||
| 179 | } |
||
| 180 | |||
| 181 | private function getPropertyType(\ReflectionProperty $property): \ReflectionNamedType |
||
| 208 | } |
||
| 209 | } |
||
| 210 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths