Passed
Push — master ( f8f76e...e5015c )
by Bas
12:29
created

CompilesPredicates   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 47
ccs 26
cts 26
cp 1
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compilePredicates() 0 18 5
A compilePredicateGroup() 0 16 4
A compilePredicate() 0 7 2
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 13
    public function compilePredicates(PredicateExpression|array $predicates): string
15
    {
16 13
        if (! is_array($predicates)) {
0 ignored issues
show
introduced by
The condition is_array($predicates) is always true.
Loading history...
17 5
            $predicates = [$predicates];
18
        }
19
20 13
        $compiledPredicates = [];
21 13
        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...
22 13
            if ($predicates[$i] instanceof PredicateExpression) {
23 13
                $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
24
            }
25
26 13
            if (is_array($predicates[$i])) {
27 2
                $compiledPredicates[] = $this->compilePredicateGroup($predicates[$i], $i);
28
            }
29
        }
30
31 13
        return implode(' ', $compiledPredicates);
32
    }
33
34 13
    protected function compilePredicate(PredicateExpression $predicate, int $position = 0): string
35
    {
36 13
        $compiledPredicate = '';
37 13
        if ($position > 0) {
38 7
            $compiledPredicate = $predicate->logicalOperator . ' ' ;
39
        }
40 13
        return $compiledPredicate . $predicate->compile($this);
41
    }
42
43 2
    protected function compilePredicateGroup(array $predicates, $position = 0): string
44
    {
45 2
        $compiledPredicates = [];
46 2
        $logicalOperator = '';
47 2
        if ($predicates[0] instanceof PredicateExpression) {
48 2
            $logicalOperator = $predicates[0]->logicalOperator;
49
        }
50 2
        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...
51 2
            $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
52
        }
53
54 2
        $groupCompilation = '';
55 2
        if ($position > 0) {
56 1
            $groupCompilation = $logicalOperator . ' ';
57
        }
58 2
        return $groupCompilation . '(' . implode(' ', $compiledPredicates) . ')';
59
    }
60
}
61