Passed
Pull Request — master (#222)
by Dmitriy
02:35
created

NestedHandler::validate()   C

Complexity

Conditions 12
Paths 26

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 48
ccs 27
cts 27
cp 1
rs 6.9666
cc 12
nc 26
nop 3
crap 12

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 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->rules as $valuePath => $rules) {
59 10
            $result = new Result((string)$valuePath);
60
61 10
            if ($rule->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) {
62 2
                $compoundResult->addError($rule->propertyPathIsNotFoundMessage, ['path' => $valuePath], $valuePath);
63
64 2
                continue;
65
            }
66
67 8
            $rules = is_array($rules) ? $rules : [$rules];
68 8
            $validatedValue = ArrayHelper::getValueByPath($value, $valuePath);
69
70 8
            $itemResult = $context->getValidator()->validate($validatedValue, $rules);
0 ignored issues
show
Bug introduced by
The method getValidator() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $itemResult = $context->/** @scrutinizer ignore-call */ getValidator()->validate($validatedValue, $rules);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72 8
            if ($itemResult->isValid()) {
73 3
                continue;
74
            }
75
76 5
            foreach ($itemResult->getErrors() as $error) {
77 5
                $result->mergeError($error);
78
            }
79 5
            $results[] = $result;
80
        }
81
82 10
        foreach ($results as $result) {
83 5
            foreach ($result->getErrors() as $error) {
84 5
                $compoundResult->mergeError($error);
85
            }
86
        }
87
88 10
        return $compoundResult;
89
    }
90
}
91