Passed
Pull Request — master (#333)
by Sergei
02:38
created

Nested::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 45
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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

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