Passed
Push — master ( 1e50c2...24b8dd )
by noitran
01:21
created

EloquentProcessor::applyAnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Noitran\RQL\Processors;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Noitran\RQL\Contracts\Expression\ExprInterface;
8
use Noitran\RQL\Contracts\Processor\ProcessorInterface;
9
use Noitran\RQL\Expressions\AndExpr;
10
use Noitran\RQL\Expressions\OrExpr;
11
use Noitran\RQL\ExprQueue;
12
13
/**
14
 * Class EloquentProcessor
15
 */
16
class EloquentProcessor implements ProcessorInterface
17
{
18
    /**
19
     * @var Builder
20
     */
21
    protected $builder;
22
23
    /**
24
     * @var array
25
     */
26
    protected static $methodMap = [
27
        '$eq' => 'where',
28
        '$notEq' => 'where',
29
        '$lt' => 'where',
30
        '$lte' => 'where',
31
        '$gt' => 'where',
32
        '$gte' => 'where',
33
        '$like' => 'where',
34
35
        '$in' => 'whereIn',
36
        '$notIn' => 'whereNotIn',
37
        '$between' => 'whereBetween',
38
39
        '$or' => null,
40
        '$and' => null,
41
    ];
42
43
    /**
44
     * @var array
45
     */
46
    protected static $comparisonMethods = [
47
        '$eq',
48
        '$notEq',
49
        '$lt',
50
        '$lte',
51
        '$gt',
52
        '$gte',
53
        '$like',
54
    ];
55
56
    /**
57
     * EloquentProcessor constructor.
58
     *
59
     * @param Builder $builder
60
     */
61
    public function __construct($builder)
62
    {
63
        $this->builder = $builder;
64
    }
65
66
    /**
67
     * @return Builder|Model
68
     */
69
    public function getBuilder()
70
    {
71
        return $this->builder;
72
    }
73
74
    /**
75
     * @param ExprQueue $exprClasses
76
     *
77
     * @return Builder
78
     */
79
    public function process(ExprQueue $exprClasses): Builder
80
    {
81
        foreach ($exprClasses as $exprClass) {
82
            /** @var ExprInterface $exprClass */
83
            if (in_array($exprClass->getExpression(), self::$comparisonMethods, true)) {
84
                return $this->applyComparison($exprClass);
85
            }
86
87
            if (in_array($exprClass->getExpression(), ['$in', '$notIn', '$between'], true)) {
88
                return $this->applyIfValueIsArray($exprClass);
89
            }
90
91
//            if ($exprClass instanceof LikeExpr) {
92
//                return $this->applyLike($exprClass);
93
//            }
94
95
            if ($exprClass instanceof OrExpr) {
96
                return $this->applyOr($exprClass);
97
            }
98
99
            if ($exprClass instanceof AndExpr) {
100
                return $this->applyAnd($exprClass);
101
            }
102
        }
103
104
        return $this->getBuilder();
105
    }
106
107
    /**
108
     * @param ExprInterface $exprClass
109
     *
110
     * @return Builder
111
     */
112
    protected function applyComparison(ExprInterface $exprClass): Builder
113
    {
114
        $method = self::$methodMap[$exprClass->getExpression()];
115
116
        return $this->getBuilder()->{$method}(
117
            $exprClass->getColumn(),
118
            $exprClass->getOperator(),
119
            $exprClass->getValue()
120
        );
121
    }
122
123
    /**
124
     * @param ExprInterface $exprClass
125
     *
126
     * @return Builder
127
     */
128
    protected function applyIfValueIsArray(ExprInterface $exprClass): Builder
129
    {
130
        $method = self::$methodMap[$exprClass->getExpression()];
131
132
        return $this->getBuilder()->{$method}(
133
            $exprClass->getColumn(),
134
            $exprClass->getValue()
135
        );
136
    }
137
138
    /**
139
     * @param ExprInterface $exprClass
140
     *
141
     * @return Builder
142
     */
143
    protected function applyOr(ExprInterface $exprClass): Builder
144
    {
145
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
146
147
    /**
148
     * @param ExprInterface $exprClass
149
     *
150
     * @return Builder
151
     */
152
    protected function applyAnd(ExprInterface $exprClass): Builder
153
    {
154
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
155
}
156