Test Failed
Pull Request — master (#222)
by Rustam
14:22 queued 33s
created

EachHandler::validate()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 36
rs 8.8333
c 0
b 0
f 0
cc 7
nc 6
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\RuleInterface;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\Exception\UnexpectedRuleException;
12
13
/**
14
 * Validates an array by checking each of its elements against a set of rules.
15
 */
16
final class EachHandler implements RuleHandlerInterface
17
{
18
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
19
    {
20
        if (!$rule instanceof Each) {
21
            throw new UnexpectedRuleException(Each::class, $rule);
22
        }
23
24
        /**
25
         * @var iterable<RuleInterface> $rules
26
         */
27
        $rules = $rule->rules;
28
        if ($rules === []) {
29
            throw new InvalidArgumentException('Rules are required.');
30
        }
31
32
        $result = new Result();
33
        if (!is_iterable($value)) {
34
            $result->addError($rule->incorrectInputMessage);
35
36
            return $result;
37
        }
38
39
        foreach ($value as $index => $item) {
40
            /**
41
             * @psalm-suppress InvalidArgument
42
             */
43
            $itemResult = $context->getValidator()->validate($item, [$index => $rules]);
0 ignored issues
show
Bug introduced by
The method getValidator() does not exist on null. ( Ignorable by Annotation )

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

43
            $itemResult = $context->/** @scrutinizer ignore-call */ getValidator()->validate($item, [$index => $rules]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            if ($itemResult->isValid()) {
45
                continue;
46
            }
47
48
            foreach ($itemResult->getErrors() as $error) {
49
                $result->mergeError($error);
50
            }
51
        }
52
53
        return $result;
54
    }
55
}
56