Failed Conditions
Pull Request — experimental/3.1 (#2285)
by Kiyotaka
41:31
created

WhereClause   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 271
rs 10
c 0
b 0
f 0
wmc 25
lcom 2
cbo 2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A newWhereClause() 0 11 3
A eq() 0 4 1
A neq() 0 4 1
A isNull() 0 4 1
A isNotNull() 0 4 1
A like() 0 4 1
A notLike() 0 4 1
A in() 0 4 2
A isMap() 0 4 1
A notIn() 0 4 2
A between() 0 4 2
A gt() 0 4 1
A gte() 0 4 1
A lt() 0 4 1
A lte() 0 4 1
A expr() 0 4 1
A build() 0 8 3
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Doctrine\Query;
25
26
27
use Doctrine\ORM\Query\Expr;
28
use Doctrine\ORM\QueryBuilder;
29
30
/**
31
 * WHERE句を組み立てるクラス。
32
 *
33
 * @package Eccube\Doctrine\Query
34
 */
35
class WhereClause
36
{
37
38
    private $expr;
39
40
    /**
41
     * @var array
42
     */
43
    private $params;
44
45
    /**
46
     * WhereClause constructor.
47
     * @param $expr
48
     * @param array $params
49
     */
50
    private function __construct($expr, $params = null)
51
    {
52
        $this->expr = $expr;
53
        $this->params = $params;
0 ignored issues
show
Documentation Bug introduced by
It seems like $params can be null. However, the property $params is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
54
    }
55
56
    private static function newWhereClause($expr, $x, $y)
57
    {
58
        if ($y) {
59
            if (is_array($y)) {
60
                return new WhereClause($expr, $y);
61
            } else {
62
                return new WhereClause($expr, [$x => $y]);
63
            }
64
        }
65
        return new WhereClause($expr);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
66
    }
67
68
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
69
     * =条件式のファクトリメソッド。
70
     *
71
     * Example:
72
     *      WhereClause::eq('name', ':Name', 'hoge')
73
     *      WhereClause::eq('name', ':Name', ['Name' => 'hoge'])
74
     *
75
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
76
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
77
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
78
     * @return WhereClause
79
     */
80
    public static function eq($x, $y, $param)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
81
    {
82
        return self::newWhereClause(self::expr()->eq($x, $y), $y, $param);
83
    }
84
85
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
86
     * <>条件式のファクトリメソッド。
87
     *
88
     * Example:
89
     *      WhereClause::neq('name', ':Name', 'hoge')
90
     *      WhereClause::neq('name', ':Name', ['Name' => 'hoge'])
91
     *
92
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
93
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
94
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
95
     * @return WhereClause
96
     */
97
    public static function neq($x, $y, $param)
98
    {
99
        return self::newWhereClause(self::expr()->neq($x, $y), $y, $param);
100
    }
101
102
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
103
     * IS NULL条件式のファクトリメソッド。
104
     *
105
     * Example:
106
     *      WhereClause::isNull('name')
107
     *
108
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
109
     * @return WhereClause
110
     */
111
    public static function isNull($x)
112
    {
113
        return new WhereClause(self::expr()->isNull($x));
114
    }
115
116
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
117
     * IS NOT NULL条件式のファクトリメソッド。
118
     *
119
     * Example:
120
     *      WhereClause::isNotNull('name')
121
     *
122
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
123
     * @return WhereClause
124
     */
125
    public static function isNotNull($x)
126
    {
127
        return new WhereClause(self::expr()->isNotNull($x));
128
    }
129
130
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
131
     * LIKE演算子のファクトリメソッド。
132
     *
133
     * Example:
134
     *      WhereClause::like('name', ':Name', '%hoge')
135
     *      WhereClause::like('name', ':Name', ['Name' => '%hoge'])
136
     *
137
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
138
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
139
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
140
     * @return WhereClause
141
     */
142
    public static function like($x, $y, $param)
143
    {
144
        return self::newWhereClause(self::expr()->like($x, $y), $y, $param);
145
    }
146
147
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
148
     * NOT LIKE演算子のファクトリメソッド。
149
     *
150
     * Example:
151
     *      WhereClause::notLike('name', ':Name', '%hoge')
152
     *      WhereClause::notLike('name', ':Name', ['Name' => '%hoge'])
153
     *
154
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
155
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
156
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
157
     * @return WhereClause
158
     */
159
    public static function notLike($x, $y, $param)
160
    {
161
        return self::newWhereClause(self::expr()->notLike($x, $y), $y, $param);
162
    }
163
164
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
165
     * IN演算子のファクトリメソッド。
166
     *
167
     * Example:
168
     *      WhereClause::in('name', ':Names', ['foo', 'bar'])
169
     *      WhereClause::in('name', ':Names', ['Names' => ['foo', 'bar']])
170
     *
171
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
172
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
173
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
174
     * @return WhereClause
175
     */
176
    public static function in($x, $y, $param)
177
    {
178
        return new WhereClause(self::expr()->in($x, $y), self::isMap($param) ? $param : [$y => $param]);
179
    }
180
181
    private static function isMap($arrayOrMap)
182
    {
183
        return array_values($arrayOrMap) !== $arrayOrMap;
184
    }
185
186
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
187
     * NOT IN演算子のファクトリメソッド。
188
     *
189
     * Example:
190
     *      WhereClause::notIn('name', ':Names', ['foo', 'bar'])
191
     *      WhereClause::notIn('name', ':Names', ['Names' => ['foo', 'bar']])
192
     *
193
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
194
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
195
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
196
     * @return WhereClause
197
     */
198
    public static function notIn($x, $y, $param)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
199
    {
200
        return new WhereClause(self::expr()->notIn($x, $y), self::isMap($param) ? $param : [$y => $param]);
201
    }
202
203
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$var" missing
Loading history...
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$params" missing
Loading history...
204
     * BETWEEN演算子のファクトリメソッド。
205
     *
206
     * Example:
207
     *      WhereClause::between('price', ':PriceMin', ':PriceMax', [1000, 2000])
208
     *      WhereClause::between('price', ':PriceMin', ':PriceMax', ['PriceMin' => 1000, 'PriceMax' => 2000])
209
     *
210
     * @param $var
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
211
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
212
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
213
     * @param $params
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
214
     * @return WhereClause
215
     */
216
    public static function between($var, $x, $y, $params)
217
    {
218
        return new WhereClause(self::expr()->between($var, $x, $y), self::isMap($params) ? $params : [$x => $params[0], $y => $params[1]]);
219
    }
220
221
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
222
     * >演算子のファクトリメソッド。
223
     *
224
     * Example:
225
     *      WhereClause::gt('price', ':Price', 1000)
226
     *      WhereClause::gt('price', ':Price', ['Price' => 1000])
227
     *
228
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
229
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
230
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
231
     * @return WhereClause
232
     */
233
    public static function gt($x, $y, $param)
234
    {
235
        return self::newWhereClause(self::expr()->gt($x, $y), $y, $param);
236
    }
237
238
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
239
     * >=演算子のファクトリメソッド。
240
     *
241
     * Example:
242
     *      WhereClause::gte('price', ':Price', 1000)
243
     *      WhereClause::gte('price', ':Price', ['Price' => 1000])
244
     *
245
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
246
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
247
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
248
     * @return WhereClause
249
     */
250
    public static function gte($x, $y, $param)
251
    {
252
        return self::newWhereClause(self::expr()->gte($x, $y), $y, $param);
253
    }
254
255
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
256
     * <演算子のファクトリメソッド。
257
     *
258
     * Example:
259
     *      WhereClause::lt('price', ':Price', 1000)
260
     *      WhereClause::lt('price', ':Price', ['Price' => 1000])
261
     *
262
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
263
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
264
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
265
     * @return WhereClause
266
     */
267
    public static function lt($x, $y, $param)
268
    {
269
        return self::newWhereClause(self::expr()->lt($x, $y), $y, $param);
270
    }
271
272
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$x" missing
Loading history...
introduced by
Doc comment for parameter "$y" missing
Loading history...
introduced by
Doc comment for parameter "$param" missing
Loading history...
273
     * <=演算子のファクトリメソッド。
274
     *
275
     * Example:
276
     *      WhereClause::lte('price', ':Price', 1000)
277
     *      WhereClause::lte('price', ':Price', ['Price' => 1000])
278
     *
279
     * @param $x
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
280
     * @param $y
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
281
     * @param $param
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
282
     * @return WhereClause
283
     */
284
    public static function lte($x, $y, $param)
285
    {
286
        return self::newWhereClause(self::expr()->lte($x, $y), $y, $param);
287
    }
288
289
    /**
290
     * @return Expr
291
     */
292
    private static function expr()
293
    {
294
        return new Expr();
295
    }
296
297
    public function build(QueryBuilder $builder) {
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
introduced by
Missing function doc comment
Loading history...
298
        $builder->andWhere($this->expr);
299
        if ($this->params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
300
            foreach ($this->params as $key=>$param) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
301
                $builder->setParameter($key, $param);
302
            }
303
        }
304
    }
305
}