Passed
Pull Request — master (#222)
by Alexander
05:06 queued 02:29
created

CountValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 29
ccs 12
cts 14
cp 0.8571
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 27 8
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\RuleValidatorInterface;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\ValidatorInterface;
12
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...
13
14
/**
15
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
16
 * {@see Countable} interface.
17
 */
18
final class CountValidator implements RuleValidatorInterface
19
{
20 19
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
21
    {
22 19
        $result = new Result();
23
24 19
        if (!is_countable($value)) {
25
            $result->addError($rule->message);
26
27
            return $result;
28
        }
29
30 19
        $count = count($value);
31
32 19
        if ($rule->exactly !== null && $count !== $rule->exactly) {
33 1
            $result->addError($rule->notExactlyMessage, ['exactly' => $rule->exactly]);
34
35 1
            return $result;
36
        }
37
38 18
        if ($rule->min !== null && $count < $rule->min) {
39 9
            $result->addError($rule->tooFewItemsMessage, ['min' => $rule->min]);
40
        }
41
42 18
        if ($rule->max !== null && $count > $rule->max) {
43 2
            $result->addError($rule->tooManyItemsMessage, ['max' => $rule->max]);
44
        }
45
46 18
        return $result;
47
    }
48
}
49