Test Failed
Pull Request — master (#244)
by Dmitriy
02:50
created

CompositeHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 1
b 0
f 0
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\Exception\UnexpectedRuleException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * Can be used to group rules for validation by `skipOnEmpty`, `skipOnError` or `when`.
14
 *
15
 * For example, we have same when closure:
16
 * ```
17
 * 'name' => [
18
 *     new Required(when: fn() => $this->useName),
19
 *     new HasLength(min: 1, max: 50, skipOnEmpty: true, when: fn() => $this->useName),
20
 * ],
21
 * ```
22
 * So we can configure it like this:
23
 * ```
24
 * 'name' => [
25
 *     new Composite(
26
 *         rules: [
27
 *             new Required(),
28
 *             new HasLength(min: 1, max: 50, skipOnEmpty: true),
29
 *         ],
30
 *         when: fn() => $this->useName,
31
 *     ),
32
 * ],
33
 * ```
34
 */
35
final class CompositeHandler implements RuleHandlerInterface
36
{
37
    use PreValidateTrait;
38
39 6
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
40
    {
41 6
        if (!$rule instanceof Composite) {
42 1
            throw new UnexpectedRuleException(Composite::class, $rule);
43
        }
44
45 5
        $context->setParameter($this->parameterPreviousRulesErrored, true);
0 ignored issues
show
Bug introduced by
The method setParameter() 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

45
        $context->/** @scrutinizer ignore-call */ 
46
                  setParameter($this->parameterPreviousRulesErrored, true);

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...
46
47 5
        if ($this->preValidate($value, $context, $rule)) {
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type null; however, parameter $context of Yiisoft\Validator\Rule\C...eHandler::preValidate() does only seem to accept Yiisoft\Validator\ValidationContext, 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

47
        if ($this->preValidate($value, /** @scrutinizer ignore-type */ $context, $rule)) {
Loading history...
48 3
            return new Result();
49
        }
50
51 2
        return $context->getValidator()->validate($value, $rule->getRules());
52
    }
53
}
54