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

SubsetHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 2
b 0
f 0
dl 0
loc 26
ccs 11
cts 14
cp 0.7856
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 24 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($rule->getIterableMessage());
26
27
            return $result;
28
        }
29
30 9
        if (!ArrayHelper::isSubset($value, $rule->getValues(), $rule->isStrict())) {
31 3
            $values = $rule->getValues() instanceof Traversable
32
                ? iterator_to_array($rule->getValues())
33 3
                : $rule->getValues();
34 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

34
            $valuesString = '"' . implode('", "', /** @scrutinizer ignore-type */ $values) . '"';
Loading history...
35
36 3
            $result->addError($rule->getSubsetMessage(), ['values' => $valuesString]);
37
        }
38
39 9
        return $result;
40
    }
41
}
42