We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 46 |
Total Lines | 287 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 2 | ||
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 ResolverArgs $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 | * @param string|array|null $groups |
|
64 | 16 | * |
|
65 | 16 | * @throws ArgumentsValidationException |
|
66 | */ |
||
67 | public function validate($groups = null, bool $throw = true): ?ConstraintViolationListInterface |
||
68 | { |
||
69 | $rootNode = new ValidationNode( |
||
70 | $this->info->parentType, |
||
71 | $this->info->fieldName, |
||
72 | null, |
||
73 | 16 | $this->resolverArgs |
|
74 | ); |
||
75 | |||
76 | 16 | $classMapping = $this->mergeClassValidation(); |
|
77 | 16 | ||
78 | 16 | $this->buildValidationTree( |
|
79 | 16 | $rootNode, |
|
80 | $this->info->fieldDefinition->config['args'], |
||
81 | $classMapping, |
||
82 | $this->resolverArgs->args->getArrayCopy() |
||
83 | ); |
||
84 | |||
85 | $validator = $this->createValidator($this->metadataFactory); |
||
86 | |||
87 | $errors = $validator->validate($rootNode, null, $groups); |
||
88 | 16 | ||
89 | if ($throw && $errors->count() > 0) { |
||
90 | 16 | throw new ArgumentsValidationException($errors); |
|
91 | } else { |
||
92 | 16 | return $errors; |
|
93 | 16 | } |
|
94 | 16 | } |
|
95 | 16 | ||
96 | 16 | private function mergeClassValidation(): array |
|
97 | { |
||
98 | $common = static::normalizeConfig($this->info->parentType->config['validation'] ?? []); |
||
99 | 16 | $specific = static::normalizeConfig($this->info->fieldDefinition->config['validation'] ?? []); |
|
100 | |||
101 | 16 | return array_filter([ |
|
102 | 'link' => $specific['link'] ?? $common['link'] ?? null, |
||
103 | 16 | 'constraints' => [ |
|
104 | 6 | ...($common['constraints'] ?? []), |
|
105 | ...($specific['constraints'] ?? []), |
||
106 | 10 | ], |
|
107 | ]); |
||
108 | } |
||
109 | |||
110 | private function createValidator(MetadataFactory $metadataFactory): ValidatorInterface |
||
124 | 6 | } |
|
125 | |||
126 | /** |
||
127 | 6 | * Creates a composition of ValidationNode objects from args |
|
128 | * and simultaneously applies to them validation constraints. |
||
129 | 6 | */ |
|
130 | 2 | protected function buildValidationTree(ValidationNode $rootObject, array $fields, array $classValidation, array $inputData): ValidationNode |
|
208 | } |
||
209 | 6 | ||
210 | 6 | /** |
|
211 | * @param GeneratedTypeInterface|ListOfType|NonNull $type |
||
212 | 6 | */ |
|
213 | 6 | private static function isListOfType($type): bool |
|
214 | { |
||
215 | if ($type instanceof ListOfType || ($type instanceof NonNull && $type->getOfType() instanceof ListOfType)) { |
||
216 | 6 | return true; |
|
217 | 6 | } |
|
218 | |||
219 | return false; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param ObjectType|InputObjectType $type |
||
224 | 8 | */ |
|
225 | private function createCollectionNode(array $values, $type, ValidationNode $parent): array |
||
234 | 8 | } |
|
235 | 8 | ||
236 | 8 | /** |
|
237 | 2 | * @param ObjectType|InputObjectType $type |
|
238 | 6 | */ |
|
239 | 6 | private function createObjectNode(array $value, $type, ValidationNode $parent): ValidationNode |
|
240 | { |
||
241 | $classValidation = static::normalizeConfig($type->config['validation'] ?? []); |
||
242 | 8 | ||
243 | return $this->buildValidationTree( |
||
244 | new ValidationNode($type, null, $parent, $this->resolverArgs), |
||
245 | 8 | self::unclosure($type->config['fields']), |
|
|
|||
246 | $classValidation, |
||
247 | 16 | $value |
|
248 | ); |
||
249 | 16 | } |
|
250 | 14 | ||
251 | private function applyClassValidation(ObjectMetadata $metadata, array $rules): void |
||
252 | 16 | { |
|
253 | $rules = static::normalizeConfig($rules); |
||
254 | |||
255 | foreach ($rules as $key => $value) { |
||
256 | switch ($key) { |
||
257 | case 'link': |
||
258 | $linkedMetadata = $this->defaultValidator->getMetadataFor($value); |
||
259 | 10 | $metadata->addConstraints($linkedMetadata->getConstraints()); |
|
260 | break; |
||
261 | 10 | case 'constraints': |
|
262 | foreach ($this->unclosure($value) as $constraint) { |
||
263 | if ($constraint instanceof Constraint) { |
||
264 | $metadata->addConstraint($constraint); |
||
265 | } elseif ($constraint instanceof GroupSequence) { |
||
266 | $metadata->setGroupSequence($constraint); |
||
267 | } |
||
268 | } |
||
269 | break; |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Restructures short forms into the full form array and |
||
276 | * unwraps constraints in closures. |
||
277 | * |
||
278 | * @param mixed $config |
||
279 | */ |
||
280 | public static function normalizeConfig($config): array |
||
281 | { |
||
282 | if ($config instanceof Closure) { |
||
283 | return ['constraints' => $config()]; |
||
284 | } |
||
285 | |||
286 | if (self::CASCADE === $config) { |
||
287 | return ['cascade' => []]; |
||
288 | } |
||
289 | |||
290 | if (isset($config['constraints']) && $config['constraints'] instanceof Closure) { |
||
291 | $config['constraints'] = $config['constraints'](); |
||
292 | } |
||
293 | |||
294 | return $config; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @param mixed $value |
||
299 | * |
||
300 | * @return mixed |
||
301 | */ |
||
302 | private function unclosure($value) |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param string|array|null $groups |
||
313 | * |
||
314 | * @throws ArgumentsValidationException |
||
315 | */ |
||
316 | public function __invoke($groups = null, bool $throw = true): ?ConstraintViolationListInterface |
||
319 | } |
||
320 | } |
||
321 |