Passed
Push — master ( 4f0280...b89b05 )
by Alexander
01:15
created

GroupProcessor::match()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.0578

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 10
nop 3
dl 0
loc 34
ccs 22
cts 24
cp 0.9167
crap 10.0578
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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
declare(strict_types=1);
3
4
namespace Yiisoft\Data\Reader\Iterable\Processor;
5
6
7
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;
8
9
abstract class GroupProcessor implements IterableProcessorInterface, FilterProcessorInterface
10
{
11
    abstract protected function checkResults(array $result): bool;
12
13
    abstract protected function checkResult($result): ?bool;
14
15
    /**
16
     * PHP variable specific execute
17
     */
18 7
    public function match(array $item, array $arguments, array $filterProcessors): bool
19
    {
20 7
        if (count($arguments) < 1) {
21 1
            throw new \RuntimeException('At least one argument should be provided!');
22 6
        } elseif (!is_array($arguments[0])) {
23 1
            throw new \RuntimeException('Sub filters is not an array!');
24
        }
25 5
        $results = [];
26 5
        foreach ($arguments[0] as $subFilter) {
27 5
            if (!is_array($subFilter)) {
28 1
                throw new \RuntimeException('Sub filter is not an array!');
29 4
            } elseif (count($subFilter) < 1) {
30 1
                throw new \RuntimeException('At least operator should be provided!');
31
            }
32 3
            $operator = array_shift($subFilter);
33 3
            if (!is_string($operator)) {
34
                throw new \RuntimeException('Operator is not a string!');
35 3
            } elseif (strlen($operator) === 0) {
36 1
                throw new \RuntimeException('The operator string cannot be empty!');
37
            }
38
39 2
            $processor = $filterProcessors[$operator] ?? null;
40 2
            if ($processor === null) {
41
                throw new \RuntimeException(sprintf('"%s" operator is not supported!', $operator));
42
            }
43
            /* @var $processor IterableProcessorInterface */
44 2
            $result = $processor->match($item, $subFilter, $filterProcessors);
45 2
            if (is_bool($this->checkResult($result))) {
46 2
                return $result;
47
            }
48 2
            $results[] = $result;
49
        }
50
51 2
        return $this->checkResults($results);
52
    }
53
54
55
}
56