Passed
Pull Request — master (#222)
by Alexander
04:31 queued 02:12
created

GroupRuleHandler::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 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\Formatter;
9
use Yiisoft\Validator\FormatterInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Validates a single value for a set of custom rules.
15
 */
16
class GroupRuleHandler implements RuleHandlerInterface
17
{
18
    private ?FormatterInterface $formatter;
19
20 6
    public function __construct(?FormatterInterface $formatter = null)
21
    {
22 6
        $this->formatter = $formatter ?? new Formatter();
23
    }
24
25 6
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
26
    {
27 6
        if (!$rule instanceof GroupRule) {
28 1
            throw new UnexpectedRuleException(GroupRule::class, $rule);
29
        }
30
31 5
        $result = new Result();
32 5
        if (!$context?->getValidator()->validate($value, $rule->getRuleSet())->isValid()) {
33 4
            $formattedMessage = $this->formatter->format(
0 ignored issues
show
Bug introduced by
The method format() 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

33
            /** @scrutinizer ignore-call */ 
34
            $formattedMessage = $this->formatter->format(

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...
34 4
                $rule->getMessage(),
35 4
                ['attribute' => $context?->getAttribute(), 'value' => $value]
36
            );
37 4
            $result->addError($formattedMessage);
38
        }
39
40 5
        return $result;
41
    }
42
}
43