Conditions | 15 |
Paths | 44 |
Total Lines | 96 |
Code Lines | 63 |
Lines | 16 |
Ratio | 16.67 % |
Changes | 3 | ||
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 |
||
138 | public function resultAction() |
||
139 | { |
||
140 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
141 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
142 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
143 | $sg = $this->getGameService(); |
||
144 | |||
145 | $game = $sg->checkGame($identifier); |
||
146 | if (!$game || $game->isClosed()) { |
||
147 | return $this->notFoundAction(); |
||
148 | } |
||
149 | |||
150 | $lastEntry = $sg->findLastInactiveEntry($game, $user); |
||
151 | View Code Duplication | if (!$lastEntry) { |
|
152 | return $this->redirect()->toUrl( |
||
153 | $this->frontendUrl()->fromRoute( |
||
154 | 'instantwin', |
||
155 | array('id' => $game->getIdentifier(), 'channel' => $channel), |
||
156 | array('force_canonical' => true) |
||
157 | ) |
||
158 | ); |
||
159 | } |
||
160 | $winner = $lastEntry->getWinner(); |
||
161 | $occurrence = null; |
||
162 | |||
163 | // On tente de récupèrer l'occurrence si elle existe pour avoir accés au lot associé |
||
164 | $occurrences = $sg->getInstantWinOccurrenceMapper()->findBy( |
||
165 | array('instantwin' => $game->getId(), 'entry' => $lastEntry->getId()) |
||
166 | ); |
||
167 | if (!empty($occurrences)) { |
||
168 | $occurrence = current($occurrences); |
||
169 | } |
||
170 | |||
171 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
172 | $redirect = urlencode( |
||
173 | $this->frontendUrl()->fromRoute( |
||
174 | 'instantwin/result', |
||
175 | array('id' => $game->getIdentifier(), 'channel' => $channel) |
||
176 | ) |
||
177 | ); |
||
178 | return $this->redirect()->toUrl( |
||
179 | $this->frontendUrl()->fromRoute( |
||
180 | 'zfcuser/register', |
||
181 | array('channel' => $channel) |
||
182 | ) . '?redirect='.$redirect |
||
183 | ); |
||
184 | } |
||
185 | |||
186 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
187 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
188 | 'instantwin', |
||
189 | array('id' => $game->getIdentifier(), 'channel' => $channel), |
||
190 | array('force_canonical' => true) |
||
191 | ).'?key='.$secretKey; |
||
192 | // With core shortener helper |
||
193 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
194 | |||
195 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
196 | $form->setAttribute('method', 'post'); |
||
197 | |||
198 | $statusMail = null; |
||
199 | if ($this->getRequest()->isPost()) { |
||
200 | $data = $this->getRequest()->getPost()->toArray(); |
||
201 | $form->setData($data); |
||
202 | if ($form->isValid()) { |
||
203 | if (isset($data['email1']) || isset($data['email2']) || isset($data['email3'])) { |
||
204 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
205 | if ($result) { |
||
206 | $statusMail = true; |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | $prize = null; |
||
213 | if ($occurrence instanceof \PlaygroundGame\Entity\InstantWinOccurrence) { |
||
214 | $prize = $occurrence->getPrize(); |
||
215 | } |
||
216 | |||
217 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
218 | $viewModel = $this->buildView($game); |
||
219 | |||
220 | $this->getGameService()->sendMail($game, $user, $lastEntry, $prize); |
||
221 | |||
222 | if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
||
223 | $viewModel->setVariables(array( |
||
224 | 'occurrence' => $occurrence, |
||
225 | 'statusMail' => $statusMail, |
||
226 | 'winner' => $winner, |
||
227 | 'form' => $form, |
||
228 | 'socialLinkUrl' => $socialLinkUrl, |
||
229 | 'secretKey' => $secretKey, |
||
230 | )); |
||
231 | } |
||
232 | return $viewModel; |
||
233 | } |
||
234 | |||
244 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: