Passed
Pull Request — master (#248)
by Rustam
03:00
created

NotEqualHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 26
c 1
b 0
f 0
dl 0
loc 50
ccs 23
cts 25
cp 0.92
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 24 3
A isNotEqual() 0 15 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
 * Checks if the specified value is not equal to another value or attribute.
15
 *
16
 * The value being validated with {@see NotEqual::$equalValue} or {@see NotEqual::$equalAttribute}, which
17
 * is set in the constructor.
18
 *
19
 * The default validation function is based on string values, which means the values
20
 * are compared byte by byte. When validating numbers, make sure to change {@see NotEqual::$type} to
21
 * {@see NotEqual::TYPE_NUMBER} to enable numeric validation.
22
 */
23
final class NotEqualHandler implements RuleHandlerInterface
24
{
25
    private FormatterInterface $formatter;
26
27 6
    public function __construct(?FormatterInterface $formatter = null)
28
    {
29 6
        $this->formatter = $formatter ?? new Formatter();
30
    }
31
32 6
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
33
    {
34 6
        if (!$rule instanceof NotEqual) {
35 1
            throw new UnexpectedRuleException(NotEqual::class, $rule);
36
        }
37
38 5
        $result = new Result();
39 5
        $targetValue = $rule->getTargetValue() ?? $context?->getDataSet()?->getAttributeValue($rule->getTargetAttribute());
0 ignored issues
show
Bug introduced by
It seems like $rule->getTargetAttribute() can also be of type null; however, parameter $attribute of Yiisoft\Validator\DataSe...ce::getAttributeValue() does only seem to accept string, 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

39
        $targetValue = $rule->getTargetValue() ?? $context?->getDataSet()?->getAttributeValue(/** @scrutinizer ignore-type */ $rule->getTargetAttribute());
Loading history...
40
41 5
        if (!$this->isNotEqual($value, $targetValue, $rule->shouldCheckStrictly(), $rule->getType())) {
42 3
            $formattedMessage = $this->formatter->format(
43 3
                $rule->getMessage(),
44
                [
45 3
                    'attribute' => $context?->getAttribute(),
46 3
                    'targetAttribute' => $rule->getTargetAttribute(),
47 3
                    'targetValue' => $rule->getTargetValue(),
48 3
                    'targetValueOrAttribute' => $rule->getTargetValue() ?? $rule->getTargetAttribute(),
49
                    'value' => $value,
50
                ]
51
            );
52 3
            $result->addError($formattedMessage);
53
        }
54
55 5
        return $result;
56
    }
57
58 5
    private function isNotEqual(mixed $value, mixed $targetValue, bool $strict, string $type): bool
59
    {
60 5
        if ($type === Equal::TYPE_NUMBER) {
61
            $value = (float)$value;
62
            $targetValue = (float)$targetValue;
63
        } else {
64 5
            $value = (string)$value;
65 5
            $targetValue = (string)$targetValue;
66
        }
67
68 5
        if ($strict) {
69 2
            return $value !== $targetValue;
70
        }
71
72 3
        return $value != $targetValue;
73
    }
74
}
75