Passed
Pull Request — master (#320)
by Dmitriy
02:50
created

Validator::validate()   B

Complexity

Conditions 11
Paths 40

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 11.0619

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 29
c 6
b 1
f 0
dl 0
loc 48
ccs 23
cts 25
cp 0.92
rs 7.3166
cc 11
nc 40
nop 3
crap 11.0619

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Closure;
8
use InvalidArgumentException;
9
use JetBrains\PhpStorm\Pure;
10
use Psr\Container\ContainerExceptionInterface;
11
use Psr\Container\NotFoundExceptionInterface;
12
use ReflectionProperty;
13
use Traversable;
14
use Yiisoft\Validator\DataSet\ArrayDataSet;
15
use Yiisoft\Validator\DataSet\MixedDataSet;
16
use Yiisoft\Validator\DataSet\ObjectDataSet;
17
use Yiisoft\Validator\Rule\Callback;
18
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
19
20
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
21
22
use function is_callable;
23
use function is_int;
24
25
/**
26
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
27
 */
28
final class Validator implements ValidatorInterface
29
{
30
    use PreValidateTrait;
31
32
    /**
33
     * @var callable
34
     */
35
    private $defaultSkipOnEmptyCallback;
36
37 661
    public function __construct(
38
        private RuleHandlerResolverInterface $ruleHandlerResolver,
39
40
        /**
41
         * @var bool|callable|null
42
         */
43
        $defaultSkipOnEmpty = null,
44
        /**
45
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
46
         * {@see validate()} method.
47
         */
48
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
49
        | ReflectionProperty::IS_PROTECTED
50
        | ReflectionProperty::IS_PUBLIC,
51
    ) {
52 661
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
53
    }
54
55
    /**
56
     * @param DataSetInterface|mixed|RulesProviderInterface $data
57
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null $rules
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|iterable<Cl...sProviderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null.
Loading history...
58
     *
59
     * @throws ContainerExceptionInterface
60
     * @throws NotFoundExceptionInterface
61
     */
62 132
    public function validate(
63
        mixed $data,
64
        iterable|RulesProviderInterface|null $rules = null,
65
        ?ValidationContext $context = null,
66
    ): Result {
67 132
        $data = $this->normalizeDataSet($data);
68
69 132
        if ($rules === null && $data instanceof RulesProviderInterface) {
70 26
            $rules = $data->getRules();
71 115
        } elseif ($rules instanceof RulesProviderInterface) {
72 2
            $rules = $rules->getRules();
73 113
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
74
            $rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules();
0 ignored issues
show
Bug introduced by
$rules of type iterable is incompatible with the type object|string expected by parameter $source of Yiisoft\Validator\RulesP...Provider::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            $rules = (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $rules, $this->rulesPropertyVisibility))->getRules();
Loading history...
75
        }
76
77 132
        $context = new ValidationContext(
78 132
            $context?->getValidator() ?? $this,
79 132
            $context?->getDataSet() ?? $data,
80 132
            $context?->getAttribute() ?? null,
81 132
            $context?->getParameters() ?? [],
82
        );
83
84 132
        $compoundResult = new Result();
85
86 132
        foreach ($rules ?? [] as $attribute => $attributeRules) {
87 121
            $tempRule = is_iterable($attributeRules) ? $attributeRules : [$attributeRules];
88 121
            $attributeRules = $this->normalizeRules($tempRule);
89
90 121
            if (is_int($attribute)) {
91 41
                $validatedData = $data->getData();
92
            } else {
93 84
                $validatedData = $data->getAttributeValue($attribute);
94 84
                $context = $context->withAttribute($attribute);
95
            }
96
97 121
            $this->validateInternal(
98
                $validatedData,
99
                $attributeRules,
100
                $context,
101
                $compoundResult,
102
            );
103
        }
104
105 127
        if ($data instanceof PostValidationHookInterface) {
106
            $data->processValidationResult($compoundResult);
107
        }
108
109 127
        return $compoundResult;
110
    }
111
112
    /**
113
     * @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules
114
     */
115 121
    private function validateInternal($value, iterable $rules, ValidationContext $context, Result $compoundResult): void
116
    {
117 121
        foreach ($rules as $rule) {
118 121
            if ($rule instanceof BeforeValidationInterface && $this->preValidate($value, $context, $rule)) {
119 25
                continue;
120
            }
121
122 116
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
123 114
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
124 111
            if ($ruleResult->isValid()) {
125 48
                continue;
126
            }
127
128 87
            $context->setParameter($this->parameterPreviousRulesErrored, true);
129
130 87
            foreach ($ruleResult->getErrors() as $error) {
131 87
                $valuePath = $error->getValuePath();
132 87
                if ($context->getAttribute() !== null) {
133 63
                    $valuePath = [$context->getAttribute(), ...$valuePath];
134
                }
135 87
                $compoundResult->addError($error->getMessage(), $valuePath, $error->getParameters());
136
            }
137
        }
138
    }
139
140
    /**
141
     * @param array $rules
142
     *
143
     * @return iterable<RuleInterface>
144
     */
145 121
    private function normalizeRules(iterable $rules): iterable
146
    {
147 121
        foreach ($rules as $rule) {
148 121
            yield $this->normalizeRule($rule);
149
        }
150
    }
151
152 121
    private function normalizeRule($rule): RuleInterface
153
    {
154 121
        if (is_callable($rule)) {
155 3
            return new Callback($rule);
156
        }
157
158 121
        if (!$rule instanceof RuleInterface) {
159
            throw new InvalidArgumentException(
160
                sprintf(
161
                    'Rule should be either an instance of %s or a callable, %s given.',
162
                    RuleInterface::class,
163
                    get_debug_type($rule)
164
                )
165
            );
166
        }
167
168 121
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
169 116
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
170
        }
171
172 121
        return $rule;
173
    }
174
175 132
    #[Pure]
176
    private function normalizeDataSet($data): DataSetInterface
177
    {
178 132
        if ($data instanceof DataSetInterface) {
179 65
            return $data;
180
        }
181
182 72
        if (is_object($data)) {
183 27
            return new ObjectDataSet($data);
184
        }
185
186 49
        if (is_array($data)) {
187 16
            return new ArrayDataSet($data);
188
        }
189
190 43
        return new MixedDataSet($data);
191
    }
192
}
193