Passed
Pull Request — master (#222)
by Dmitriy
02:36
created

CountValidator::validate()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.1582

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 31
ccs 14
cts 16
cp 0.875
rs 8.0555
cc 9
nc 7
nop 4
crap 9.1582
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Count;
6
7
use Countable;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule\AtLeast\AtLeast;
10
use Yiisoft\Validator\Rule\RuleValidatorInterface;
11
use Yiisoft\Validator\ValidationContext;
12
use Yiisoft\Validator\ValidatorInterface;
13
use function count;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Validator\Rule\Count\count. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Yiisoft\Validator\Exception\UnexpectedRuleException;
15
16
/**
17
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
18
 * {@see Countable} interface.
19
 */
20
final class CountValidator implements RuleValidatorInterface
21
{
22 20
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
23
    {
24 20
        if (!$rule instanceof Count) {
25 1
            throw new UnexpectedRuleException(Count::class, $rule);
26
        }
27
28 19
        $result = new Result();
29
30 19
        if (!is_countable($value)) {
31
            $result->addError($rule->message);
32
33
            return $result;
34
        }
35
36 19
        $count = count($value);
37
38 19
        if ($rule->exactly !== null && $count !== $rule->exactly) {
39 1
            $result->addError($rule->notExactlyMessage, ['exactly' => $rule->exactly]);
40
41 1
            return $result;
42
        }
43
44 18
        if ($rule->min !== null && $count < $rule->min) {
45 9
            $result->addError($rule->tooFewItemsMessage, ['min' => $rule->min]);
46
        }
47
48 18
        if ($rule->max !== null && $count > $rule->max) {
49 2
            $result->addError($rule->tooManyItemsMessage, ['max' => $rule->max]);
50
        }
51
52 18
        return $result;
53
    }
54
}
55