Passed
Pull Request — master (#13)
by Bas
03:44 queued 01:39
created

CompilesPredicates::compilePredicates()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 18
ccs 8
cts 10
cp 0.8
crap 5.2
rs 9.6111
1
<?php
2
3
namespace LaravelFreelancerNL\FluentAQL\Traits;
4
5
use LaravelFreelancerNL\FluentAQL\Expressions\PredicateExpression;
6
7
/**
8
 * Trait hasFunctions.
9
 *
10
 * AQL Function API calls.
11
 */
12
trait CompilesPredicates
13
{
14
    /**
15
     * @param PredicateExpression|array $predicates
16
     * @return string
17
     */
18 4
    public function compilePredicates($predicates)
19
    {
20 4
        if (! is_array($predicates)) {
21
            $predicates = [$predicates];
22
        }
23
24 4
        $compiledPredicates = [];
25 4
        for ($i = 0; $i < count($predicates); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
26 4
            if ($predicates[$i] instanceof PredicateExpression) {
27 4
                $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
28
            }
29
30 4
            if (is_array($predicates[$i])) {
31
                $compiledPredicates[] = $this->compilePredicateGroup($predicates[$i], $i);
32
            }
33
        }
34
35 4
        return implode(' ', $compiledPredicates);
36
    }
37
38 4
    protected function compilePredicate(PredicateExpression $predicate, $position = 0)
39
    {
40 4
        $compiledPredicate = '';
41 4
        if ($position > 0) {
42
            $compiledPredicate = $predicate->logicalOperator . ' ' ;
43
        }
44 4
        return $compiledPredicate . $predicate->compile($this);
45
    }
46
47
    protected function compilePredicateGroup(array $predicates, $position = 0)
48
    {
49
        $compiledPredicates = [];
50
        $logicalOperator = '';
51
        if ($predicates[0] instanceof PredicateExpression) {
52
            $logicalOperator = $predicates[0]->logicalOperator;
53
        }
54
        for ($i = 0; $i < count($predicates); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
55
            $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
56
        }
57
58
        $groupCompilation = '';
59
        if ($position > 0) {
60
            $groupCompilation = $logicalOperator . ' ';
61
        }
62
        return $groupCompilation . '(' . implode(' ', $compiledPredicates) . ')';
63
    }
64
}
65