Passed
Pull Request — master (#314)
by Dmitriy
06:03 queued 03:28
created

Nested::ensureArrayHasRules()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

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

139
            $rules = (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $source, $this->rulesPropertyVisibility))->getRules();
Loading history...
140
        } else {
141 3
            $rules = $source;
142
        }
143
144 8
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
145 8
        self::ensureArrayHasRules($rules);
146
147 7
        return $this->normalizeRules ? $this->normalizeRules($rules) : $rules;
148
    }
149
150 8
    private static function ensureArrayHasRules(iterable &$rules)
151
    {
152 8
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
153
154 8
        foreach ($rules as &$rule) {
155 7
            if (is_iterable($rule)) {
156 5
                self::ensureArrayHasRules($rule);
157 5
                continue;
158
            }
159 7
            if (!$rule instanceof RuleInterface) {
160 1
                $message = sprintf('Each rule should be an instance of %s, %s given.', RuleInterface::class, get_debug_type($rule));
161 1
                throw new InvalidArgumentException($message);
162
            }
163
        }
164
    }
165
166 7
    private function normalizeRules(iterable $rules): iterable
167
    {
168 7
        while (true) {
169 7
            $breakWhile = true;
170 7
            $rulesMap = [];
171
172 7
            foreach ($rules as $valuePath => $rule) {
173 6
                if ($valuePath === self::EACH_SHORTCUT) {
174 1
                    throw new InvalidArgumentException('Bare shortcut is prohibited. Use "Each" rule instead.');
175
                }
176
177 5
                $parts = StringHelper::parsePath(
178 5
                    (string) $valuePath,
179
                    delimiter: self::EACH_SHORTCUT,
180
                    preserveDelimiterEscaping: true
181
                );
182 5
                if (count($parts) === 1) {
183 5
                    continue;
184
                }
185
186
                $breakWhile = false;
187
188
                $lastValuePath = array_pop($parts);
189
                $lastValuePath = ltrim($lastValuePath, '.');
190
                $lastValuePath = str_replace('\\' . self::EACH_SHORTCUT, self::EACH_SHORTCUT, $lastValuePath);
191
192
                $remainingValuePath = implode(self::EACH_SHORTCUT, $parts);
193
                $remainingValuePath = rtrim($remainingValuePath, self::SEPARATOR);
194
195
                if (!isset($rulesMap[$remainingValuePath])) {
196
                    $rulesMap[$remainingValuePath] = [];
197
                }
198
199
                $rulesMap[$remainingValuePath][$lastValuePath] = $rule;
200
                unset($rules[$valuePath]);
201
            }
202
203 6
            foreach ($rulesMap as $valuePath => $nestedRules) {
204
                $rules[$valuePath] = new Each([new self($nestedRules, normalizeRules: false)]);
205
            }
206
207 6
            if ($breakWhile === true) {
208 6
                break;
209
            }
210
        }
211
212 6
        return $rules;
213
    }
214
215 4
    #[ArrayShape([
216
        'requirePropertyPath' => 'bool',
217
        'noPropertyPathMessage' => 'array',
218
        'skipOnEmpty' => 'bool',
219
        'skipOnError' => 'bool',
220
        'rules' => 'array|null',
221
    ])]
222
    public function getOptions(): array
223
    {
224
        return [
225 4
            'requirePropertyPath' => $this->getRequirePropertyPath(),
226
            'noPropertyPathMessage' => [
227 4
                'message' => $this->getNoPropertyPathMessage(),
228
            ],
229 4
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
230 4
            'skipOnError' => $this->skipOnError,
231 4
            'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
232
        ];
233
    }
234
235 24
    public function getHandlerClassName(): string
236
    {
237 24
        return NestedHandler::class;
238
    }
239
}
240