Passed
Pull Request — master (#320)
by Dmitriy
02:52
created

SubsetHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 72.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 31
ccs 13
cts 18
cp 0.7221
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 29 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Traversable;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Validator\Exception\UnexpectedRuleException;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\RuleHandlerInterface;
12
use Yiisoft\Validator\ValidationContext;
13
14
final class SubsetHandler implements RuleHandlerInterface
15
{
16 10
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
17
    {
18 10
        if (!$rule instanceof Subset) {
19 1
            throw new UnexpectedRuleException(Subset::class, $rule);
20
        }
21
22 9
        $result = new Result();
23
24 9
        if (!is_iterable($value)) {
25
            $result->addError(
26
                message: $rule->getIterableMessage(),
27
                parameters: ['value' => $value]
28
            );
29
            return $result;
30
        }
31
32 9
        if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) {
33 3
            $values = $rule->getValues() instanceof Traversable
34
                ? iterator_to_array($rule->getValues())
35 3
                : $rule->getValues();
36 3
            $valuesString = '"' . implode('", "', $values) . '"';
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type iterable; however, parameter $pieces of implode() does only seem to accept array, 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

36
            $valuesString = '"' . implode('", "', /** @scrutinizer ignore-type */ $values) . '"';
Loading history...
37
38 3
            $result->addError(
39 3
                message: $rule->getSubsetMessage(),
40 3
                parameters: ['value' => $value, 'values' => $valuesString]
41
            );
42
        }
43
44 9
        return $result;
45
    }
46
}
47