We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 47 |
Total Lines | 290 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like InputValidator 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 InputValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class InputValidator |
||
33 | { |
||
34 | private const TYPE_PROPERTY = 'property'; |
||
35 | private const TYPE_GETTER = 'getter'; |
||
36 | public const CASCADE = 'cascade'; |
||
37 | |||
38 | private array $resolverArgs; |
||
39 | private ValidatorInterface $defaultValidator; |
||
40 | private MetadataFactory $metadataFactory; |
||
41 | private ResolveInfo $info; |
||
42 | private ConstraintValidatorFactoryInterface $constraintValidatorFactory; |
||
43 | private ?TranslatorInterface $defaultTranslator; |
||
44 | 17 | ||
45 | /** @var ClassMetadataInterface[] */ |
||
46 | private array $cachedMetadata = []; |
||
47 | |||
48 | public function __construct( |
||
60 | 16 | } |
|
61 | 16 | ||
62 | 16 | /** |
|
63 | 16 | * Converts a numeric array of resolver args to an associative one. |
|
64 | 16 | * |
|
65 | 16 | * @param mixed $value |
|
66 | * @param mixed $context |
||
67 | */ |
||
68 | private function mapResolverArgs($value, ArgumentInterface $args, $context, ResolveInfo $info): array |
||
69 | { |
||
70 | return [ |
||
71 | 'value' => $value, |
||
72 | 'args' => $args, |
||
73 | 16 | 'context' => $context, |
|
74 | 'info' => $info, |
||
75 | ]; |
||
76 | 16 | } |
|
77 | 16 | ||
78 | 16 | /** |
|
79 | 16 | * @param string|array|null $groups |
|
80 | * |
||
81 | * @throws ArgumentsValidationException |
||
82 | */ |
||
83 | public function validate($groups = null, bool $throw = true): ?ConstraintViolationListInterface |
||
84 | { |
||
85 | $rootObject = new ValidationNode($this->info->parentType, $this->info->fieldName, null, $this->resolverArgs); |
||
86 | |||
87 | $classMapping = $this->mergeClassValidation(); |
||
88 | 16 | ||
89 | $this->buildValidationTree( |
||
90 | 16 | $rootObject, |
|
91 | $this->info->fieldDefinition->config['args'], |
||
92 | 16 | $classMapping, |
|
93 | 16 | $this->resolverArgs['args']->getArrayCopy() |
|
94 | 16 | ); |
|
95 | 16 | ||
96 | 16 | $validator = $this->createValidator($this->metadataFactory); |
|
97 | |||
98 | $errors = $validator->validate($rootObject, null, $groups); |
||
99 | 16 | ||
100 | if ($throw && $errors->count() > 0) { |
||
101 | 16 | throw new ArgumentsValidationException($errors); |
|
102 | } else { |
||
103 | 16 | return $errors; |
|
104 | 6 | } |
|
105 | } |
||
106 | 10 | ||
107 | private function mergeClassValidation(): array |
||
108 | { |
||
109 | $common = static::normalizeConfig($this->info->parentType->config['validation'] ?? []); |
||
110 | $specific = static::normalizeConfig($this->info->fieldDefinition->config['validation'] ?? []); |
||
111 | |||
112 | return array_filter([ |
||
113 | 'link' => $specific['link'] ?? $common['link'] ?? null, |
||
114 | 16 | 'constraints' => [ |
|
115 | ...($common['constraints'] ?? []), |
||
116 | 16 | ...($specific['constraints'] ?? []), |
|
117 | ], |
||
118 | 16 | ]); |
|
119 | 8 | } |
|
120 | |||
121 | private function createValidator(MetadataFactory $metadataFactory): ValidatorInterface |
||
135 | 6 | } |
|
136 | |||
137 | 6 | /** |
|
138 | 4 | * Creates a composition of ValidationNode objects from args |
|
139 | * and simultaneously applies to them validation constraints. |
||
140 | */ |
||
141 | 6 | protected function buildValidationTree(ValidationNode $rootObject, array $fields, array $classValidation, array $inputData): ValidationNode |
|
222 | } |
||
223 | |||
224 | 8 | /** |
|
225 | * @param GeneratedTypeInterface|ListOfType|NonNull $type |
||
226 | 8 | */ |
|
227 | private static function isListOfType($type): bool |
||
228 | 8 | { |
|
229 | if ($type instanceof ListOfType || ($type instanceof NonNull && $type->getOfType() instanceof ListOfType)) { |
||
230 | 8 | return true; |
|
231 | 2 | } |
|
232 | 2 | ||
233 | 2 | return false; |
|
234 | 8 | } |
|
235 | 8 | ||
236 | 8 | /** |
|
237 | 2 | * @param ObjectType|InputObjectType $type |
|
238 | 6 | */ |
|
239 | 6 | private function createCollectionNode(array $values, $type, ValidationNode $parent): array |
|
248 | } |
||
249 | 16 | ||
250 | 14 | /** |
|
251 | * @param ObjectType|InputObjectType $type |
||
252 | 16 | */ |
|
253 | private function createObjectNode(array $value, $type, ValidationNode $parent): ValidationNode |
||
254 | { |
||
255 | $classValidation = static::normalizeConfig($type->config['validation'] ?? []); |
||
256 | |||
257 | return $this->buildValidationTree( |
||
258 | new ValidationNode($type, null, $parent, $this->resolverArgs), |
||
259 | 10 | $type->config['fields'](), |
|
260 | $classValidation, |
||
261 | 10 | $value |
|
262 | ); |
||
263 | } |
||
264 | |||
265 | private function applyClassValidation(ObjectMetadata $metadata, array $rules): void |
||
266 | { |
||
267 | $rules = static::normalizeConfig($rules); |
||
268 | |||
269 | foreach ($rules as $key => $value) { |
||
270 | switch ($key) { |
||
271 | case 'link': |
||
272 | $linkedMetadata = $this->defaultValidator->getMetadataFor($value); |
||
273 | $metadata->addConstraints($linkedMetadata->getConstraints()); |
||
274 | break; |
||
275 | case 'constraints': |
||
276 | if ($value instanceof Closure) { |
||
277 | $value = $value(); |
||
278 | } |
||
279 | foreach ($value as $constraint) { |
||
280 | if ($constraint instanceof Constraint) { |
||
281 | $metadata->addConstraint($constraint); |
||
282 | } elseif ($constraint instanceof GroupSequence) { |
||
283 | $metadata->setGroupSequence($constraint); |
||
284 | } |
||
285 | } |
||
286 | break; |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Restructures short forms into the full form array and |
||
293 | * unwraps constraints in closures. |
||
294 | * |
||
295 | * @param mixed $config |
||
296 | */ |
||
297 | public static function normalizeConfig($config): array |
||
298 | { |
||
299 | if ($config instanceof Closure) { |
||
300 | return ['constraints' => $config()]; |
||
301 | } |
||
302 | |||
303 | if (self::CASCADE === $config) { |
||
304 | return ['cascade' => []]; |
||
305 | } |
||
306 | |||
307 | if (isset($config['constraints']) && $config['constraints'] instanceof Closure) { |
||
308 | $config['constraints'] = $config['constraints'](); |
||
309 | } |
||
310 | |||
311 | return $config; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param string|array|null $groups |
||
316 | * |
||
317 | * @throws ArgumentsValidationException |
||
318 | */ |
||
319 | public function __invoke($groups = null, bool $throw = true): ?ConstraintViolationListInterface |
||
322 | } |
||
323 | } |
||
324 |