Conditions | 10 |
Paths | 21 |
Total Lines | 62 |
Code Lines | 42 |
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 |
||
29 | public function produceReport(): array |
||
30 | { |
||
31 | $report = []; |
||
32 | $repo = $this->em->getRepository(PurchaseRecord::class); |
||
33 | /** @var EntityRepository $repo */ |
||
34 | $qb = $repo->createQueryBuilder('p'); |
||
35 | $qb->join('p.tickets', 't'); |
||
36 | $purchases = $qb->getQuery()->execute(); |
||
37 | foreach ($purchases as $purchase) |
||
38 | { |
||
39 | /** @var PurchaseRecord $purchase */ |
||
40 | $discountUsed = false; |
||
41 | |||
42 | foreach ($purchase->getTickets() as $ticket) { |
||
43 | /** @var TicketRecord $ticket */ |
||
44 | switch (true) { |
||
45 | case !$purchase->hasDiscountCode(): |
||
46 | $report = $this->recordTicketPurchase( |
||
47 | $report, |
||
48 | $ticket->getTicketType()->getIdentifier(), |
||
49 | '-' |
||
50 | ); |
||
51 | break; |
||
52 | case !$discountUsed && ($purchase->getDiscountCode()->getDiscountType() instanceof Fixed): |
||
53 | $report = $this->recordTicketPurchase( |
||
54 | $report, |
||
55 | $ticket->getTicketType()->getIdentifier(), |
||
56 | $purchase->getDiscountCode()->getCode() |
||
57 | ); |
||
58 | $discountUsed = true; |
||
59 | break; |
||
60 | case ($purchase->getDiscountCode()->getDiscountType() instanceof FixedPerTicket): |
||
61 | $report = $this->recordTicketPurchase( |
||
62 | $report, |
||
63 | $ticket->getTicketType()->getIdentifier(), |
||
64 | $purchase->getDiscountCode()->getCode() |
||
65 | ); |
||
66 | break; |
||
67 | case ($purchase->getDiscountCode()->getDiscountType() instanceof Percentage): |
||
68 | $report = $this->recordTicketPurchase( |
||
69 | $report, |
||
70 | $ticket->getTicketType()->getIdentifier(), |
||
71 | $purchase->getDiscountCode()->getCode() |
||
72 | ); |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | $flattened = []; |
||
79 | |||
80 | foreach ($report as $ticketType => $codes) { |
||
81 | foreach ($codes as $code => $count) { |
||
82 | $flattened[] = [ |
||
83 | 'ticket_type' => $ticketType, |
||
84 | 'discount_code' => $code, |
||
85 | 'count' => $count |
||
86 | ]; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $flattened; |
||
91 | } |
||
108 | } |