Passed
Pull Request — master (#296)
by Sergei
02:33
created

EmbeddedHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
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\DataSet\ObjectDataSet;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Formatter;
10
use Yiisoft\Validator\FormatterInterface;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\RuleHandlerInterface;
13
use Yiisoft\Validator\ValidationContext;
14
15
final class EmbeddedHandler implements RuleHandlerInterface
16
{
17 4
    public function __construct(
18
        private ?FormatterInterface $formatter = null
19
    ) {
20 4
        $this->formatter = $formatter ?? new Formatter();
21
    }
22
23 4
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
24
    {
25 4
        if (!$rule instanceof Embedded) {
26
            throw new UnexpectedRuleException(Embedded::class, $rule);
27
        }
28
29 4
        if (!is_object($value)) {
30
            $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

30
            /** @scrutinizer ignore-call */ 
31
            $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...
31
                'Value should be an object, {type} given.',
32
                [
33
                    'attribute' => $context->getAttribute(),
34
                    'value' => $value,
35
                    'type' => get_debug_type($value),
36
                ]
37
            );
38
            return (new Result())->addError($formattedMessage);
39
        }
40
41 4
        $dataSet = new ObjectDataSet($value, $rule->getPropertyVisibility());
42
43 4
        return $context->getValidator()->validate($dataSet);
44
    }
45
}
46