| Conditions | 4 |
| Paths | 8 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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 |
||
| 31 | public function testCaseAsLeftOperandOfComparisonAndInExpression() : void |
||
| 32 | { |
||
| 33 | // Rules to define offer status (the order matters): |
||
| 34 | // 1 - active === false? INACTIVE |
||
| 35 | // 2 - closed === true? CLOSED |
||
| 36 | // 3 - balance > 0? ACTIVE |
||
| 37 | // 4 - balance === 0? FINISHED |
||
| 38 | $offersInfo = [ |
||
| 39 | // The fields order is: id, active, closed, balance |
||
| 40 | [1, true, false, 10], // ACTIVE |
||
| 41 | [2, true, false, 0], // FINISHED |
||
| 42 | [3, false, true, 30], // INACTIVE |
||
| 43 | [4, true, true, 20], // CLOSED |
||
| 44 | ]; |
||
| 45 | foreach ($offersInfo as $offerInfo) { |
||
| 46 | $offer = new GH7605Offer($offerInfo[0], $offerInfo[1], $offerInfo[2], $offerInfo[3]); |
||
| 47 | $this->em->persist($offer); |
||
| 48 | } |
||
| 49 | $this->em->flush(); |
||
| 50 | $this->em->clear(); |
||
| 51 | |||
| 52 | $dqlStatusLogic = "CASE |
||
| 53 | WHEN offer.active = FALSE THEN 'INACTIVE' |
||
| 54 | WHEN offer.closed = TRUE THEN 'CLOSED' |
||
| 55 | WHEN offer.balance > 0 THEN 'ACTIVE' |
||
| 56 | ELSE 'FINISHED' |
||
| 57 | END"; |
||
| 58 | |||
| 59 | // ComparisonExpression tests |
||
| 60 | $query = $this->em->createQueryBuilder() |
||
| 61 | ->select('offer') |
||
| 62 | ->from(GH7605Offer::class, 'offer') |
||
| 63 | ->where($dqlStatusLogic . ' = :status') |
||
| 64 | ->setMaxResults(1) |
||
| 65 | ->getQuery(); |
||
| 66 | |||
| 67 | $query->setParameter('status', 'ACTIVE'); |
||
| 68 | $offer = $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT); |
||
| 69 | self::assertEquals(1, $offer->id); |
||
| 70 | |||
| 71 | $query->setParameter('status', 'INACTIVE'); |
||
| 72 | $offer = $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT); |
||
| 73 | self::assertEquals(3, $offer->id); |
||
| 74 | |||
| 75 | // InExpression tests |
||
| 76 | $qb = $this->em->createQueryBuilder() |
||
| 77 | ->select('offer') |
||
| 78 | ->from(GH7605Offer::class, 'offer') |
||
| 79 | ->where($dqlStatusLogic . ' IN (:status)') |
||
| 80 | ->orderBy('offer.id', 'ASC'); |
||
| 81 | $query = $qb->getQuery(); |
||
| 82 | $query->setParameter('status', ['INACTIVE', 'CLOSED']); |
||
| 83 | $offers = $query->getResult(AbstractQuery::HYDRATE_OBJECT); |
||
| 84 | $expectedIds = [3, 4]; |
||
| 85 | foreach ($offers as $i => $offer) { |
||
| 86 | self::assertEquals($expectedIds[$i], $offer->id); |
||
| 87 | } |
||
| 88 | |||
| 89 | $query = $qb->where($dqlStatusLogic . ' NOT IN (:status)')->getQuery(); |
||
| 90 | $query->setParameter('status', ['FINISHED', 'INACTIVE']); |
||
| 91 | $offers = $query->getResult(AbstractQuery::HYDRATE_OBJECT); |
||
| 92 | $expectedIds = [1, 4]; |
||
| 93 | foreach ($offers as $i => $offer) { |
||
| 94 | self::assertEquals($expectedIds[$i], $offer->id); |
||
| 95 | } |
||
| 131 |