| Total Complexity | 49 |
| Total Lines | 277 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Mapper 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 Mapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 16 | class Mapper |
||
| 17 | { |
||
| 18 | private ArgumentResolverFactory $argumentResolverFactory; |
||
| 19 | |||
| 20 | public function __construct(ArgumentResolverFactory $argumentResolverFactory = null) |
||
| 21 | { |
||
| 22 | if (null === $argumentResolverFactory) { |
||
| 23 | $argumentResolverFactory = new ArgumentResolverFactory(); |
||
| 24 | } |
||
| 25 | $this->argumentResolverFactory = $argumentResolverFactory; |
||
| 26 | } |
||
| 27 | |||
| 28 | public static function make(): static |
||
| 29 | { |
||
| 30 | return new static(); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Register new custom argument resolver. |
||
| 35 | * |
||
| 36 | * @param string $resolverAlias - resolver name |
||
| 37 | * @param string $resolverClassName - class name of argument resolver |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | public function registerCustomArgumentResolver(string $resolverAlias, string $resolverClassName): void |
||
| 42 | { |
||
| 43 | $this->argumentResolverFactory->addResolver($resolverAlias, $resolverClassName); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @template T |
||
| 48 | * |
||
| 49 | * @param class-string<T> $targetClass |
||
|
|
|||
| 50 | * |
||
| 51 | * @throws ReflectionException |
||
| 52 | * @throws ValidationException |
||
| 53 | * @throws Resolver\ArgumentResolverException |
||
| 54 | * @throws Resolver\Parser\SyntaxException |
||
| 55 | * @return T |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | public function mapFromFile(string $targetClass, string $configFile): object |
||
| 59 | { |
||
| 60 | $config = Yaml::parseFile($configFile); |
||
| 61 | |||
| 62 | return $this->map($targetClass, $config); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @template T |
||
| 67 | * |
||
| 68 | * @param class-string<T> $targetClass |
||
| 69 | * |
||
| 70 | * @throws ValidationException |
||
| 71 | * @throws ReflectionException|Resolver\ArgumentResolverException|Resolver\Parser\SyntaxException |
||
| 72 | * |
||
| 73 | * @return T |
||
| 74 | */ |
||
| 75 | public function map(string $targetClass, array $config): object |
||
| 76 | { |
||
| 77 | $instance = new $targetClass; |
||
| 78 | $classInfoReflector = new ClassInfoReflector(); |
||
| 79 | $classInfo = $classInfoReflector->introspect($targetClass); |
||
| 80 | $validationResult = $this->validate($classInfo, $config); |
||
| 81 | |||
| 82 | if (!$validationResult->isValid()) { |
||
| 83 | throw new ValidationException($validationResult); |
||
| 84 | } |
||
| 85 | |||
| 86 | $mappingConfig = $this->getMappingConfig($classInfo, $config, $instance); |
||
| 87 | $rootResolver = new InstanceArgumentResolver($classInfo, $mappingConfig); |
||
| 88 | |||
| 89 | return $rootResolver->resolve(new Context($config, $rootResolver)); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @template T |
||
| 94 | * |
||
| 95 | * @param class-string<T> $targetClass |
||
| 96 | * |
||
| 97 | * @throws ValidationException |
||
| 98 | * @throws ReflectionException|Resolver\ArgumentResolverException|Resolver\Parser\SyntaxException |
||
| 99 | * |
||
| 100 | * @return T |
||
| 101 | */ |
||
| 102 | public function mapFromString(string $targetClass, string $yaml): object |
||
| 103 | { |
||
| 104 | $config = Yaml::parse($yaml); |
||
| 105 | |||
| 106 | return $this->map($targetClass, $config); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @throws Resolver\ArgumentResolverException |
||
| 111 | * @throws Resolver\Parser\SyntaxException |
||
| 112 | */ |
||
| 113 | private function getMappingConfig(ClassInfo $classInfo, ?array $config, $parentKey = null): array |
||
| 159 | } |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @param ClassField $field |
||
| 164 | * @param mixed $rawValue |
||
| 165 | * |
||
| 166 | * @throws Resolver\ArgumentResolverException |
||
| 167 | * @throws Resolver\Parser\SyntaxException |
||
| 168 | * @return ArgumentResolver |
||
| 169 | */ |
||
| 170 | private function toArgumentResolver(ClassField $field, mixed $rawValue): ArgumentResolver |
||
| 171 | { |
||
| 172 | if ($field->isPrimitive()) { |
||
| 173 | $argResolver = $this->createArgumentResolverForPrimitive($rawValue); |
||
| 174 | } else if ($field->isList()) { |
||
| 175 | $value = []; |
||
| 176 | if ($field->isTypedCollection()) { |
||
| 177 | foreach ($rawValue as $key => $item) { |
||
| 178 | $value[] = new InstanceArgumentResolver($field->getClassInfo(), $this->getMappingConfig($field->getClassInfo(), $item, $key)); |
||
| 179 | } |
||
| 180 | } else { |
||
| 181 | $value = $this->doMapArray($rawValue); |
||
| 182 | } |
||
| 183 | |||
| 184 | $argResolver = new ListArgumentResolver($value); |
||
| 185 | } else { |
||
| 186 | $argResolver = new InstanceArgumentResolver($field->getClassInfo(), $this->getMappingConfig($field->getClassInfo(), $rawValue, $field->newInstance())); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $argResolver; |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @param mixed $rawValue |
||
| 195 | * |
||
| 196 | * @throws Resolver\Parser\SyntaxException |
||
| 197 | * @return ArgumentResolver |
||
| 198 | */ |
||
| 199 | private function createArgumentResolverForPrimitive(mixed $rawValue): ArgumentResolver |
||
| 200 | { |
||
| 201 | if (is_bool($rawValue) || is_int($rawValue) || is_array($rawValue)) { |
||
| 202 | $argResolver = new ScalarArgumentResolver($rawValue); |
||
| 203 | } else { |
||
| 204 | $argResolver = $this->processExpression($rawValue); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $argResolver; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @throws Resolver\Parser\SyntaxException |
||
| 212 | */ |
||
| 213 | private function processExpression(string $expression): ArgumentResolver |
||
| 214 | { |
||
| 215 | $parser = new Parser($expression); |
||
| 216 | |||
| 217 | return $parser->parse()->toResolver($this->argumentResolverFactory); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @throws Resolver\Parser\SyntaxException |
||
| 222 | */ |
||
| 223 | private function doMapArray(array $array): array |
||
| 224 | { |
||
| 225 | $result = []; |
||
| 226 | foreach ($array as $key => $value) { |
||
| 227 | if (is_array($value)) { |
||
| 228 | $result[$key] = $this->doMapArray($value); |
||
| 229 | } else if (is_string($value)) { |
||
| 230 | $result[$key] = $this->processExpression($value); |
||
| 231 | } else { |
||
| 232 | $result[$key] = new ScalarArgumentResolver($value); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | return $result; |
||
| 237 | } |
||
| 238 | |||
| 239 | private function validate(ClassInfo $classInfo, ?array $config, ?array $parent = [], ?ValidationResult $validationResult = null): ValidationResult |
||
| 293 | } |
||
| 294 | } |
||
| 295 |