Test Failed
Pull Request — master (#364)
by
unknown
03:02
created

Nested::propagateOptions()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 38
ccs 7
cts 7
cp 1
rs 7.6666
cc 10
nc 20
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\PropagateOptionsInterface;
15
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
16
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
17
use Yiisoft\Validator\Rule\Trait\WhenTrait;
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\SkipOnErrorInterface;
25
use Yiisoft\Validator\ValidationContext;
26
use Yiisoft\Validator\WhenInterface;
27
28
use function array_pop;
29
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...
30
use function implode;
31
use function is_array;
32
use function is_int;
33
use function is_string;
34
use function ltrim;
35
use function rtrim;
36
use function sprintf;
37
38
/**
39
 * Can be used for validation of nested structures.
40
 */
41
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
42
final class Nested implements
43
    SerializableRuleInterface,
44
    SkipOnErrorInterface,
45
    WhenInterface,
46
    SkipOnEmptyInterface,
47
    PropagateOptionsInterface
48
{
49
    use SkipOnEmptyTrait;
50
    use SkipOnErrorTrait;
51
    use WhenTrait;
52
53
    private const SEPARATOR = '.';
54
    private const EACH_SHORTCUT = '*';
55
56
    /**
57
     * @var iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|null
58
     */
59 23
    private ?iterable $rules;
60
61
    /**
62
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|object|RulesProviderInterface|null $rules
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[]>|object|RulesProviderInterface|null.
Loading history...
63
     *
64
     * Rules for validate value that can be described by:
65
     * - object that implement {@see RulesProviderInterface};
66
     * - name of class from whose attributes their will be derived;
67
     * - array or object implementing the `Traversable` interface that contain {@see RuleInterface} implementations
68
     *   or closures.
69
     *
70
     * `$rules` can be null if validatable value is object. In this case rules will be derived from object via
71
     * `getRules()` method if object implement {@see RulesProviderInterface} or from attributes otherwise.
72
     */
73
    public function __construct(
74
        iterable|object|string|null $rules = null,
75
76
        /**
77
         * @var int What visibility levels to use when reading data and rules from validated object.
78
         */
79
        private int $propertyVisibility = ReflectionProperty::IS_PRIVATE
80
        | ReflectionProperty::IS_PROTECTED
81
        | ReflectionProperty::IS_PUBLIC,
82
        /**
83
         * @var int What visibility levels to use when reading rules from the class specified in {@see $rules}
84
         * attribute.
85
         */
86
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
87
        | ReflectionProperty::IS_PROTECTED
88
        | ReflectionProperty::IS_PUBLIC,
89
        private string $noRulesWithNoObjectMessage = 'Nested rule without rules can be used for objects only.',
90
        private string $incorrectDataSetTypeMessage = 'An object data set data can only have an array or an object ' .
91
        'type.',
92
        private string $incorrectInputMessage = 'The value must have an array or an object type.',
93
        private bool $requirePropertyPath = false,
94
        private string $noPropertyPathMessage = 'Property path "{path}" is not found.',
95
        private bool $normalizeRules = true,
96
        private bool $propagateOptions = false,
97
98
        /**
99
         * @var bool|callable|null
100
         */
101
        private $skipOnEmpty = null,
102
        private bool $skipOnError = false,
103 23
104
        /**
105
         * @var Closure(mixed, ValidationContext):bool|null
106 2
         */
107
        private ?Closure $when = null,
108 2
    ) {
109
        $this->prepareRules($rules);
110
    }
111
112
    public function getName(): string
113
    {
114 39
        return 'nested';
115
    }
116 39
117
    /**
118
     * @psalm-suppress InvalidReturnType
119 8
     * @psalm-suppress InvalidReturnStatement
120
     *
121 8
     * @return iterable<iterable<RuleInterface>|RuleInterface>|null
122
     */
123
    public function getRules(): iterable|null
124 33
    {
125
        return $this->rules;
126 33
    }
127
128
    public function getPropertyVisibility(): int
129 9
    {
130
        return $this->propertyVisibility;
131 9
    }
132
133
    public function getNoRulesWithNoObjectMessage(): string
134
    {
135
        return $this->noRulesWithNoObjectMessage;
136
    }
137 23
138
    public function getIncorrectDataSetTypeMessage(): string
139 23
    {
140 14
        return $this->incorrectDataSetTypeMessage;
141
    }
142 14
143
    public function getIncorrectInputMessage(): string
144
    {
145 9
        return $this->incorrectInputMessage;
146 1
    }
147 8
148 4
    public function getRequirePropertyPath(): bool
149
    {
150 4
        return $this->requirePropertyPath;
151
    }
152
153 9
    public function getNoPropertyPathMessage(): string
154 9
    {
155
        return $this->noPropertyPathMessage;
156 8
    }
157
158 8
    /**
159 8
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|object|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[]>|object|RulesProviderInterface|null.
Loading history...
160
     */
161
    private function prepareRules(iterable|object|string|null $source): void
162 7
    {
163 1
        if ($source === null) {
164
            $this->rules = null;
165
166
            return;
167 9
        }
168
169 9
        if ($source instanceof RulesProviderInterface) {
170
            $rules = $source->getRules();
171 9
        } elseif (!$source instanceof Traversable && !is_array($source)) {
172 8
            $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

172
            $rules = (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $source, $this->rulesPropertyVisibility))->getRules();
Loading history...
173 6
        } else {
174 6
            $rules = $source;
175
        }
176 8
177 1
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
178
        self::ensureArrayHasRules($rules);
179
180 1
        /** @var iterable<RuleInterface> $rules */
181
        $this->rules = $rules;
182 1
183
        if ($this->normalizeRules) {
184
            $this->normalizeRules();
185
        }
186
187 8
        if ($this->propagateOptions) {
188
            $this->propagateOptions();
189
        }
190 8
    }
191
192 8
    private static function ensureArrayHasRules(iterable &$rules): void
193 8
    {
194 8
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
195
196 8
        foreach ($rules as &$rule) {
197 7
            if (is_iterable($rule)) {
198 1
                self::ensureArrayHasRules($rule);
199
                continue;
200
            }
201 6
            if (!$rule instanceof RuleInterface) {
202 6
                $message = sprintf(
203
                    'Each rule should be an instance of %s, %s given.',
204
                    RuleInterface::class,
205
                    get_debug_type($rule)
206 6
                );
207 6
                throw new InvalidArgumentException($message);
208
            }
209
        }
210
    }
211
212
    private function normalizeRules(): void
213
    {
214
        if ($this->rules === null) {
215
            return;
216
        }
217
218
        /** @var iterable<RuleInterface> $rules */
219
        $rules = $this->rules;
220
        if ($rules instanceof Traversable) {
221
            $rules = iterator_to_array($rules);
222
        }
223
224
        while (true) {
225
            $breakWhile = true;
226
            $rulesMap = [];
227 7
228
            /** @var int|string $valuePath */
229
            foreach ($rules as $valuePath => $rule) {
230
                if ($valuePath === self::EACH_SHORTCUT) {
231 7
                    throw new InvalidArgumentException('Bare shortcut is prohibited. Use "Each" rule instead.');
232 7
                }
233
234
                $parts = StringHelper::parsePath(
235
                    (string) $valuePath,
236 7
                    delimiter: self::EACH_SHORTCUT,
237
                    preserveDelimiterEscaping: true
238
                );
239 1
                if (count($parts) === 1) {
240
                    continue;
241 1
                }
242 1
243 1
                $breakWhile = false;
244 1
245 1
                $lastValuePath = array_pop($parts);
246
                $lastValuePath = ltrim($lastValuePath, '.');
247 1
                $lastValuePath = str_replace('\\' . self::EACH_SHORTCUT, self::EACH_SHORTCUT, $lastValuePath);
248 1
249
                $remainingValuePath = implode(self::EACH_SHORTCUT, $parts);
250 1
                $remainingValuePath = rtrim($remainingValuePath, self::SEPARATOR);
251 1
252
                if (!isset($rulesMap[$remainingValuePath])) {
253
                    $rulesMap[$remainingValuePath] = [];
254 1
                }
255
256 1
                $rulesMap[$remainingValuePath][$lastValuePath] = $rule;
257 1
                unset($rules[$valuePath]);
258
            }
259
260
            foreach ($rulesMap as $valuePath => $nestedRules) {
261
                $rules[$valuePath] = new Each([new self($nestedRules, normalizeRules: false)]);
262 1
            }
263
264
            if ($breakWhile === true) {
265 5
                break;
266
            }
267
        }
268
269
        $this->rules = $rules;
270
    }
271
272
    public function propagateOptions(): void
273
    {
274
        if ($this->rules === null) {
275 5
            return;
276
        }
277 5
278
        $rules = [];
279 5
        /** @var iterable<RuleInterface> $attributeRules */
280 5
        foreach ($this->rules as $attributeRulesIndex => $attributeRules) {
281 5
            if (!is_int($attributeRulesIndex) && !is_string($attributeRulesIndex)) {
282
                $message = sprintf(
283
                    'A value path can only have an integer or a string type. %s given',
284
                    get_debug_type($attributeRulesIndex),
285 39
                );
286
287 39
                throw new InvalidArgumentException($message);
288
            }
289
290
            foreach ($attributeRules as $attributeRule) {
291
                if ($attributeRule instanceof SkipOnEmptyInterface) {
292
                    $attributeRule = $attributeRule->skipOnEmpty($this->skipOnEmpty);
293
                }
294
                if ($attributeRule instanceof SkipOnErrorInterface) {
295
                    $attributeRule = $attributeRule->skipOnError($this->skipOnError);
296
                }
297
                if ($attributeRule instanceof WhenInterface) {
298
                    $attributeRule = $attributeRule->when($this->when);
299
                }
300
301
                $rules[$attributeRulesIndex][] = $attributeRule;
302
303
                if ($attributeRule instanceof PropagateOptionsInterface) {
304
                    $attributeRule->propagateOptions();
305
                }
306
            }
307
        }
308
309
        $this->rules = $rules;
310
    }
311
312
    #[ArrayShape([
313
        'requirePropertyPath' => 'bool',
314
        'noPropertyPathMessage' => 'array',
315
        'skipOnEmpty' => 'bool',
316
        'skipOnError' => 'bool',
317
        'rules' => 'array|null',
318
    ])]
319
    public function getOptions(): array
320
    {
321
        return [
322
            'noRulesWithNoObjectMessage' => $this->noRulesWithNoObjectMessage,
323
            'incorrectDataSetTypeMessage' => $this->incorrectDataSetTypeMessage,
324
            'incorrectInputMessage' => $this->incorrectInputMessage,
325
            'requirePropertyPath' => $this->getRequirePropertyPath(),
326
            'noPropertyPathMessage' => [
327
                'message' => $this->getNoPropertyPathMessage(),
328
            ],
329
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
330
            'skipOnError' => $this->skipOnError,
331
            'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
332
        ];
333
    }
334
335
    public function getHandlerClassName(): string
336
    {
337
        return NestedHandler::class;
338
    }
339
}
340