Passed
Pull Request — master (#339)
by Sergei
03:22
created

Validator::validate()   D

Complexity

Conditions 15
Paths 270

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 15.0549

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 53
ccs 30
cts 32
cp 0.9375
rs 4.2083
cc 15
nc 270
nop 2
crap 15.0549

How to fix   Long Method    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\ObjectDataSet;
16
use Yiisoft\Validator\DataSet\MixedDataSet;
17
use Yiisoft\Validator\Rule\Callback;
18
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
19
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
20
21
use function is_array;
22
use function is_callable;
23
use function is_int;
24
use function is_object;
25
26
/**
27
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
28
 */
29
final class Validator implements ValidatorInterface
30
{
31
    use PreValidateTrait;
32
33
    /**
34
     * @var callable
35
     */
36
    private $defaultSkipOnEmptyCallback;
37
38 661
    public function __construct(
39
        private RuleHandlerResolverInterface $ruleHandlerResolver,
40
        /**
41
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
42
         * {@see validate()} method.
43
         */
44
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
45
        | ReflectionProperty::IS_PROTECTED
46
        | ReflectionProperty::IS_PUBLIC,
47
48
        /**
49
         * @var bool|callable|null
50
         */
51
        $defaultSkipOnEmpty = null,
52
    ) {
53 661
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
54
    }
55
56
    /**
57
     * @param DataSetInterface|mixed|RulesProviderInterface $data
58
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|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[]>|RuleInterface|RulesProviderInterface|null.
Loading history...
59
     *
60
     * @throws ContainerExceptionInterface
61
     * @throws NotFoundExceptionInterface
62
     */
63 681
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
64
    {
65 681
        $data = $this->normalizeDataSet($data);
66 681
        if ($rules === null && $data instanceof RulesProviderInterface) {
67 26
            $rules = $data->getRules();
68 659
        } elseif ($rules instanceof RulesProviderInterface) {
69 2
            $rules = $rules->getRules();
70 657
        } elseif ($rules instanceof RuleInterface) {
71 1
            $rules = [$rules];
72 656
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
73
            $rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules();
0 ignored issues
show
Bug introduced by
It seems like $rules can also be of type iterable; however, parameter $source of Yiisoft\Validator\RulesP...Provider::__construct() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

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

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