Passed
Pull Request — master (#199)
by Alexander
02:50
created

Each::validateValue()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.1867

Importance

Changes 0
Metric Value
cc 8
eloc 22
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 38
ccs 18
cts 21
cp 0.8571
crap 8.1867
rs 8.4444
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
 * 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
    private ?RuleSet $ruleSet = null;
22
23 8
    public function __construct(
24
        iterable $rules = [],
25
        private string $incorrectInputMessage = 'Value should be array or iterable.',
26
        private string $message = '{error} {value} given.',
27
        ?FormatterInterface $formatter = null,
28
        bool $skipOnEmpty = false,
29
        bool $skipOnError = false,
30
        $when = null,
31
    ) {
32 8
        if ($rules !== []) {
33 8
            $this->ruleSet = new RuleSet($rules);
34
        }
35
36 8
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
37
    }
38
39 4
    protected function validateValue($value, ?ValidationContext $context = null): Result
40
    {
41 4
        if ($this->ruleSet === null) {
42
            throw new InvalidArgumentException('Rules are required.');
43
        }
44
45 4
        $result = new Result();
46 4
        if (!is_iterable($value)) {
47
            $result->addError($this->incorrectInputMessage);
48
49
            return $result;
50
        }
51
52 4
        foreach ($value as $index => $item) {
53 4
            $itemResult = $this->ruleSet->validate($item, $context);
54 4
            if ($itemResult->isValid()) {
55 3
                continue;
56
            }
57
58 4
            foreach ($itemResult->getErrors() as $error) {
59 4
                if (!is_array($item)) {
60 4
                    $errorKey = [$index];
61 4
                    $formatMessage = true;
62
                } else {
63 1
                    $errorKey = [$index, ...$error->getValuePath()];
64 1
                    $formatMessage = false;
65
                }
66
67 4
                $message = !$formatMessage ? $error->getMessage() : $this->formatMessage($this->message, [
68 4
                    'error' => $error->getMessage(),
69
                    'value' => $item,
70
                ]);
71
72 4
                $result->addError($message, $errorKey);
73
            }
74
        }
75
76 4
        return $result;
77
    }
78
79 2
    public function getOptions(): array
80
    {
81 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

81
        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...
82
    }
83
}
84