Passed
Pull Request — master (#317)
by
unknown
02:26
created

Nested::propagateOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.9666
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Error;
10
use InvalidArgumentException;
11
use JetBrains\PhpStorm\ArrayShape;
12
use ReflectionProperty;
13
use Traversable;
14
use Yiisoft\Strings\StringHelper;
15
use Yiisoft\Validator\BeforeValidationInterface;
16
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
17
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
18
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
19
use Yiisoft\Validator\RuleInterface;
20
use Yiisoft\Validator\RulesDumper;
21
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
22
use Yiisoft\Validator\RulesProviderInterface;
23
use Yiisoft\Validator\SerializableRuleInterface;
24
use Yiisoft\Validator\SkipOnEmptyInterface;
25
use Yiisoft\Validator\ValidationContext;
26
27
use function array_pop;
28
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...
29
use function implode;
30
use function is_array;
31
use function ltrim;
32
use function rtrim;
33
use function sprintf;
34
35
/**
36
 * Can be used for validation of nested structures.
37
 */
38
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
39
final class Nested implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
40
{
41
    use BeforeValidationTrait;
42
    use RuleNameTrait;
43
    use SkipOnEmptyTrait;
44
45
    private const SEPARATOR = '.';
46
    private const EACH_SHORTCUT = '*';
47
48
    /**
49
     * @var iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
50
     */
51
    private ?iterable $rules;
52
53 23
    public function __construct(
54
        /**
55
         * Rules for validate value that can be described by:
56
         * - object that implement {@see RulesProviderInterface};
57
         * - name of class from whose attributes their will be derived;
58
         * - array or object implementing the `Traversable` interface that contain {@see RuleInterface} implementations
59
         *   or closures.
60
         *
61
         * `$rules` can be null if validatable value is object. In this case rules will be derived from object via
62
         * `getRules()` method if object implement {@see RulesProviderInterface} or from attributes otherwise.
63
         *
64
         * @var class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null
65
         */
66
        iterable|object|string|null $rules = null,
67
68
        /**
69
         * @var int What visibility levels to use when reading data and rules from validated object.
70
         */
71
        private int $propertyVisibility = ReflectionProperty::IS_PRIVATE
72
        | ReflectionProperty::IS_PROTECTED
73
        | ReflectionProperty::IS_PUBLIC,
74
        /**
75
         * @var int What visibility levels to use when reading rules from the class specified in {@see $rules}
76
         * attribute.
77
         */
78
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
79
        | ReflectionProperty::IS_PROTECTED
80
        | ReflectionProperty::IS_PUBLIC,
81
        private bool $requirePropertyPath = false,
82
        private string $noPropertyPathMessage = 'Property path "{path}" is not found.',
83
        private bool $normalizeRules = true,
84
        private bool $propagateOptions = false,
85
86
        /**
87
         * @var bool|callable|null
88
         */
89
        private $skipOnEmpty = null,
90
        private bool $skipOnError = false,
91
92
        /**
93
         * @var Closure(mixed, ValidationContext):bool|null
94
         */
95
        private ?Closure $when = null,
96
    ) {
97 23
        $this->prepareRules($rules);
98
    }
99
100
    /**
101
     * @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
102
     */
103 39
    public function getRules(): ?iterable
104
    {
105 39
        return $this->rules;
106
    }
107
108 8
    public function getPropertyVisibility(): int
109
    {
110 8
        return $this->propertyVisibility;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116 33
    public function getRequirePropertyPath(): bool
117
    {
118 33
        return $this->requirePropertyPath;
119
    }
120
121
    /**
122
     * @return string
123
     */
124 9
    public function getNoPropertyPathMessage(): string
125
    {
126 9
        return $this->noPropertyPathMessage;
127
    }
128
129
    /**
130
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null $source
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|iterable<Cl...sProviderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null.
Loading history...
131
     */
132 23
    private function prepareRules(iterable|object|string|null $source): void
133
    {
134 23
        if ($source === null) {
135 14
            $this->rules = null;
136
137 14
            return;
138
        }
139
140 9
        if ($source instanceof RulesProviderInterface) {
141 1
            $rules = $source->getRules();
142 8
        } elseif (!$source instanceof Traversable && !is_array($source)) {
143 4
            $rules = (new AttributesRulesProvider($source, $this->rulesPropertyVisibility))->getRules();
0 ignored issues
show
Bug introduced by
It seems like $source can also be of type 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

143
            $rules = (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $source, $this->rulesPropertyVisibility))->getRules();
Loading history...
144
        } else {
145 4
            $rules = $source;
146
        }
147
148 9
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
149 9
        if (self::checkRules($rules)) {
150 1
            $message = sprintf('Each rule should be an instance of %s.', RuleInterface::class);
151 1
            throw new InvalidArgumentException($message);
152
        }
153
154 8
        $this->rules = $rules;
155
156 8
        if ($this->normalizeRules) {
157 8
            $this->normalizeRules();
158
        }
159
160 7
        if ($this->propagateOptions) {
161 1
            $this->propagateOptions();
162
        }
163
    }
164
165 9
    private static function checkRules($rules): bool
166
    {
167 9
        return array_reduce(
168
            $rules,
169 9
            function (bool $carry, $rule) {
170 8
                return $carry || (is_array($rule) ? self::checkRules($rule) : !$rule instanceof RuleInterface);
171
            },
172
            false
173
        );
174
    }
175
176 8
    private function normalizeRules(): void
177
    {
178 8
        $rules = $this->rules;
179
180 8
        while (true) {
181 8
            $breakWhile = true;
182 8
            $rulesMap = [];
183
184 8
            foreach ($rules as $valuePath => $rule) {
185 7
                if ($valuePath === self::EACH_SHORTCUT) {
186 1
                    throw new InvalidArgumentException('Bare shortcut is prohibited. Use "Each" rule instead.');
187
                }
188
189 6
                $parts = StringHelper::parsePath(
190 6
                    (string) $valuePath,
191
                    delimiter: self::EACH_SHORTCUT,
192
                    preserveDelimiterEscaping: true
193
                );
194 6
                if (count($parts) === 1) {
195 6
                    continue;
196
                }
197
198
                $breakWhile = false;
199
200
                $lastValuePath = array_pop($parts);
201
                $lastValuePath = ltrim($lastValuePath, '.');
202
                $lastValuePath = str_replace('\\' . self::EACH_SHORTCUT, self::EACH_SHORTCUT, $lastValuePath);
203
204
                $remainingValuePath = implode(self::EACH_SHORTCUT, $parts);
205
                $remainingValuePath = rtrim($remainingValuePath, self::SEPARATOR);
206
207
                if (!isset($rulesMap[$remainingValuePath])) {
208
                    $rulesMap[$remainingValuePath] = [];
209
                }
210
211
                $rulesMap[$remainingValuePath][$lastValuePath] = $rule;
212
                unset($rules[$valuePath]);
213
            }
214
215 7
            foreach ($rulesMap as $valuePath => $nestedRules) {
216
                $rules[$valuePath] = new Each([new self($nestedRules, normalizeRules: false)]);
217
            }
218
219 7
            if ($breakWhile === true) {
220 7
                break;
221
            }
222
        }
223
224 7
        $this->rules = $rules;
225
    }
226
227 1
    public function propagateOptions(): void
228
    {
229 1
        foreach ($this->rules as $attributeRulesIndex => $attributeRules) {
230 1
            foreach ($attributeRules as $index => $attributeRule) {
231 1
                $attributeRule = $attributeRule->skipOnEmpty($this->skipOnEmpty);
232 1
                $attributeRule = $attributeRule->skipOnError($this->skipOnError);
233
234 1
                $this->rules[$attributeRulesIndex][$index] = $attributeRule;
235
236
                try {
237 1
                    $attributeRule->propagateOptions();
238 1
                } catch (Error) {
239 1
                    continue;
240
                }
241
            }
242
        }
243
    }
244
245 5
    #[ArrayShape([
246
        'requirePropertyPath' => 'bool',
247
        'noPropertyPathMessage' => 'array',
248
        'skipOnEmpty' => 'bool',
249
        'skipOnError' => 'bool',
250
        'rules' => 'array|null',
251
    ])]
252
    public function getOptions(): array
253
    {
254
        return [
255 5
            'requirePropertyPath' => $this->getRequirePropertyPath(),
256
            'noPropertyPathMessage' => [
257 5
                'message' => $this->getNoPropertyPathMessage(),
258
            ],
259 5
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
260 5
            'skipOnError' => $this->skipOnError,
261 5
            'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
262
        ];
263
    }
264
265 24
    public function getHandlerClassName(): string
266
    {
267 24
        return NestedHandler::class;
268
    }
269
}
270