| Conditions | 1 |
| Paths | 1 |
| Total Lines | 309 |
| Code Lines | 176 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 113 | public function buildConditions(): array |
||
| 114 | { |
||
| 115 | $conditions = [ |
||
| 116 | /* empty values */ |
||
| 117 | [['like', 'name', []], '0=1', []], |
||
| 118 | [['not like', 'name', []], '', []], |
||
| 119 | [['or like', 'name', []], '0=1', []], |
||
| 120 | [['or not like', 'name', []], '', []], |
||
| 121 | |||
| 122 | /* not */ |
||
| 123 | [['not', ''], '', []], |
||
| 124 | [['not', 'name'], 'NOT (name)', []], |
||
| 125 | [ |
||
| 126 | [ |
||
| 127 | 'not', |
||
| 128 | $this->mock->query()->select('exists')->from('some_table'), |
||
| 129 | ], |
||
| 130 | 'NOT ((SELECT [[exists]] FROM [[some_table]]))', [], |
||
| 131 | ], |
||
| 132 | |||
| 133 | /* and */ |
||
| 134 | [['and', '', ''], '', []], |
||
| 135 | [['and', '', 'id=2'], 'id=2', []], |
||
| 136 | [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []], |
||
| 137 | [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []], |
||
| 138 | [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]], |
||
| 139 | [ |
||
| 140 | [ |
||
| 141 | 'and', |
||
| 142 | ['expired' => false], |
||
| 143 | $this->mock->query()->select('count(*) > 1')->from('queue'), |
||
| 144 | ], |
||
| 145 | '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))', |
||
| 146 | [':qp0' => false], |
||
| 147 | ], |
||
| 148 | |||
| 149 | /* or */ |
||
| 150 | [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []], |
||
| 151 | [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []], |
||
| 152 | [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]], |
||
| 153 | |||
| 154 | /* between */ |
||
| 155 | [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 156 | [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 157 | [ |
||
| 158 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 159 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 160 | [], |
||
| 161 | ], |
||
| 162 | [ |
||
| 163 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 164 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 165 | [':qp0' => 123], |
||
| 166 | ], |
||
| 167 | [ |
||
| 168 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 169 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 170 | [], |
||
| 171 | ], |
||
| 172 | [ |
||
| 173 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 174 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 175 | [':qp0' => 123], |
||
| 176 | ], |
||
| 177 | [ |
||
| 178 | new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'), |
||
| 179 | ':qp0 BETWEEN [[create_time]] AND [[update_time]]', |
||
| 180 | [':qp0' => '2018-02-11'], |
||
| 181 | ], |
||
| 182 | [ |
||
| 183 | new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'), |
||
| 184 | ':qp0 NOT BETWEEN NOW() AND [[update_time]]', |
||
| 185 | [':qp0' => '2018-02-11'], |
||
| 186 | ], |
||
| 187 | [ |
||
| 188 | new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'), |
||
| 189 | 'NOW() BETWEEN [[create_time]] AND [[update_time]]', |
||
| 190 | [], |
||
| 191 | ], |
||
| 192 | [ |
||
| 193 | new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'), |
||
| 194 | 'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]', |
||
| 195 | [], |
||
| 196 | ], |
||
| 197 | [ |
||
| 198 | new BetweenColumnsCondition( |
||
| 199 | new Expression('NOW()'), |
||
| 200 | 'NOT BETWEEN', |
||
| 201 | $this->mock->query()->select('min_date')->from('some_table'), |
||
| 202 | 'max_date' |
||
| 203 | ), |
||
| 204 | 'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]', |
||
| 205 | [], |
||
| 206 | ], |
||
| 207 | [ |
||
| 208 | new BetweenColumnsCondition( |
||
| 209 | new Expression('NOW()'), |
||
| 210 | 'NOT BETWEEN', |
||
| 211 | new Expression('min_date'), |
||
| 212 | $this->mock->query()->select('max_date')->from('some_table'), |
||
| 213 | ), |
||
| 214 | 'NOW() NOT BETWEEN min_date AND (SELECT [[max_date]] FROM [[some_table]])', |
||
| 215 | [], |
||
| 216 | ], |
||
| 217 | |||
| 218 | /* in */ |
||
| 219 | [ |
||
| 220 | ['in', 'id', [1, 2, $this->mock->query()->select('three')->from('digits')]], |
||
| 221 | '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))', |
||
| 222 | [':qp0' => 1, ':qp1' => 2], |
||
| 223 | ], |
||
| 224 | [ |
||
| 225 | ['not in', 'id', [1, 2, 3]], |
||
| 226 | '[[id]] NOT IN (:qp0, :qp1, :qp2)', |
||
| 227 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 228 | ], |
||
| 229 | [ |
||
| 230 | ['in', 'id', $this->mock->query()->select('id')->from('users')->where(['active' => 1])], |
||
| 231 | '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 232 | [':qp0' => 1], |
||
| 233 | ], |
||
| 234 | [ |
||
| 235 | ['not in', 'id', $this->mock->query()->select('id')->from('users')->where(['active' => 1])], |
||
| 236 | '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 237 | [':qp0' => 1], |
||
| 238 | ], |
||
| 239 | [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 240 | [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 241 | [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 242 | 'composite in' => [ |
||
| 243 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]], |
||
| 244 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
| 245 | [':qp0' => 1, ':qp1' => 'oy'], |
||
| 246 | ], |
||
| 247 | 'composite in (just one column)' => [ |
||
| 248 | ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]], |
||
| 249 | '[[id]] IN (:qp0, :qp1)', |
||
| 250 | [':qp0' => 1, ':qp1' => 2], |
||
| 251 | ], |
||
| 252 | 'composite in using array objects (just one column)' => [ |
||
| 253 | [ |
||
| 254 | 'in', |
||
| 255 | new TraversableObject(['id']), |
||
| 256 | new TraversableObject([['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]), |
||
| 257 | ], |
||
| 258 | '[[id]] IN (:qp0, :qp1)', |
||
| 259 | [':qp0' => 1, ':qp1' => 2], |
||
| 260 | ], |
||
| 261 | |||
| 262 | /* in using array objects. */ |
||
| 263 | [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 264 | [ |
||
| 265 | ['in', 'id', new TraversableObject([1, 2, 3])], |
||
| 266 | '[[id]] IN (:qp0, :qp1, :qp2)', |
||
| 267 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 268 | ], |
||
| 269 | |||
| 270 | /* in using array objects containing null value */ |
||
| 271 | [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]], |
||
| 272 | [ |
||
| 273 | ['in', 'id', new TraversableObject([1, 2, null])], |
||
| 274 | '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2], |
||
| 275 | ], |
||
| 276 | |||
| 277 | /* not in using array object containing null value */ |
||
| 278 | [ |
||
| 279 | ['not in', 'id', new TraversableObject([1, null])], |
||
| 280 | '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1], |
||
| 281 | ], |
||
| 282 | [ |
||
| 283 | ['not in', 'id', new TraversableObject([1, 2, null])], |
||
| 284 | '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL', |
||
| 285 | [':qp0' => 1, ':qp1' => 2], |
||
| 286 | ], |
||
| 287 | |||
| 288 | /* in using array object containing only null value */ |
||
| 289 | [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []], |
||
| 290 | [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []], |
||
| 291 | 'composite in using array objects' => [ |
||
| 292 | [ |
||
| 293 | 'in', |
||
| 294 | new TraversableObject(['id', 'name']), |
||
| 295 | new TraversableObject([['id' => 1, 'name' => 'oy'], ['id' => 2, 'name' => 'yo']]), |
||
| 296 | ], |
||
| 297 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 298 | [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'], |
||
| 299 | ], |
||
| 300 | |||
| 301 | /* in object conditions */ |
||
| 302 | [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 303 | [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 304 | [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 305 | [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 306 | [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 307 | [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 308 | [new InCondition([], 'in', 1), '0=1', []], |
||
| 309 | [new InCondition([], 'in', [1]), '0=1', []], |
||
| 310 | [new InCondition(['id', 'name'], 'in', []), '0=1', []], |
||
| 311 | [ |
||
| 312 | new InCondition( |
||
| 313 | ['id'], |
||
| 314 | 'in', |
||
| 315 | $this->mock->query()->select('id')->from('users')->where(['active' => 1]), |
||
| 316 | ), |
||
| 317 | '([[id]]) IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 318 | [':qp0' => 1], |
||
| 319 | ], |
||
| 320 | [ |
||
| 321 | new InCondition(['id', 'name'], 'in', [['id' => 1]]), |
||
| 322 | '([[id]], [[name]]) IN ((:qp0, NULL))', |
||
| 323 | [':qp0' => 1], |
||
| 324 | ], |
||
| 325 | [ |
||
| 326 | new InCondition(['id', 'name'], 'in', [['name' => 'oy']]), |
||
| 327 | '([[id]], [[name]]) IN ((NULL, :qp0))', |
||
| 328 | [':qp0' => 'oy'], |
||
| 329 | ], |
||
| 330 | [ |
||
| 331 | new InCondition(['id', 'name'], 'in', [['id' => 1, 'name' => 'oy']]), |
||
| 332 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
| 333 | [':qp0' => 1, ':qp1' => 'oy'], |
||
| 334 | ], |
||
| 335 | |||
| 336 | /* exists */ |
||
| 337 | [ |
||
| 338 | [ |
||
| 339 | 'exists', |
||
| 340 | $this->mock->query()->select('id')->from('users')->where(['active' => 1]), |
||
| 341 | ], |
||
| 342 | 'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 343 | [':qp0' => 1], |
||
| 344 | ], |
||
| 345 | [ |
||
| 346 | ['not exists', $this->mock->query()->select('id')->from('users')->where(['active' => 1])], |
||
| 347 | 'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1], |
||
| 348 | ], |
||
| 349 | |||
| 350 | /* simple conditions */ |
||
| 351 | [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']], |
||
| 352 | [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]], |
||
| 353 | [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']], |
||
| 354 | [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]], |
||
| 355 | [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']], |
||
| 356 | [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]], |
||
| 357 | [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']], |
||
| 358 | [ |
||
| 359 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')], |
||
| 360 | '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)', |
||
| 361 | [], |
||
| 362 | ], |
||
| 363 | [ |
||
| 364 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])], |
||
| 365 | '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)', |
||
| 366 | [':month' => 2], |
||
| 367 | ], |
||
| 368 | [ |
||
| 369 | ['=', 'date', $this->mock->query()->select('max(date)')->from('test')->where(['id' => 5])], |
||
| 370 | '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)', |
||
| 371 | [':qp0' => 5], |
||
| 372 | ], |
||
| 373 | [['=', 'a', null], '[[a]] = NULL', []], |
||
| 374 | |||
| 375 | /* operand1 is Expression */ |
||
| 376 | [ |
||
| 377 | ['=', new Expression('date'), '2019-08-01'], |
||
| 378 | 'date = :qp0', |
||
| 379 | [':qp0' => '2019-08-01'], |
||
| 380 | ], |
||
| 381 | [ |
||
| 382 | ['=', $this->mock->query()->select('COUNT(*)')->from('test')->where(['id' => 6]), 0], |
||
| 383 | '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1', |
||
| 384 | [':qp0' => 6, ':qp1' => 0], |
||
| 385 | ], |
||
| 386 | |||
| 387 | /* hash condition */ |
||
| 388 | [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 389 | [ |
||
| 390 | ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2], |
||
| 391 | '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)', |
||
| 392 | [':qp0' => 2], |
||
| 393 | ], |
||
| 394 | [['a' => null], '[[a]] IS NULL', []], |
||
| 395 | |||
| 396 | /* direct conditions */ |
||
| 397 | ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []], |
||
| 398 | [ |
||
| 399 | new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']), |
||
| 400 | 'a = CONCAT(col1, :param1)', |
||
| 401 | ['param1' => 'value1'], |
||
| 402 | ], |
||
| 403 | |||
| 404 | /* Expression with params as operand of 'not' */ |
||
| 405 | [ |
||
| 406 | ['not', new Expression('any_expression(:a)', [':a' => 1])], |
||
| 407 | 'NOT (any_expression(:a))', [':a' => 1], |
||
| 408 | ], |
||
| 409 | [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]], |
||
| 410 | |||
| 411 | /* like */ |
||
| 412 | [['like', 'a', 'b'], '[[a]] LIKE :qp0', [':qp0' => '%b%']], |
||
| 413 | [ |
||
| 414 | ['like', 'a', new Expression(':qp0', [':qp0' => '%b%'])], |
||
| 415 | '[[a]] LIKE :qp0', |
||
| 416 | [':qp0' => '%b%'], |
||
| 417 | ], |
||
| 418 | [['like', new Expression('CONCAT(col1, col2)'), 'b'], 'CONCAT(col1, col2) LIKE :qp0', [':qp0' => '%b%']], |
||
| 419 | ]; |
||
| 420 | |||
| 421 | return $this->replaceQuotes($conditions); |
||
| 422 | } |
||
| 567 |