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 |
||
361 | public function buildConditions(): array |
||
362 | { |
||
363 | $conditions = [ |
||
364 | /* empty values */ |
||
365 | [['like', 'name', []], '0=1', []], |
||
366 | [['not like', 'name', []], '', []], |
||
367 | [['or like', 'name', []], '0=1', []], |
||
368 | [['or not like', 'name', []], '', []], |
||
369 | |||
370 | /* not */ |
||
371 | [['not', ''], '', []], |
||
372 | [['not', 'name'], 'NOT (name)', []], |
||
373 | [ |
||
374 | [ |
||
375 | 'not', |
||
376 | $this->mock->query()->select('exists')->from('some_table'), |
||
377 | ], |
||
378 | 'NOT ((SELECT [[exists]] FROM [[some_table]]))', [], |
||
379 | ], |
||
380 | |||
381 | /* and */ |
||
382 | [['and', '', ''], '', []], |
||
383 | [['and', '', 'id=2'], 'id=2', []], |
||
384 | [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []], |
||
385 | [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []], |
||
386 | [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]], |
||
387 | [ |
||
388 | [ |
||
389 | 'and', |
||
390 | ['expired' => false], |
||
391 | $this->mock->query()->select('count(*) > 1')->from('queue'), |
||
392 | ], |
||
393 | '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))', |
||
394 | [':qp0' => false], |
||
395 | ], |
||
396 | |||
397 | /* or */ |
||
398 | [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []], |
||
399 | [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []], |
||
400 | [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]], |
||
401 | |||
402 | /* between */ |
||
403 | [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
404 | [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
405 | [ |
||
406 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
407 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
408 | [], |
||
409 | ], |
||
410 | [ |
||
411 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
412 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
413 | [':qp0' => 123], |
||
414 | ], |
||
415 | [ |
||
416 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
417 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
418 | [], |
||
419 | ], |
||
420 | [ |
||
421 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
422 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
423 | [':qp0' => 123], |
||
424 | ], |
||
425 | [ |
||
426 | new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'), |
||
427 | ':qp0 BETWEEN [[create_time]] AND [[update_time]]', |
||
428 | [':qp0' => '2018-02-11'], |
||
429 | ], |
||
430 | [ |
||
431 | new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'), |
||
432 | ':qp0 NOT BETWEEN NOW() AND [[update_time]]', |
||
433 | [':qp0' => '2018-02-11'], |
||
434 | ], |
||
435 | [ |
||
436 | new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'), |
||
437 | 'NOW() BETWEEN [[create_time]] AND [[update_time]]', |
||
438 | [], |
||
439 | ], |
||
440 | [ |
||
441 | new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'), |
||
442 | 'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]', |
||
443 | [], |
||
444 | ], |
||
445 | [ |
||
446 | new BetweenColumnsCondition( |
||
447 | new Expression('NOW()'), |
||
448 | 'NOT BETWEEN', |
||
449 | $this->mock->query()->select('min_date')->from('some_table'), |
||
450 | 'max_date' |
||
451 | ), |
||
452 | 'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]', |
||
453 | [], |
||
454 | ], |
||
455 | [ |
||
456 | new BetweenColumnsCondition( |
||
457 | new Expression('NOW()'), |
||
458 | 'NOT BETWEEN', |
||
459 | new Expression('min_date'), |
||
460 | $this->mock->query()->select('max_date')->from('some_table'), |
||
461 | ), |
||
462 | 'NOW() NOT BETWEEN min_date AND (SELECT [[max_date]] FROM [[some_table]])', |
||
463 | [], |
||
464 | ], |
||
465 | |||
466 | /* in */ |
||
467 | [ |
||
468 | ['in', 'id', [1, 2, $this->mock->query()->select('three')->from('digits')]], |
||
469 | '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))', |
||
470 | [':qp0' => 1, ':qp1' => 2], |
||
471 | ], |
||
472 | [ |
||
473 | ['not in', 'id', [1, 2, 3]], |
||
474 | '[[id]] NOT IN (:qp0, :qp1, :qp2)', |
||
475 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
476 | ], |
||
477 | [ |
||
478 | ['in', 'id', $this->mock->query()->select('id')->from('users')->where(['active' => 1])], |
||
479 | '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
480 | [':qp0' => 1], |
||
481 | ], |
||
482 | [ |
||
483 | ['not in', 'id', $this->mock->query()->select('id')->from('users')->where(['active' => 1])], |
||
484 | '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
485 | [':qp0' => 1], |
||
486 | ], |
||
487 | [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]], |
||
488 | [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]], |
||
489 | [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]], |
||
490 | 'composite in' => [ |
||
491 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]], |
||
492 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
493 | [':qp0' => 1, ':qp1' => 'oy'], |
||
494 | ], |
||
495 | 'composite in (just one column)' => [ |
||
496 | ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]], |
||
497 | '[[id]] IN (:qp0, :qp1)', |
||
498 | [':qp0' => 1, ':qp1' => 2], |
||
499 | ], |
||
500 | 'composite in using array objects (just one column)' => [ |
||
501 | [ |
||
502 | 'in', |
||
503 | new TraversableObject(['id']), |
||
504 | new TraversableObject([['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]), |
||
505 | ], |
||
506 | '[[id]] IN (:qp0, :qp1)', |
||
507 | [':qp0' => 1, ':qp1' => 2], |
||
508 | ], |
||
509 | |||
510 | /* in using array objects. */ |
||
511 | [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
512 | [ |
||
513 | ['in', 'id', new TraversableObject([1, 2, 3])], |
||
514 | '[[id]] IN (:qp0, :qp1, :qp2)', |
||
515 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
516 | ], |
||
517 | |||
518 | /* in using array objects containing null value */ |
||
519 | [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]], |
||
520 | [ |
||
521 | ['in', 'id', new TraversableObject([1, 2, null])], |
||
522 | '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2], |
||
523 | ], |
||
524 | |||
525 | /* not in using array object containing null value */ |
||
526 | [ |
||
527 | ['not in', 'id', new TraversableObject([1, null])], |
||
528 | '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1], |
||
529 | ], |
||
530 | [ |
||
531 | ['not in', 'id', new TraversableObject([1, 2, null])], |
||
532 | '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL', |
||
533 | [':qp0' => 1, ':qp1' => 2], |
||
534 | ], |
||
535 | |||
536 | /* in using array object containing only null value */ |
||
537 | [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []], |
||
538 | [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []], |
||
539 | 'composite in using array objects' => [ |
||
540 | [ |
||
541 | 'in', |
||
542 | new TraversableObject(['id', 'name']), |
||
543 | new TraversableObject([['id' => 1, 'name' => 'oy'], ['id' => 2, 'name' => 'yo']]), |
||
544 | ], |
||
545 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
546 | [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'], |
||
547 | ], |
||
548 | |||
549 | /* in object conditions */ |
||
550 | [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]], |
||
551 | [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]], |
||
552 | [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]], |
||
553 | [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]], |
||
554 | [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
555 | [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
556 | [new InCondition([], 'in', 1), '0=1', []], |
||
557 | [new InCondition([], 'in', [1]), '0=1', []], |
||
558 | [new InCondition(['id', 'name'], 'in', []), '0=1', []], |
||
559 | [ |
||
560 | new InCondition( |
||
561 | ['id'], |
||
562 | 'in', |
||
563 | $this->mock->query()->select('id')->from('users')->where(['active' => 1]), |
||
564 | ), |
||
565 | '([[id]]) IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
566 | [':qp0' => 1], |
||
567 | ], |
||
568 | [ |
||
569 | new InCondition(['id', 'name'], 'in', [['id' => 1]]), |
||
570 | '([[id]], [[name]]) IN ((:qp0, NULL))', |
||
571 | [':qp0' => 1], |
||
572 | ], |
||
573 | [ |
||
574 | new InCondition(['id', 'name'], 'in', [['name' => 'oy']]), |
||
575 | '([[id]], [[name]]) IN ((NULL, :qp0))', |
||
576 | [':qp0' => 'oy'], |
||
577 | ], |
||
578 | [ |
||
579 | new InCondition(['id', 'name'], 'in', [['id' => 1, 'name' => 'oy']]), |
||
580 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
581 | [':qp0' => 1, ':qp1' => 'oy'], |
||
582 | ], |
||
583 | |||
584 | /* exists */ |
||
585 | [ |
||
586 | [ |
||
587 | 'exists', |
||
588 | $this->mock->query()->select('id')->from('users')->where(['active' => 1]), |
||
589 | ], |
||
590 | 'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
591 | [':qp0' => 1], |
||
592 | ], |
||
593 | [ |
||
594 | ['not exists', $this->mock->query()->select('id')->from('users')->where(['active' => 1])], |
||
595 | 'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1], |
||
596 | ], |
||
597 | |||
598 | /* simple conditions */ |
||
599 | [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']], |
||
600 | [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]], |
||
601 | [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']], |
||
602 | [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]], |
||
603 | [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']], |
||
604 | [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]], |
||
605 | [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']], |
||
606 | [ |
||
607 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')], |
||
608 | '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)', |
||
609 | [], |
||
610 | ], |
||
611 | [ |
||
612 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])], |
||
613 | '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)', |
||
614 | [':month' => 2], |
||
615 | ], |
||
616 | [ |
||
617 | ['=', 'date', $this->mock->query()->select('max(date)')->from('test')->where(['id' => 5])], |
||
618 | '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)', |
||
619 | [':qp0' => 5], |
||
620 | ], |
||
621 | [['=', 'a', null], '[[a]] = NULL', []], |
||
622 | |||
623 | /* operand1 is Expression */ |
||
624 | [ |
||
625 | ['=', new Expression('date'), '2019-08-01'], |
||
626 | 'date = :qp0', |
||
627 | [':qp0' => '2019-08-01'], |
||
628 | ], |
||
629 | [ |
||
630 | ['=', $this->mock->query()->select('COUNT(*)')->from('test')->where(['id' => 6]), 0], |
||
631 | '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1', |
||
632 | [':qp0' => 6, ':qp1' => 0], |
||
633 | ], |
||
634 | |||
635 | /* hash condition */ |
||
636 | [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
637 | [ |
||
638 | ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2], |
||
639 | '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)', |
||
640 | [':qp0' => 2], |
||
641 | ], |
||
642 | [['a' => null], '[[a]] IS NULL', []], |
||
643 | |||
644 | /* direct conditions */ |
||
645 | ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []], |
||
646 | [ |
||
647 | new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']), |
||
648 | 'a = CONCAT(col1, :param1)', |
||
649 | ['param1' => 'value1'], |
||
650 | ], |
||
651 | |||
652 | /* Expression with params as operand of 'not' */ |
||
653 | [ |
||
654 | ['not', new Expression('any_expression(:a)', [':a' => 1])], |
||
655 | 'NOT (any_expression(:a))', [':a' => 1], |
||
656 | ], |
||
657 | [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]], |
||
658 | |||
659 | /* like */ |
||
660 | [['like', 'a', 'b'], '[[a]] LIKE :qp0', [':qp0' => '%b%']], |
||
661 | [ |
||
662 | ['like', 'a', new Expression(':qp0', [':qp0' => '%b%'])], |
||
663 | '[[a]] LIKE :qp0', |
||
664 | [':qp0' => '%b%'], |
||
665 | ], |
||
666 | [['like', new Expression('CONCAT(col1, col2)'), 'b'], 'CONCAT(col1, col2) LIKE :qp0', [':qp0' => '%b%']], |
||
667 | ]; |
||
668 | |||
669 | return $this->replaceQuotes($conditions); |
||
670 | } |
||
1067 |