Passed
Pull Request — master (#369)
by
unknown
02:46
created

SubsetHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 32 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 12
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
17
    {
18 12
        if (!$rule instanceof Subset) {
19 1
            throw new UnexpectedRuleException(Subset::class, $rule);
20
        }
21
22 11
        $result = new Result();
23 11
        if (!is_iterable($value)) {
24 1
            return $result->addError($rule->getIterableMessage(), [
25 1
                'attribute' => $context->getAttribute(),
26 1
                'type' => get_debug_type($value),
27
            ]);
28
        }
29
30 10
        if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) {
31 4
            $values = $rule->getValues();
32 4
            if ($values instanceof Traversable) {
33 1
                $values = iterator_to_array($values);
34
            }
35
36 4
            $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 4
            $result->addError(
39 4
                $rule->getSubsetMessage(),
40
                [
41 4
                    'attribute' => $context->getAttribute(),
42
                    'values' => $valuesString,
43
                ],
44
            );
45
        }
46
47 10
        return $result;
48
    }
49
}
50