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

CountHandler::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 0
Metric Value
eloc 15
dl 0
loc 31
ccs 14
cts 16
cp 0.875
rs 8.0555
c 0
b 0
f 0
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\RuleHandlerInterface;
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
use Yiisoft\Validator\Exception\UnexpectedRuleException;
14
15
/**
16
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
17
 * {@see Countable} interface.
18
 */
19
final class CountHandler implements RuleHandlerInterface
20
{
21 20
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
22
    {
23 20
        if (!$rule instanceof Count) {
24 1
            throw new UnexpectedRuleException(Count::class, $rule);
25
        }
26
27 19
        $result = new Result();
28
29 19
        if (!is_countable($value)) {
30
            $result->addError($rule->message);
31
32
            return $result;
33
        }
34
35 19
        $count = count($value);
36
37 19
        if ($rule->exactly !== null && $count !== $rule->exactly) {
38 1
            $result->addError($rule->notExactlyMessage, ['exactly' => $rule->exactly]);
39
40 1
            return $result;
41
        }
42
43 18
        if ($rule->min !== null && $count < $rule->min) {
44 9
            $result->addError($rule->tooFewItemsMessage, ['min' => $rule->min]);
45
        }
46
47 18
        if ($rule->max !== null && $count > $rule->max) {
48 2
            $result->addError($rule->tooManyItemsMessage, ['max' => $rule->max]);
49
        }
50
51 18
        return $result;
52
    }
53
}
54