Passed
Pull Request — master (#222)
by Dmitriy
02:23
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 Yiisoft\Validator\ValidatorInterface;
11
use function is_array;
12
use function is_object;
13
use Yiisoft\Validator\Exception\UnexpectedRuleException;
14
15
/**
16
 * Can be used for validation of nested structures.
17
 *
18
 * For example, we have an inbound request with the following structure:
19
 *
20
 * ```php
21
 * $request = [
22
 *     'author' => [
23
 *         'name' => 'Dmitry',
24
 *         'age' => 18,
25
 *     ],
26
 * ];
27
 * ```
28
 *
29
 * So to make validation we can configure it like this:
30
 *
31
 * ```php
32
 * $rule = new Nested([
33
 *     'author' => new Nested([
34
 *         'name' => [new HasLength(min: 3)],
35
 *         'age' => [new Number(min: 18)],
36
 *     )];
37
 * ]);
38
 * ```
39
 */
40
final class NestedHandler implements RuleHandlerInterface
41
{
42 12
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
43
    {
44 12
        if (!$rule instanceof Nested) {
45 1
            throw new UnexpectedRuleException(Nested::class, $rule);
46
        }
47
48 11
        $compoundResult = new Result();
49 11
        if (!is_object($value) && !is_array($value)) {
50 1
            $message = sprintf('Value should be an array or an object. %s given.', gettype($value));
51 1
            $compoundResult->addError($message);
52
53 1
            return $compoundResult;
54
        }
55
56 10
        $value = (array)$value;
57
58 10
        $results = [];
59 10
        foreach ($rule->rules as $valuePath => $rules) {
60 10
            $result = new Result((string)$valuePath);
61
62 10
            if ($rule->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) {
63 2
                $compoundResult->addError($rule->propertyPathIsNotFoundMessage, ['path' => $valuePath], $valuePath);
64
65 2
                continue;
66
            }
67
68 8
            $rules = is_array($rules) ? $rules : [$rules];
69 8
            $validatedValue = ArrayHelper::getValueByPath($value, $valuePath);
70
71 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

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