Passed
Pull Request — master (#320)
by Dmitriy
12:15 queued 09:25
created

HasLengthHandler::__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 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
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\LimitHandlerTrait;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
use function is_string;
14
15
/**
16
 * Validates that the value is of certain length.
17
 *
18
 * Note, this rule should only be used with strings.
19
 */
20
final class HasLengthHandler implements RuleHandlerInterface
21
{
22
    use LimitHandlerTrait;
23
24 60
    public function validate($value, object $rule, ?ValidationContext $context = null): Result
25
    {
26 60
        if (!$rule instanceof HasLength) {
27 1
            throw new UnexpectedRuleException(HasLength::class, $rule);
28
        }
29
30 59
        $result = new Result();
31
32 59
        if (!is_string($value)) {
33 5
            $result->addError(
34 5
                message: $rule->getMessage(),
35 5
                parameters: ['value' => $value]
36
            );
37 5
            return $result;
38
        }
39
40 54
        $length = mb_strlen($value, $rule->getEncoding());
41 54
        $this->validateLimits($value, $rule, $context, $length, $result);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type null; however, parameter $context of Yiisoft\Validator\Rule\H...ndler::validateLimits() 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

41
        $this->validateLimits($value, $rule, /** @scrutinizer ignore-type */ $context, $length, $result);
Loading history...
42
43 54
        return $result;
44
    }
45
}
46