Passed
Pull Request — master (#364)
by
unknown
03:13
created

SubsetHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 72.21%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 34 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 9
        if (!is_iterable($value)) {
24
            $result->addError($rule->getIterableMessage(), [
25
                'attribute' => $context->getAttribute(),
26
                'valueType' => get_debug_type($value),
27
            ]);
28
29
            return $result;
30
        }
31
32 9
        if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) {
33 3
            $values = $rule->getValues();
34 3
            if ($values instanceof Traversable) {
35
                $values = iterator_to_array($values);
36
            }
37
38 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

38
            $valuesString = '"' . implode('", "', /** @scrutinizer ignore-type */ $values) . '"';
Loading history...
39
40 3
            $result->addError(
41 3
                $rule->getSubsetMessage(),
42
                [
43 3
                    'attribute' => $context->getAttribute(),
44
                    'values' => $valuesString,
45
                ],
46
            );
47
        }
48
49 9
        return $result;
50
    }
51
}
52