Passed
Pull Request — master (#222)
by Alexander
02:25
created

NestedHandler::validate()   C

Complexity

Conditions 14
Paths 44

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 14

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 55
ccs 32
cts 32
cp 1
rs 6.2666
cc 14
nc 44
nop 3
crap 14

How to fix   Long Method    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 Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\ValidationContext;
10
use function is_array;
11
use function is_object;
12
use Yiisoft\Validator\Exception\UnexpectedRuleException;
13
14
/**
15
 * Can be used for validation of nested structures.
16
 *
17
 * For example, we have an inbound request with the following structure:
18
 *
19
 * ```php
20
 * $request = [
21
 *     'author' => [
22
 *         'name' => 'Dmitry',
23
 *         'age' => 18,
24
 *     ],
25
 * ];
26
 * ```
27
 *
28
 * So to make validation we can configure it like this:
29
 *
30
 * ```php
31
 * $rule = new Nested([
32
 *     'author' => new Nested([
33
 *         'name' => [new HasLength(min: 3)],
34
 *         'age' => [new Number(min: 18)],
35
 *     )];
36
 * ]);
37
 * ```
38
 */
39
final class NestedHandler implements RuleHandlerInterface
40
{
41 12
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
42
    {
43 12
        if (!$rule instanceof Nested) {
44 1
            throw new UnexpectedRuleException(Nested::class, $rule);
45
        }
46
47 11
        $compoundResult = new Result();
48 11
        if (!is_object($value) && !is_array($value)) {
49 1
            $message = sprintf('Value should be an array or an object. %s given.', gettype($value));
50 1
            $compoundResult->addError($message);
51
52 1
            return $compoundResult;
53
        }
54
55 10
        $value = (array)$value;
56
57 10
        $results = [];
58 10
        foreach ($rule->getRules() as $valuePath => $rules) {
59 10
            $result = new Result();
60
61 10
            if ($rule->isErrorWhenPropertyPathIsNotFound() && !ArrayHelper::pathExists($value, $valuePath)) {
62 2
                $compoundResult->addError(
63 2
                    $rule->getPropertyPathIsNotFoundMessage(),
64 2
                    [$valuePath, 'path' => $valuePath]
65
                );
66
67 2
                continue;
68
            }
69
70 8
            $rules = is_array($rules) ? $rules : [$rules];
71 8
            $validatedValue = ArrayHelper::getValueByPath($value, $valuePath);
72
73 8
            $itemResult = $context?->getValidator()->validate($validatedValue, $rules);
74
75 8
            if ($itemResult->isValid()) {
76 3
                continue;
77
            }
78
79 5
            foreach ($itemResult->getErrors() as $error) {
80 5
                $errorValuePath = is_int($valuePath) ? [$valuePath] : explode('.', $valuePath);
81 5
                if ($error->getValuePath()) {
82 3
                    $errorValuePath = array_merge($errorValuePath, $error->getValuePath());
83
                }
84 5
                $result->addError($error->getMessage(), $errorValuePath);
85
            }
86 5
            $results[] = $result;
87
        }
88
89 10
        foreach ($results as $result) {
90 5
            foreach ($result->getErrors() as $error) {
91 5
                $compoundResult->addError($error->getMessage(), $error->getValuePath());
92
            }
93
        }
94
95 10
        return $compoundResult;
96
    }
97
}
98