Passed
Push — master ( 5c2907...37dc07 )
by Sergei
24:35 queued 21:56
created

SubsetHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
                $rule->getIterableMessage(),
27
                [
28
                    'attribute' => $context->getAttribute(),
29
                    'value' => (string) $value,
30
                ],
31
            );
32
            return $result;
33
        }
34
35 9
        if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) {
36 3
            $values = $rule->getValues() instanceof Traversable
37
                ? iterator_to_array($rule->getValues())
38 3
                : $rule->getValues();
39 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

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