Passed
Push — master ( 8c7a41...68be67 )
by Sergei
02:44
created

RulesNormalizer::normalizeRulesGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Helper;
6
7
use InvalidArgumentException;
8
use ReflectionException;
9
use Yiisoft\Validator\RuleInterface;
10
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
11
use Yiisoft\Validator\RulesProviderInterface;
12
use Yiisoft\Validator\ValidatorInterface;
13
14
use function is_callable;
15
use function is_int;
16
use function is_string;
17
18
/**
19
 * @psalm-import-type RulesType from ValidatorInterface
20
 */
21
final class RulesNormalizer
22
{
23
    /**
24
     * @psalm-param RulesType $rules
25
     *
26
     * @throws InvalidArgumentException
27
     * @throws ReflectionException
28
     *
29
     * @return iterable<int|string, iterable<int, RuleInterface>>
30 824
     */
31
    public static function normalize(
32
        callable|iterable|object|string|null $rules,
33
        mixed $data = null,
34
        ?callable $defaultSkipOnEmptyCriteria = null,
35 824
    ): iterable {
36
        $rules = self::prepareRulesArray($rules, $data);
37 823
38
        $normalizedRules = [];
39
40
        /**
41
         * @var mixed $attribute
42
         * @var mixed $attributeRules
43 823
         */
44 811
        foreach ($rules as $attribute => $attributeRules) {
45 1
            if (!is_int($attribute) && !is_string($attribute)) {
46 1
                throw new InvalidArgumentException(
47
                    sprintf(
48 1
                        'An attribute can only have an integer or a string type. %s given.',
49
                        get_debug_type($attribute),
50
                    )
51
                );
52
            }
53 810
54 810
            $normalizedRules[$attribute] = new RulesNormalizerIterator(
55
                is_iterable($attributeRules) ? $attributeRules : [$attributeRules],
56
                $defaultSkipOnEmptyCriteria
57
            );
58
        }
59 822
60
        return $normalizedRules;
61
    }
62
63
    /**
64
     * @throws InvalidArgumentException
65
     *
66
     * @return iterable<int, RuleInterface>
67 824
     */
68
    public static function normalizeList(iterable|callable|RuleInterface $rules): iterable
69
    {
70
        return new RulesNormalizerIterator(
71 824
            is_iterable($rules) ? $rules : [$rules]
0 ignored issues
show
Bug introduced by
It seems like is_iterable($rules) ? $rules : array($rules) can also be of type Yiisoft\Validator\RuleInterface and callable; however, parameter $rules of Yiisoft\Validator\Helper...Iterator::__construct() does only seem to accept iterable, 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

71
            /** @scrutinizer ignore-type */ is_iterable($rules) ? $rules : [$rules]
Loading history...
72 34
        );
73 30
    }
74 34
75
    /**
76
     * @psalm-param RulesType $rules
77 794
     *
78 2
     * @throws ReflectionException
79
     */
80
    private static function prepareRulesArray(
81 792
        callable|iterable|object|string|null $rules,
82 1
        mixed $data,
83
    ): iterable {
84
        if ($rules === null) {
85
            return $data instanceof RulesProviderInterface
86 791
                ? $data->getRules()
87 790
                : [];
88
        }
89
90 1
        if ($rules instanceof RulesProviderInterface) {
91
            return $rules->getRules();
92
        }
93
94
        if ($rules instanceof RuleInterface || is_callable($rules)) {
95
            return [$rules];
96
        }
97
98 810
        if (is_iterable($rules)) {
99
            return $rules;
100
        }
101 810
102 810
        return (new AttributesRulesProvider($rules))->getRules();
0 ignored issues
show
Bug introduced by
It seems like $rules can also be of type callable and 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

102
        return (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $rules))->getRules();
Loading history...
103
    }
104
}
105