Passed
Pull Request — master (#13)
by Bas
05:52
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
        if ($predicates[0] instanceof PredicateExpression) {
51
            $logicalOperator = $predicates[0]->logicalOperator;
52
        }
53
        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...
54
            $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
55
        }
56
57
        $groupCompilation = '';
58
        if ($position > 0) {
59
            $groupCompilation = $logicalOperator.' ';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $logicalOperator does not seem to be defined for all execution paths leading up to this point.
Loading history...
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
60
        }
61
        return $groupCompilation . '(' . implode(' ', $compiledPredicates) . ')';
62
    }
63
}