Test Failed
Pull Request — master (#279)
by
unknown
02:27
created

Nested::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use InvalidArgumentException;
10
use JetBrains\PhpStorm\ArrayShape;
11
use Traversable;
12
use Yiisoft\Arrays\ArrayHelper;
13
use Yiisoft\Validator\BeforeValidationInterface;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\RuleInterface;
17
use Yiisoft\Validator\RulesDumper;
18
use Yiisoft\Validator\SerializableRuleInterface;
19
use Yiisoft\Validator\ValidationContext;
20
21
use function array_pop;
22
use function count;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Validator\Rule\count. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use function implode;
24
use function is_array;
25
use function ltrim;
26
use function rtrim;
27
use function sprintf;
28
use function strlen;
29
30
/**
31
 * Can be used for validation of nested structures.
32 4
 */
33
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
34
final class Nested implements SerializableRuleInterface, BeforeValidationInterface
35
{
36
    use BeforeValidationTrait;
37
    use RuleNameTrait;
38
39
    private const SEPARATOR = '.';
40
    private const EACH_SHORTCUT = '*';
41
42
    public function __construct(
43
        /**
44
         * @var iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
45
         */
46 4
        private iterable $rules = [],
47 4
        private bool $requirePropertyPath = false,
48 1
        private string $noPropertyPathMessage = 'Property path "{path}" is not found.',
49
        private bool $normalizeRules = true,
50
        private bool $skipOnEmpty = false,
51 3
        private bool $skipOnError = false,
52 1
        /**
53 1
         * @var Closure(mixed, ValidationContext):bool|null
54
         */
55
        private ?Closure $when = null,
56 2
    ) {
57
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
58
        if (empty($rules)) {
59
            throw new InvalidArgumentException('Rules must not be empty.');
60
        }
61
62 16
        if (self::checkRules($rules)) {
63
            $message = sprintf('Each rule should be an instance of %s.', RuleInterface::class);
64 16
            throw new InvalidArgumentException($message);
65
        }
66
67
        $this->rules = $rules;
68
69
        if ($this->normalizeRules === true) {
70 20
            $this->normalizeRules();
71
        }
72 20
    }
73
74
    /**
75
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
76
     */
77
    public function getRules(): iterable
78 7
    {
79
        return $this->rules;
80 7
    }
81
82
    /**
83 3
     * @return bool
84
     */
85 3
    public function getRequirePropertyPath(): bool
86
    {
87 3
        return $this->requirePropertyPath;
88 3
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getNoPropertyPathMessage(): string
94 4
    {
95
        return $this->noPropertyPathMessage;
96
    }
97
98
    private static function checkRules($rules): bool
99
    {
100
        return array_reduce(
101
            $rules,
102
            function (bool $carry, $rule) {
103
                return $carry || (is_array($rule) ? self::checkRules($rule) : !$rule instanceof RuleInterface);
104 4
            },
105
            false
106 4
        );
107
    }
108 4
109 4
    private function normalizeRules(): void
110 4
    {
111
        /** @var iterable $rules */
112
        $rules = $this->getRules();
113
        while (true) {
114 6
            $breakWhile = true;
115
            $rulesMap = [];
116 6
117
            foreach ($rules as $valuePath => $rule) {
118
                if ($valuePath === self::EACH_SHORTCUT) {
119
                    throw new InvalidArgumentException('Bare shortcut is prohibited. Use "Each" rule instead.');
120
                }
121
122
                $parts = ArrayHelper::parsePath((string) $valuePath, self::EACH_SHORTCUT, false);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Arrays\ArrayHelper::parsePath() has too many arguments starting with false. ( Ignorable by Annotation )

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

122
                /** @scrutinizer ignore-call */ 
123
                $parts = ArrayHelper::parsePath((string) $valuePath, self::EACH_SHORTCUT, false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
123
                if (count($parts) === 1) {
124
                    continue;
125
                }
126
127
                $breakWhile = false;
128
129
                $lastValuePath = array_pop($parts);
130
                $lastValuePath = ltrim($lastValuePath, '.');
131
                $lastValuePath = str_replace('\\' . self::EACH_SHORTCUT, self::EACH_SHORTCUT, $lastValuePath);
132
133
                $remainingValuePath = implode(self::EACH_SHORTCUT, $parts);
134
                $remainingValuePath = rtrim($remainingValuePath, self::SEPARATOR);
135
136
                if (!isset($rulesMap[$remainingValuePath])) {
137
                    $rulesMap[$remainingValuePath] = [];
138
                }
139
140
                $rulesMap[$remainingValuePath][$lastValuePath] = $rule;
141
                unset($rules[$valuePath]);
142
            }
143
144
            foreach ($rulesMap as $valuePath => $nestedRules) {
145
                $rules[$valuePath] = new Each([new self($nestedRules, normalizeRules: false)]);
146
            }
147
148
            if ($breakWhile === true) {
149
                break;
150
            }
151
        }
152
153
        $this->rules = $rules;
154
    }
155
156
    #[ArrayShape([
157
        'requirePropertyPath' => 'bool',
158
        'noPropertyPathMessage' => 'array',
159
        'skipOnEmpty' => 'bool',
160
        'skipOnError' => 'bool',
161
        'rules' => 'array',
162
    ])]
163
    public function getOptions(): array
164
    {
165
        return [
166
            'requirePropertyPath' => $this->getRequirePropertyPath(),
167
            'noPropertyPathMessage' => [
168
                'message' => $this->getNoPropertyPathMessage(),
169
            ],
170
            'skipOnEmpty' => $this->skipOnEmpty,
171
            'skipOnError' => $this->skipOnError,
172
            'rules' => (new RulesDumper())->asArray($this->rules),
173
        ];
174
    }
175
176
    public function getHandlerClassName(): string
177
    {
178
        return NestedHandler::class;
179
    }
180
}
181