Passed
Pull Request — master (#222)
by Alexander
04:26 queued 02:14
created

Nested::validateValue()   B

Complexity

Conditions 11
Paths 7

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
nc 7
nop 2
dl 0
loc 39
ccs 24
cts 24
cp 1
crap 11
rs 7.3166
c 1
b 0
f 0

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 Traversable;
11
use Yiisoft\Validator\PreValidatableRuleInterface;
12
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 12 at column 27
Loading history...
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
15
use Yiisoft\Validator\ParametrizedRuleInterface;
16
use Yiisoft\Validator\RuleInterface;
17
use Yiisoft\Validator\RulesDumper;
18
use function is_array;
19
20
/**
21
 * Can be used for validation of nested structures.
22
 */
23
#[Attribute(Attribute::TARGET_PROPERTY)]
24
final class Nested implements ParametrizedRuleInterface, PreValidatableRuleInterface
25
{
26
    use HandlerClassNameTrait;
27
    use PreValidatableTrait;
28
    use RuleNameTrait;
29
30 3
    public function __construct(
31
        /**
32
         * @var RuleInterface[][]
33
         */
34
        private iterable $rules = [],
35
        private bool $errorWhenPropertyPathIsNotFound = false,
36
        private string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.',
37
        private bool $skipOnEmpty = false,
38
        private bool $skipOnError = false,
39
        private ?Closure $when = null,
40
    ) {
41 3
        $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules;
42 3
        if (empty($rules)) {
43 1
            throw new InvalidArgumentException('Rules must not be empty.');
44
        }
45
46 2
        if ($this->checkRules($rules)) {
47 1
            $message = sprintf('Each rule should be an instance of %s.', RuleInterface::class);
48 1
            throw new InvalidArgumentException($message);
49
        }
50
51 1
        $this->rules = $rules;
52
    }
53
54
    /**
55
     * @return iterable
56
     */
57 10
    public function getRules(): iterable
58
    {
59 10
        return $this->rules;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65 10
    public function isErrorWhenPropertyPathIsNotFound(): bool
66
    {
67 10
        return $this->errorWhenPropertyPathIsNotFound;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 2
    public function getPropertyPathIsNotFoundMessage(): string
74
    {
75 2
        return $this->propertyPathIsNotFoundMessage;
76
    }
77
78 2
    private function checkRules(array $rules): bool
79
    {
80 2
        return array_reduce(
81
            $rules,
82 2
            function (bool $carry, $rule) {
83 2
                return $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface);
84
            },
85
            false
86
        );
87
    }
88
89 4
    public function getOptions(): array
90
    {
91 4
        return (new RulesDumper())->asArray($this->rules);
92
    }
93
}
94