Passed
Pull Request — master (#240)
by Dmitriy
03:45
created

CompositeHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
use Yiisoft\Validator\ValidatorInterface;
12
13
/**
14
 * Can be used to group rules for validation by `skipOnEmpty`, `skipOnError` or `when`.
15
 *
16
 * For example, we have same when closure:
17
 * ```
18
 * 'name' => [
19
 *     new Required(when: fn() => $this->useName),
20
 *     new HasLength(min: 1, max: 50, skipOnEmpty: true, when: fn() => $this->useName),
21
 * ],
22
 * ```
23
 * So we can configure it like this:
24
 * ```
25
 * 'name' => [
26
 *     new Composite(
27
 *         rules: [
28
 *             new Required(),
29
 *             new HasLength(min: 1, max: 50, skipOnEmpty: true),
30
 *         ],
31
 *         when: fn() => $this->useName,
32
 *     ),
33
 * ],
34
 * ```
35
 */
36
final class CompositeHandler implements RuleHandlerInterface
37
{
38
    use PreValidateTrait;
39
40 50
    public function __construct(private ValidatorInterface $validator)
41
    {
42
    }
43
44 6
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
45
    {
46 6
        if (!$rule instanceof Composite) {
47 1
            throw new UnexpectedRuleException(Composite::class, $rule);
48
        }
49
50 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

50
        $context->/** @scrutinizer ignore-call */ 
51
                  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...
51
52 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

52
        if ($this->preValidate($value, /** @scrutinizer ignore-type */ $context, $rule)) {
Loading history...
53 3
            return new Result();
54
        }
55
56 2
        return $this->validator->validate($value, $rule->getRules());
57
    }
58
}
59