Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | class StatsController |
||
27 | { |
||
28 | public function __construct( |
||
29 | ContentReader $reader, |
||
30 | EventRepository $eventRepo, |
||
31 | TicketRepository $ticketRepo, |
||
32 | UnregistrationRepository $unregistrationRepo, |
||
33 | EngineInterface $renderer |
||
34 | ) |
||
35 | { |
||
36 | $this->eventRepo = $eventRepo; |
||
|
|||
37 | $this->ticketRepo = $ticketRepo; |
||
38 | $this->unregistrationRepo = $unregistrationRepo; |
||
39 | $this->reader = $reader; |
||
40 | $this->renderer = $renderer; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Generates the event's statistics as json. |
||
45 | * |
||
46 | * @param Request $request |
||
47 | * |
||
48 | * @return Response |
||
49 | */ |
||
50 | public function statsAction(Request $request) |
||
70 | |||
71 | /** |
||
72 | * Creates the JSON used to render the stats. |
||
73 | * |
||
74 | * @param Request $request |
||
75 | * |
||
76 | * @return Response |
||
77 | */ |
||
78 | protected function statsJson(Request $request) |
||
79 | { |
||
80 | $response = new Response(); |
||
81 | $response->setTtl(10); |
||
82 | $response->setPublic(); |
||
83 | if ($response->isNotModified($request)) { |
||
84 | return $response; |
||
85 | } |
||
86 | $event = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.')); |
||
87 | $tickets = $this->ticketRepo->getTicketsForEvent($event); |
||
88 | $unregistrations = $this->unregistrationRepo->getUnregistrationsForEvent($event); |
||
89 | |||
90 | $stats = array( |
||
91 | 'checkins' => array( |
||
92 | 'sa' => $this->getCheckinsPerDay($tickets, Ticket::DAY_SATURDAY), |
||
93 | 'sa_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SATURDAY), |
||
94 | 'su' => $this->getCheckinsPerDay($tickets, Ticket::DAY_SUNDAY), |
||
95 | 'su_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SUNDAY), |
||
96 | 'unique' => array( |
||
97 | 'sa' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SATURDAY), |
||
98 | 'su' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY), |
||
99 | 'both' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY, true), |
||
100 | ), |
||
101 | 'noshows' => array( |
||
102 | 'sa' => $this->getNoShows($tickets, Ticket::DAY_SATURDAY), |
||
103 | 'su' => $this->getNoShows($tickets, Ticket::DAY_SUNDAY), |
||
104 | ) |
||
105 | ), |
||
106 | 'unregistrations' => $this->getUnregistrationsPerDay($unregistrations) |
||
107 | ); |
||
108 | |||
109 | $data = array('stats' => $stats); |
||
110 | $response->setContent(json_encode($data)); |
||
111 | $response->setCharset('utf-8'); |
||
112 | $response->headers->set('Content-Type', 'application/json'); |
||
113 | return $response; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns the number of checkins per day. |
||
118 | * |
||
119 | * @param Ticket[] $tickets |
||
120 | * @param integer $day |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | protected function getCheckinsPerDay(array $tickets, $day) |
||
125 | { |
||
126 | View Code Duplication | return array_reduce($tickets, function ($count, Ticket $ticket) use ($day) { |
|
127 | return $count + ($ticket->getType() === Registration::TYPE_NORMAL && $ticket->isCheckedIn() && $ticket->getDay() == $day ? 1 : 0); |
||
128 | }, 0); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns the number of attendees who have checked in only on the given day |
||
133 | * |
||
134 | * @param Ticket[] $tickets |
||
135 | * @param integer $day |
||
136 | * @param boolean $both |
||
137 | * |
||
138 | * @return integer |
||
139 | */ |
||
140 | protected function getUniqueDayCheckins(array $tickets, $day, $both = false) |
||
163 | |||
164 | protected function getNoShows(array $tickets, $day) |
||
175 | |||
176 | /** |
||
177 | * Returns the number of checkins per hour summarized for every 10 minutes. |
||
178 | * |
||
179 | * @param Ticket[] $tickets |
||
180 | * @param integer $day |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | protected function getCheckinsPerHour(array $tickets, $day) |
||
203 | |||
204 | /** |
||
205 | * Returns the number of checkins per day. |
||
206 | * |
||
207 | * @param Unregistration[] $unregistrations |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | protected function getUnregistrationsPerDay($unregistrations) |
||
231 | } |
||
232 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: