Conditions | 26 |
Paths | 7824 |
Total Lines | 132 |
Code Lines | 78 |
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 |
||
108 | public function saveTicketAction(Request $request) |
||
109 | { |
||
110 | if (!$this->getDataStore()->fetchSetting('selfSubmission')) { |
||
111 | $this->denyAccessUnlessGranted(self::MANAGER_REQUIRED_ROLE); |
||
112 | } |
||
113 | |||
114 | $title = $request->get('title'); |
||
115 | $songKey = $request->get('songId'); |
||
116 | $band = $request->get('band') ?: []; // band must be array even if null (as passed by AJAX if no performers) |
||
117 | $private = $request->get('private') === 'true' ? 1 : 0; |
||
118 | $blocking = $request->get('blocking') === 'true' ? 1 : 0; |
||
119 | $existingTicketId = $request->get('existingTicketId'); |
||
120 | $submissionKey = trim($request->get('submissionKey')); |
||
121 | $userId = null; |
||
122 | |||
123 | if ($this->isGranted(self::MANAGER_REQUIRED_ROLE)) { |
||
124 | $user = $this->getUser(); |
||
125 | /** @var User $user */ |
||
126 | $userId = $user->getId(); |
||
127 | } else { |
||
128 | $blocking = 0; |
||
129 | $private = 1; |
||
130 | if ($existingTicketId) { |
||
131 | throw new AccessDeniedException('Cannot modify existing tickets'); |
||
132 | } |
||
133 | |||
134 | // Check song is unused, check no performers have more than 3 upcoming songs |
||
135 | // Ensure that name format is valid |
||
136 | |||
137 | $dataErrors = []; |
||
138 | $fixableError = false; |
||
139 | |||
140 | $ticketsForSong = $this->getDataStore()->getQueueEntriesForSongId($songKey); |
||
141 | |||
142 | if (count($ticketsForSong)) { |
||
143 | $dataErrors[] = 'The song is already taken'; |
||
144 | } |
||
145 | |||
146 | $performerStats = $this->getDataStore()->generatePerformerStats(); |
||
147 | |||
148 | foreach ($band as $instrumentCandidates) { |
||
149 | foreach ($instrumentCandidates as $candidate) { |
||
150 | $candidate = trim($candidate); |
||
151 | if ($candidate) { |
||
152 | if (preg_match('/\w+ \w+/', $candidate)) { |
||
153 | foreach ($performerStats as $performerStat) { |
||
154 | if (($performerStat['songsPending'] > 2) && |
||
155 | !strcasecmp($candidate, $performerStat['performerName']) |
||
156 | ) { |
||
157 | $dataErrors[] = "$candidate has too many songs pending"; |
||
158 | } |
||
159 | } |
||
160 | } else { |
||
161 | $dataErrors[] = "'$candidate' is not a valid name"; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if ($this->getDataStore()->fetchSetting('selfSubmissionKey') && |
||
168 | strcasecmp($submissionKey, $this->getDataStore()->fetchSetting('selfSubmissionKey')) |
||
169 | ) { |
||
170 | $dataErrors[] = 'Secret code wrong or missing'; |
||
171 | $fixableError = 'E_BAD_SECRET'; |
||
172 | } |
||
173 | |||
174 | if ($dataErrors) { |
||
175 | $responseData = [ |
||
176 | 'status' => 'error', |
||
177 | 'message' => implode(', ', $dataErrors) |
||
178 | ]; |
||
179 | if ($fixableError) { |
||
180 | $responseData['error'] = $fixableError; // more than one error = faily death. |
||
181 | } |
||
182 | return new JsonResponse($responseData, 200); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $song = null; |
||
187 | $songId = null; |
||
188 | |||
189 | if (preg_match('/^[a-f0-9]{6}$/i', $songKey)) { |
||
190 | $song = $this->getDataStore()->fetchSongByKey($songKey); |
||
191 | } elseif (preg_match('/^\d+$/', $songKey)) { |
||
192 | $song = $this->getDataStore()->fetchSongRowById($songKey); |
||
193 | } |
||
194 | |||
195 | if ($song) { |
||
196 | $songId = $song['id']; |
||
197 | } |
||
198 | |||
199 | if (!$title) { |
||
200 | $title = null; |
||
201 | } |
||
202 | |||
203 | if ($existingTicketId) { |
||
204 | $ticketId = $existingTicketId; |
||
205 | } else { |
||
206 | $ticketId = $this->getDataStore()->storeNewTicket($title, $songId, $userId); |
||
207 | } |
||
208 | |||
209 | // update even new tickets so that we can add any new columns easily |
||
210 | $updated = ['title' => $title, 'songId' => $songId, 'blocking' => $blocking, 'private' => $private]; |
||
211 | |||
212 | $this->getDataStore()->updateTicketById( |
||
213 | $ticketId, |
||
214 | $updated |
||
215 | ); |
||
216 | |||
217 | // think this is legacy? |
||
218 | if ($this->bandIdentifier === self::BAND_IDENTIFIER_PERFORMERS) { |
||
219 | $this->getDataStore()->storeBandToTicket($ticketId, $band); |
||
220 | } |
||
221 | |||
222 | $ticket = $this->getDataStore()->fetchTicketById($ticketId); |
||
223 | |||
224 | $ticket = $this->getDataStore()->expandTicketData($ticket); |
||
225 | |||
226 | $responseData = [ |
||
227 | 'ticket' => $ticket, |
||
228 | 'performers' => $this->getDataStore()->generatePerformerStats(), |
||
229 | 'status' => 'ok' |
||
230 | ]; |
||
231 | |||
232 | if ($ticketId) { |
||
233 | $jsonResponse = new JsonResponse($responseData); |
||
234 | } else { |
||
235 | $jsonResponse = new JsonResponse($responseData, 500); |
||
236 | } |
||
237 | |||
238 | return $jsonResponse; |
||
239 | } |
||
240 | |||
290 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.