Passed
Push — master ( 951d77...1f27ac )
by Alexander
02:35
created

Group::match()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 9
nop 3
dl 0
loc 53
ccs 32
cts 32
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\Handler;
6
7
use InvalidArgumentException;
8
use Yiisoft\Data\Reader\FilterDataValidationHelper;
9
10
use function array_shift;
11
use function count;
12
use function is_array;
13
use function is_string;
14
use function sprintf;
15
16
abstract class Group implements IterableHandlerInterface
17
{
18
    abstract protected function checkResults(array $results): bool;
19
20 73
    public function match(array|object $item, array $arguments, array $filterHandlers): bool
21
    {
22 73
        if (count($arguments) !== 1) {
23 8
            throw new InvalidArgumentException('$arguments should contain exactly one element.');
24
        }
25
26 65
        [$subFilters] = $arguments;
27
28 65
        if (!is_array($subFilters)) {
29 16
            throw new InvalidArgumentException(sprintf(
30 16
                'The sub filters should be array. The %s is received.',
31 16
                FilterDataValidationHelper::getValueType($subFilters),
32 16
            ));
33
        }
34
35 49
        $results = [];
36
37 49
        foreach ($subFilters as $subFilter) {
38 49
            if (!is_array($subFilter)) {
39 16
                throw new InvalidArgumentException(sprintf(
40 16
                    'The sub filter should be array. The %s is received.',
41 16
                    FilterDataValidationHelper::getValueType($subFilter),
42 16
                ));
43
            }
44
45 33
            if (empty($subFilter)) {
46 2
                throw new InvalidArgumentException('At least operator should be provided.');
47
            }
48
49 31
            $operator = array_shift($subFilter);
50
51 31
            if (!is_string($operator)) {
52 10
                throw new InvalidArgumentException(sprintf(
53 10
                    'The operator should be string. The %s is received.',
54 10
                    FilterDataValidationHelper::getValueType($operator),
55 10
                ));
56
            }
57
58 21
            if ($operator === '') {
59 2
                throw new InvalidArgumentException('The operator string cannot be empty.');
60
            }
61
62 19
            $filterHandler = $filterHandlers[$operator] ?? null;
63
64 19
            if ($filterHandler === null) {
65 2
                throw new InvalidArgumentException(sprintf('"%s" operator is not supported.', $operator));
66
            }
67
68 17
            FilterDataValidationHelper::assertFilterHandlerIsIterable($filterHandler);
69 15
            $results[] = $filterHandler->match($item, $subFilter, $filterHandlers);
70
        }
71
72 15
        return $this->checkResults($results);
73
    }
74
}
75