CompilesPredicates::compilePredicates()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

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 19
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Traits;
6
7
use LaravelFreelancerNL\FluentAQL\Expressions\PredicateExpression;
8
9
trait CompilesPredicates
10
{
11
    /**
12
     * @param array<mixed>|PredicateExpression $predicates
13
     */
14 16
    public function compilePredicates(
15
        array|PredicateExpression $predicates
16
    ): string {
17 16
        if (! is_array($predicates)) {
0 ignored issues
show
introduced by
The condition is_array($predicates) is always true.
Loading history...
18 1
            $predicates = [$predicates];
19
        }
20
21 16
        $compiledPredicates = [];
22 16
        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...
23 16
            if ($predicates[$i] instanceof PredicateExpression) {
24 16
                $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
25
            }
26
27 16
            if (is_array($predicates[$i])) {
28 2
                $compiledPredicates[] = $this->compilePredicateGroup($predicates[$i], $i);
29
            }
30
        }
31
32 16
        return implode(' ', $compiledPredicates);
33
    }
34
35 16
    protected function compilePredicate(PredicateExpression $predicate, int $position = 0): string
36
    {
37 16
        $compiledPredicate = '';
38 16
        if ($position > 0) {
39 7
            $compiledPredicate = $predicate->logicalOperator . ' ' ;
40
        }
41 16
        return $compiledPredicate . $predicate->compile($this);
42
    }
43
44
    /**
45
     * @param array<mixed> $predicates
46
     * @param int $position
47
     * @return string
48
     */
49 2
    protected function compilePredicateGroup(
50
        array $predicates,
51
        int $position = 0
52
    ): string {
53 2
        $compiledPredicates = [];
54 2
        $logicalOperator = '';
55 2
        if ($predicates[0] instanceof PredicateExpression) {
56 2
            $logicalOperator = $predicates[0]->logicalOperator;
57
        }
58 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...
59 2
            if ($predicates[$i] instanceof PredicateExpression) {
60 2
                $compiledPredicates[] = $this->compilePredicate($predicates[$i], $i);
61
            }
62
        }
63
64 2
        $groupCompilation = '';
65 2
        if ($position > 0) {
66 1
            $groupCompilation = $logicalOperator . ' ';
67
        }
68 2
        return $groupCompilation . '(' . implode(' ', $compiledPredicates) . ')';
69
    }
70
}
71