Passed
Pull Request — master (#175)
by
unknown
02:24
created

Each   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 57
ccs 22
cts 25
cp 0.88
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getOptions() 0 3 1
B validateValue() 0 37 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use InvalidArgumentException;
9
use Yiisoft\Validator\FormatterInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule;
12
use Yiisoft\Validator\RuleSet;
13
use Yiisoft\Validator\ValidationContext;
14
15
/**
16
 * Each validator validates an array by checking each of its elements against a set of rules
17
 */
18
#[Attribute(Attribute::TARGET_PROPERTY)]
19
final class Each extends Rule
20
{
21 8
    public function __construct(
22
        private ?RuleSet $ruleSet = null,
23
        private string $incorrectInputMessage = 'Value should be array or iterable.',
24
        private string $message = '{error} {value} given.',
25
        ?FormatterInterface $formatter = null,
26
        bool $skipOnEmpty = false,
27
        bool $skipOnError = false,
28
        $when = null,
29
    ) {
30 8
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
31
    }
32
33 4
    protected function validateValue($value, ?ValidationContext $context = null): Result
34
    {
35 4
        if ($this->ruleSet === null) {
36
            throw new InvalidArgumentException('Rule set is required.');
37
        }
38
39 4
        $result = new Result();
40 4
        if (!is_iterable($value)) {
41
            $result->addError($this->incorrectInputMessage);
42
            return $result;
43
        }
44
45 4
        foreach ($value as $index => $item) {
46 4
            $itemResult = $this->ruleSet->validate($item, $context);
47 4
            if ($itemResult->isValid()) {
48 3
                continue;
49
            }
50
51 4
            foreach ($itemResult->getErrors() as $error) {
52 4
                if (!is_array($item)) {
53 4
                    $errorKey = [$index];
54 4
                    $formatMessage = true;
55
                } else {
56 1
                    $errorKey = [$index, ...$error->getValuePath()];
57 1
                    $formatMessage = false;
58
                }
59
60 4
                $message = !$formatMessage ? $error->getMessage() : $this->formatMessage($this->message, [
61 4
                    'error' => $error->getMessage(),
62
                    'value' => $item,
63
                ]);
64
65 4
                $result->addError($message, $errorKey);
66
            }
67
        }
68
69 4
        return $result;
70
    }
71
72 2
    public function getOptions(): array
73
    {
74 2
        return $this->ruleSet->asArray();
0 ignored issues
show
Bug introduced by
The method asArray() 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

74
        return $this->ruleSet->/** @scrutinizer ignore-call */ asArray();

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...
75
    }
76
}
77