| Conditions | 2 |
| Paths | 2 |
| Total Lines | 373 |
| Code Lines | 217 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 190 | public function buildConditionsProvider(): array |
||
| 191 | { |
||
| 192 | $conditions = [ |
||
| 193 | /* empty values */ |
||
| 194 | [['like', 'name', []], '0=1', []], |
||
| 195 | [['not like', 'name', []], '', []], |
||
| 196 | [['or like', 'name', []], '0=1', []], |
||
| 197 | [['or not like', 'name', []], '', []], |
||
| 198 | |||
| 199 | /* not */ |
||
| 200 | [['not', 'name'], 'NOT (name)', []], |
||
| 201 | [ |
||
| 202 | [ |
||
| 203 | 'not', |
||
| 204 | (new Query($this->db)) |
||
| 205 | ->select('exists') |
||
| 206 | ->from('some_table'), |
||
| 207 | ], |
||
| 208 | 'NOT ((SELECT [[exists]] FROM [[some_table]]))', [], |
||
| 209 | ], |
||
| 210 | |||
| 211 | /* and */ |
||
| 212 | [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []], |
||
| 213 | [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []], |
||
| 214 | [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]], |
||
| 215 | [ |
||
| 216 | [ |
||
| 217 | 'and', |
||
| 218 | ['expired' => false], |
||
| 219 | (new Query($this->db)) |
||
| 220 | ->select('count(*) > 1') |
||
| 221 | ->from('queue'), |
||
| 222 | ], |
||
| 223 | '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))', |
||
| 224 | [':qp0' => false], |
||
| 225 | ], |
||
| 226 | |||
| 227 | /* or */ |
||
| 228 | [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []], |
||
| 229 | [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []], |
||
| 230 | [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]], |
||
| 231 | |||
| 232 | /* between */ |
||
| 233 | [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 234 | [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 235 | [ |
||
| 236 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 237 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 238 | [], |
||
| 239 | ], |
||
| 240 | [ |
||
| 241 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 242 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 243 | [':qp0' => 123], |
||
| 244 | ], |
||
| 245 | [ |
||
| 246 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 247 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 248 | [], |
||
| 249 | ], |
||
| 250 | [ |
||
| 251 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 252 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 253 | [':qp0' => 123], |
||
| 254 | ], |
||
| 255 | [ |
||
| 256 | new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'), |
||
| 257 | ':qp0 BETWEEN [[create_time]] AND [[update_time]]', |
||
| 258 | [':qp0' => '2018-02-11'], |
||
| 259 | ], |
||
| 260 | [ |
||
| 261 | new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'), |
||
| 262 | ':qp0 NOT BETWEEN NOW() AND [[update_time]]', |
||
| 263 | [':qp0' => '2018-02-11'], |
||
| 264 | ], |
||
| 265 | [ |
||
| 266 | new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'), |
||
| 267 | 'NOW() BETWEEN [[create_time]] AND [[update_time]]', |
||
| 268 | [], |
||
| 269 | ], |
||
| 270 | [ |
||
| 271 | new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'), |
||
| 272 | 'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]', |
||
| 273 | [], |
||
| 274 | ], |
||
| 275 | [ |
||
| 276 | new BetweenColumnsCondition( |
||
| 277 | new Expression('NOW()'), |
||
| 278 | 'NOT BETWEEN', |
||
| 279 | (new Query($this->db)) |
||
| 280 | ->select('min_date') |
||
| 281 | ->from('some_table'), |
||
| 282 | 'max_date' |
||
| 283 | ), |
||
| 284 | 'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]', |
||
| 285 | [], |
||
| 286 | ], |
||
| 287 | |||
| 288 | /* in */ |
||
| 289 | [ |
||
| 290 | [ |
||
| 291 | 'in', |
||
| 292 | 'id', |
||
| 293 | [ |
||
| 294 | 1, |
||
| 295 | 2, |
||
| 296 | (new Query($this->db)) |
||
| 297 | ->select('three') |
||
| 298 | ->from('digits'), |
||
| 299 | ], |
||
| 300 | ], |
||
| 301 | '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))', |
||
| 302 | [':qp0' => 1, ':qp1' => 2], |
||
| 303 | ], |
||
| 304 | [ |
||
| 305 | ['not in', 'id', [1, 2, 3]], |
||
| 306 | '[[id]] NOT IN (:qp0, :qp1, :qp2)', |
||
| 307 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 308 | ], |
||
| 309 | [ |
||
| 310 | [ |
||
| 311 | 'in', |
||
| 312 | 'id', |
||
| 313 | (new Query($this->db)) |
||
| 314 | ->select('id') |
||
| 315 | ->from('users') |
||
| 316 | ->where(['active' => 1]), |
||
| 317 | ], |
||
| 318 | '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 319 | [':qp0' => 1], |
||
| 320 | ], |
||
| 321 | [ |
||
| 322 | [ |
||
| 323 | 'not in', |
||
| 324 | 'id', |
||
| 325 | (new Query($this->db)) |
||
| 326 | ->select('id') |
||
| 327 | ->from('users') |
||
| 328 | ->where(['active' => 1]), |
||
| 329 | ], |
||
| 330 | '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 331 | [':qp0' => 1], |
||
| 332 | ], |
||
| 333 | [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 334 | [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 335 | [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 336 | 'composite in' => [ |
||
| 337 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]], |
||
| 338 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
| 339 | [':qp0' => 1, ':qp1' => 'oy'], |
||
| 340 | ], |
||
| 341 | 'composite in (just one column)' => [ |
||
| 342 | ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]], |
||
| 343 | '[[id]] IN (:qp0, :qp1)', |
||
| 344 | [':qp0' => 1, ':qp1' => 2], |
||
| 345 | ], |
||
| 346 | 'composite in using array objects (just one column)' => [ |
||
| 347 | ['in', new TraversableObject(['id']), new TraversableObject([ |
||
| 348 | ['id' => 1, 'name' => 'Name1'], |
||
| 349 | ['id' => 2, 'name' => 'Name2'], |
||
| 350 | ])], |
||
| 351 | '[[id]] IN (:qp0, :qp1)', |
||
| 352 | [':qp0' => 1, ':qp1' => 2], |
||
| 353 | ], |
||
| 354 | |||
| 355 | /* in using array objects. */ |
||
| 356 | [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 357 | [ |
||
| 358 | ['in', 'id', new TraversableObject([1, 2, 3])], |
||
| 359 | '[[id]] IN (:qp0, :qp1, :qp2)', |
||
| 360 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 361 | ], |
||
| 362 | |||
| 363 | /* in using array objects containing null value */ |
||
| 364 | [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]], |
||
| 365 | [ |
||
| 366 | ['in', 'id', new TraversableObject([1, 2, null])], |
||
| 367 | '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2], |
||
| 368 | ], |
||
| 369 | |||
| 370 | /* not in using array object containing null value */ |
||
| 371 | [ |
||
| 372 | ['not in', 'id', new TraversableObject([1, null])], |
||
| 373 | '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1], |
||
| 374 | ], |
||
| 375 | [ |
||
| 376 | ['not in', 'id', new TraversableObject([1, 2, null])], |
||
| 377 | '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL', |
||
| 378 | [':qp0' => 1, ':qp1' => 2], |
||
| 379 | ], |
||
| 380 | |||
| 381 | /* in using array object containing only null value */ |
||
| 382 | [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []], |
||
| 383 | [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []], |
||
| 384 | 'composite in using array objects' => [ |
||
| 385 | ['in', new TraversableObject(['id', 'name']), new TraversableObject([ |
||
| 386 | ['id' => 1, 'name' => 'oy'], |
||
| 387 | ['id' => 2, 'name' => 'yo'], |
||
| 388 | ])], |
||
| 389 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 390 | [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'], |
||
| 391 | ], |
||
| 392 | |||
| 393 | /* in object conditions */ |
||
| 394 | [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 395 | [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 396 | [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 397 | [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 398 | [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 399 | [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 400 | |||
| 401 | /* exists */ |
||
| 402 | [ |
||
| 403 | [ |
||
| 404 | 'exists', |
||
| 405 | (new Query($this->db)) |
||
| 406 | ->select('id') |
||
| 407 | ->from('users') |
||
| 408 | ->where(['active' => 1]), |
||
| 409 | ], |
||
| 410 | 'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 411 | [':qp0' => 1], |
||
| 412 | ], |
||
| 413 | [ |
||
| 414 | [ |
||
| 415 | 'not exists', |
||
| 416 | (new Query($this->db)) |
||
| 417 | ->select('id') |
||
| 418 | ->from('users') |
||
| 419 | ->where(['active' => 1]), |
||
| 420 | ], |
||
| 421 | 'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1], |
||
| 422 | ], |
||
| 423 | |||
| 424 | /* simple conditions */ |
||
| 425 | [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']], |
||
| 426 | [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]], |
||
| 427 | [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']], |
||
| 428 | [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]], |
||
| 429 | [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']], |
||
| 430 | [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]], |
||
| 431 | [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']], |
||
| 432 | [ |
||
| 433 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')], |
||
| 434 | '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)', |
||
| 435 | [], |
||
| 436 | ], |
||
| 437 | [ |
||
| 438 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])], |
||
| 439 | '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)', |
||
| 440 | [':month' => 2], |
||
| 441 | ], |
||
| 442 | [ |
||
| 443 | [ |
||
| 444 | '=', |
||
| 445 | 'date', |
||
| 446 | (new Query($this->db)) |
||
| 447 | ->select('max(date)') |
||
| 448 | ->from('test') |
||
| 449 | ->where(['id' => 5]), |
||
| 450 | ], |
||
| 451 | '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)', |
||
| 452 | [':qp0' => 5], |
||
| 453 | ], |
||
| 454 | |||
| 455 | /* operand1 is Expression */ |
||
| 456 | [ |
||
| 457 | ['=', new Expression('date'), '2019-08-01'], |
||
| 458 | 'date = :qp0', |
||
| 459 | [':qp0' => '2019-08-01'], |
||
| 460 | ], |
||
| 461 | [ |
||
| 462 | [ |
||
| 463 | '=', |
||
| 464 | (new Query($this->db)) |
||
| 465 | ->select('COUNT(*)') |
||
| 466 | ->from('test') |
||
| 467 | ->where(['id' => 6]), |
||
| 468 | 0, |
||
| 469 | ], |
||
| 470 | '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1', |
||
| 471 | [':qp0' => 6, ':qp1' => 0], |
||
| 472 | ], |
||
| 473 | |||
| 474 | /* hash condition */ |
||
| 475 | [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 476 | [ |
||
| 477 | ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2], |
||
| 478 | '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)', |
||
| 479 | [':qp0' => 2], |
||
| 480 | ], |
||
| 481 | |||
| 482 | /* direct conditions */ |
||
| 483 | ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []], |
||
| 484 | [ |
||
| 485 | new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']), |
||
| 486 | 'a = CONCAT(col1, :param1)', |
||
| 487 | ['param1' => 'value1'], |
||
| 488 | ], |
||
| 489 | |||
| 490 | /* Expression with params as operand of 'not' */ |
||
| 491 | [ |
||
| 492 | ['not', new Expression('any_expression(:a)', [':a' => 1])], |
||
| 493 | 'NOT (any_expression(:a))', [':a' => 1], |
||
| 494 | ], |
||
| 495 | [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]], |
||
| 496 | ]; |
||
| 497 | |||
| 498 | $conditions = match ($this->db->getDriverName()) { |
||
| 499 | 'sqlsrv', 'sqlite' => array_merge($conditions, [ |
||
| 500 | [ |
||
| 501 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 502 | '(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))', |
||
| 503 | [':qp0' => 1, |
||
| 504 | ':qp1' => 'foo', |
||
| 505 | ':qp2' => 2, |
||
| 506 | ':qp3' => 'bar',], |
||
| 507 | ], |
||
| 508 | [ |
||
| 509 | ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 510 | '(([[id]] != :qp0 OR [[name]] != :qp1) AND ([[id]] != :qp2 OR [[name]] != :qp3))', |
||
| 511 | [':qp0' => 1, |
||
| 512 | ':qp1' => 'foo', |
||
| 513 | ':qp2' => 2, |
||
| 514 | ':qp3' => 'bar',], |
||
| 515 | ], |
||
| 516 | //[['in', ['id', 'name'], (new Query())->select(['id', 'name'])->from('users')->where(['active' => 1])], 'EXISTS (SELECT 1 FROM (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0) AS a WHERE a.[[id]] = [[id AND a.]]name[[ = ]]name`)', [':qp0' => 1] ], |
||
| 517 | //[ ['not in', ['id', 'name'], (new Query())->select(['id', 'name'])->from('users')->where(['active' => 1])], 'NOT EXISTS (SELECT 1 FROM (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0) AS a WHERE a.[[id]] = [[id]] AND a.[[name = ]]name`)', [':qp0' => 1] ], |
||
| 518 | ]), |
||
| 519 | default => array_merge($conditions, [ |
||
| 520 | [ |
||
| 521 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 522 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 523 | [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'], |
||
| 524 | ], |
||
| 525 | [ |
||
| 526 | ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 527 | '([[id]], [[name]]) NOT IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 528 | [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'], |
||
| 529 | ], |
||
| 530 | [ |
||
| 531 | [ |
||
| 532 | 'in', |
||
| 533 | ['id', 'name'], |
||
| 534 | (new Query($this->db)) |
||
| 535 | ->select(['id', 'name']) |
||
| 536 | ->from('users') |
||
| 537 | ->where(['active' => 1]), |
||
| 538 | ], |
||
| 539 | '([[id]], [[name]]) IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 540 | [':qp0' => 1], |
||
| 541 | ], |
||
| 542 | [ |
||
| 543 | [ |
||
| 544 | 'not in', |
||
| 545 | ['id', 'name'], |
||
| 546 | (new Query($this->db)) |
||
| 547 | ->select(['id', 'name']) |
||
| 548 | ->from('users') |
||
| 549 | ->where(['active' => 1]), |
||
| 550 | ], |
||
| 551 | '([[id]], [[name]]) NOT IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 552 | [':qp0' => 1], |
||
| 553 | ], |
||
| 554 | ]), |
||
| 555 | }; |
||
| 556 | |||
| 557 | /* adjust dbms specific escaping */ |
||
| 558 | foreach ($conditions as $i => $condition) { |
||
| 559 | $conditions[$i][1] = DbHelper::replaceQuotes($condition[1], $this->db->getDriverName()); |
||
| 560 | } |
||
| 561 | |||
| 562 | return $conditions; |
||
| 563 | } |
||
| 1076 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths