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