Passed
Pull Request — master (#364)
by
unknown
02:41
created

Validator::validate()   F

Complexity

Conditions 16
Paths 280

Size

Total Lines 70
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 16.5393

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 40
c 3
b 0
f 0
dl 0
loc 70
ccs 34
cts 39
cp 0.8718
rs 3.7333
cc 16
nc 280
nop 2
crap 16.5393

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\Translator\TranslatorInterface;
15
use Yiisoft\Validator\DataSet\ArrayDataSet;
16
use Yiisoft\Validator\DataSet\ObjectDataSet;
17
use Yiisoft\Validator\DataSet\SingleValueDataSet;
18
use Yiisoft\Validator\Rule\Callback;
19
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
20
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
21
22
use function is_array;
23
use function is_callable;
24
use function is_int;
25
use function is_object;
26
use function is_string;
27
28
/**
29
 * Validator validates {@link LimitInterface} against rules set for data set attributes.
30
 */
31
final class Validator implements ValidatorInterface
32
{
33
    use PreValidateTrait;
34
35
    /**
36
     * @var callable
37
     */
38
    private $defaultSkipOnEmptyCallback;
39
40 680
    public function __construct(
41
        private RuleHandlerResolverInterface $ruleHandlerResolver,
42
        private TranslatorInterface $translator,
43
        /**
44
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
45
         * {@see validate()} method.
46
         */
47
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
48
        | ReflectionProperty::IS_PROTECTED
49
        | ReflectionProperty::IS_PUBLIC,
50
51
        bool|callable|null $defaultSkipOnEmpty = null,
52
    ) {
53 680
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
54
    }
55
56
    /**
57
     * @param LimitInterface|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 700
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
64
    {
65 700
        $data = $this->normalizeDataSet($data);
66 700
        if ($rules === null && $data instanceof RulesProviderInterface) {
67 27
            $rules = $data->getRules();
68 677
        } elseif ($rules instanceof RulesProviderInterface) {
69 2
            $rules = $rules->getRules();
70 675
        } elseif ($rules instanceof RuleInterface) {
71 1
            $rules = [$rules];
72 674
        } 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 700
        $compoundResult = new Result();
77 700
        $context = new ValidationContext($this, $data);
78 700
        $results = [];
79
80
        /**
81
         * @var mixed $attribute
82
         * @var iterable<Closure|RuleInterface>|RuleInterface $attributeRules
83
         */
84 700
        foreach ($rules ?? [] as $attribute => $attributeRules) {
85 689
            $result = new Result();
86
87 689
            if (!is_iterable($attributeRules)) {
88 614
                $attributeRules = [$attributeRules];
89
            }
90
91 689
            $attributeRules = $this->normalizeRules($attributeRules);
92
93 689
            if (is_int($attribute)) {
94
                /** @psalm-suppress MixedAssignment */
95 599
                $validatedData = $data->getData();
96 94
            } elseif (is_string($attribute)) {
97
                /** @psalm-suppress MixedAssignment */
98 94
                $validatedData = $data->getAttributeValue($attribute);
99 94
                $context = $context->withAttribute($attribute);
100
            } else {
101
                $message = sprintf(
102
                    'An attribute can only have an integer or a string type. %s given',
103
                    get_debug_type($attribute),
104
                );
105
106
                throw new InvalidArgumentException($message);
107
            }
108
109 689
            $tempResult = $this->validateInternal($validatedData, $attributeRules, $context);
110
111 661
            foreach ($tempResult->getErrors() as $error) {
112 384
                $result->addError($error->getMessage(), $error->getParameters(), $error->getValuePath());
113
            }
114
115 661
            $results[] = $result;
116
        }
117
118 672
        foreach ($results as $result) {
119 661
            foreach ($result->getErrors() as $error) {
120 384
                $compoundResult->addError(
121 384
                    $this->translator->translate($error->getMessage(), $error->getParameters()),
122 384
                    $error->getParameters(),
123 384
                    $error->getValuePath()
124
                );
125
            }
126
        }
127
128 672
        if ($data instanceof PostValidationHookInterface) {
129
            $data->processValidationResult($compoundResult);
130
        }
131
132 672
        return $compoundResult;
133
    }
134
135 700
    #[Pure]
136
    private function normalizeDataSet(mixed $data): DataSetInterface
137
    {
138 700
        if ($data instanceof DataSetInterface) {
139 69
            return $data;
140
        }
141
142 636
        if (is_object($data)) {
143 37
            return new ObjectDataSet($data);
144
        }
145
146 603
        if (is_array($data)) {
147 105
            return new ArrayDataSet($data);
148
        }
149
150 521
        return new SingleValueDataSet($data);
151
    }
152
153
    /**
154
     * @param iterable<RuleInterface> $rules
155
     */
156 689
    private function validateInternal(mixed $value, iterable $rules, ValidationContext $context): Result
157
    {
158 689
        $compoundResult = new Result();
159 689
        foreach ($rules as $rule) {
160 689
            if ($this->preValidate($value, $context, $rule)) {
161 27
                continue;
162
            }
163
164 683
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
165 681
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
166 655
            if ($ruleResult->isValid()) {
167 294
                continue;
168
            }
169
170 384
            $context->setParameter($this->parameterPreviousRulesErrored, true);
171
172 384
            foreach ($ruleResult->getErrors() as $error) {
173 384
                $valuePath = $error->getValuePath();
174 384
                if ($context->getAttribute() !== null) {
175 67
                    $valuePath = [$context->getAttribute(), ...$valuePath];
176
                }
177 384
                $compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePath);
178
            }
179
        }
180 661
        return $compoundResult;
181
    }
182
183
    /**
184
     * @param iterable<Closure|RuleInterface> $rules
185
     *
186
     * @return iterable<RuleInterface>
187
     */
188 689
    private function normalizeRules(iterable $rules): iterable
189
    {
190 689
        foreach ($rules as $rule) {
191 689
            yield $this->normalizeRule($rule);
192
        }
193
    }
194
195 689
    private function normalizeRule(RuleInterface|Closure $rule): RuleInterface
196
    {
197 689
        if (is_callable($rule)) {
198 3
            return new Callback($rule);
199
        }
200
201 689
        if (!$rule instanceof RuleInterface) {
202
            throw new InvalidArgumentException(
203
                sprintf(
204
                    'Rule should be either an instance of %s or a callable, %s given.',
205
                    RuleInterface::class,
206
                    get_debug_type($rule)
207
                )
208
            );
209
        }
210
211 689
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
212 602
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
213
        }
214
215 689
        return $rule;
216
    }
217
}
218