Completed
Push — master ( 086a41...409693 )
by Thomas
25s queued 11s
created

Result::compare()   C

Complexity

Conditions 12
Paths 49

Size

Total Lines 45
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 45
rs 6.9666
cc 12
nc 49
nop 1

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
3
namespace ORM\Testing\EntityFetcherMock;
4
5
use ORM\Entity;
6
use ORM\EntityFetcher;
7
8
class Result extends EntityFetcher
9
{
10
    /** @var Entity[] */
11
    protected $entities = [];
12
13
    /** @var string[] */
14
    protected $regularExpressions = [];
15
16
    /**
17
     * Check if $fetcher matches the current query
18
     *
19
     * Returns the score for the given EntityFetcher. The more conditions match the higher the score:
20
     * - 0 = the query does not match one of the conditions
21
     * - 1 = no conditions required to match the query
22
     * - n = n-1 conditions matched the query
23
     *
24
     * @param EntityFetcher $fetcher
25
     * @return int
26
     */
27
    public function compare(EntityFetcher $fetcher)
28
    {
29
        $result = 1;
30
31
        // joins, grouping and ordering are just lists so they have to exist
32
        foreach (['joins', 'groupBy', 'orderBy'] as $attribute) {
33
            foreach ($this->$attribute as $condition) {
34
                if (!in_array($condition, $fetcher->$attribute)) {
35
                    return 0;
36
                }
37
                $result++;
38
            }
39
        }
40
41
        // where conditions can have 'AND ' or 'OR ' in front
42
        // there is a lot of logic behind these keywords that we ignore here
43
        foreach ($this->where as $condition) {
44
            $condition = preg_replace('/^(AND |OR )/', '', $condition);
45
            foreach ($fetcher->where as $fetcherCondition) {
46
                $fetcherCondition = preg_replace('/^(AND |OR )/', '', $fetcherCondition);
47
                if ($condition === $fetcherCondition) {
48
                    $result++;
49
                    continue 2; // continue the outer foreach to not execute return 0
50
                }
51
            }
52
            return 0; // this is only reached when no condition matched
53
        }
54
55
        // check if limit and offset matches
56
        if ($this->limit) {
57
            if ($this->limit !== $fetcher->limit || $this->offset !== $fetcher->offset) {
58
                return 0;
59
            }
60
            $result++;
61
        }
62
63
        // check if regular expressions match
64
        foreach ($this->regularExpressions as $expression) {
65
            if (!preg_match($expression, $fetcher->getQuery())) {
66
                return 0;
67
            }
68
            $result++;
69
        }
70
71
        return $result;
72
    }
73
74
    /**
75
     * Add a regular expression that has to match
76
     *
77
     * @param string $expression
78
     * @return $this
79
     * @codeCoverageIgnore trivial code
80
     */
81
    public function matches($expression)
82
    {
83
        $this->regularExpressions[] = $expression;
84
        return $this;
85
    }
86
87
    /**
88
     * Add entities to the result
89
     *
90
     * @param Entity[] $entities
91
     * @return $this
92
     * @codeCoverageIgnore trivial code
93
     */
94
    public function addEntities(Entity ...$entities)
95
    {
96
        if (!empty($entities)) {
97
            array_push($this->entities, ...$entities);
98
        }
99
        return $this;
100
    }
101
102
    /**
103
     * Get the entities for this result
104
     *
105
     * @return Entity[]
106
     * @codeCoverageIgnore trivial code
107
     */
108
    public function getEntities()
109
    {
110
        return $this->entities;
111
    }
112
}
113