Conditions | 9 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 47 |
Lines | 11 |
Ratio | 14.86 % |
Changes | 11 | ||
Bugs | 3 | 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 |
||
12 | public function playAction() |
||
13 | { |
||
14 | $redirectFb = $this->checkFbRegistration($this->user, $this->game); |
||
15 | if ($redirectFb) { |
||
16 | return $redirectFb; |
||
17 | } |
||
18 | |||
19 | if ($this->game->getOccurrenceType()=='datetime') { |
||
20 | $entry = $this->getGameService()->play($this->game, $this->user); |
||
21 | View Code Duplication | if (!$entry) { |
|
|
|||
22 | // the user has already taken part of this game and the participation limit has been reached |
||
23 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
24 | |||
25 | return $this->redirect()->toUrl( |
||
26 | $this->frontendUrl()->fromRoute( |
||
27 | 'instantwin/result', |
||
28 | array('id' => $this->game->getIdentifier()) |
||
29 | ) |
||
30 | ); |
||
31 | } |
||
32 | |||
33 | // update the winner attribute in entry. |
||
34 | $occurrence = $this->getGameService()->IsInstantWinner($this->game, $this->user); |
||
35 | |||
36 | $viewVariables = array( |
||
37 | 'occurrence' => $occurrence, |
||
38 | 'entry' => $entry |
||
39 | ); |
||
40 | } elseif ($this->game->getOccurrenceType()=='code') { |
||
41 | $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrencecode_form'); |
||
42 | $form->setAttribute( |
||
43 | 'action', |
||
44 | $this->frontendUrl()->fromRoute( |
||
45 | 'instantwin/play', |
||
46 | array('id' => $this->game->getIdentifier()), |
||
47 | array('force_canonical' => true) |
||
48 | ) |
||
49 | ); |
||
50 | |||
51 | if ($this->getRequest()->isPost()) { |
||
52 | $form->setData($this->getRequest()->getPost()); |
||
53 | if ($form->isValid()) { |
||
54 | $data = $form->getData('code-input'); |
||
55 | $code = filter_var($data['code-input'], FILTER_SANITIZE_STRING); |
||
56 | $occurrence = $this->getGameService()->isInstantWinner($this->game, $this->user, $code); |
||
57 | if (!$occurrence) { |
||
58 | $this->flashMessenger()->addMessage('Le code entré est invalide ou a déjà été utilisé !'); |
||
59 | return $this->redirect()->toUrl( |
||
60 | $this->frontendUrl()->fromRoute( |
||
61 | 'instantwin/play', |
||
62 | array('id' => $this->game->getIdentifier()), |
||
63 | array('force_canonical' => true) |
||
64 | ) |
||
65 | ); |
||
66 | } else { |
||
67 | return $this->redirect()->toUrl( |
||
68 | $this->frontendUrl()->fromRoute( |
||
69 | 'instantwin/result', |
||
70 | array('id' => $this->game->getIdentifier()) |
||
71 | ) |
||
72 | ); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | $viewVariables = array('form' => $form); |
||
77 | } |
||
78 | |||
79 | $viewModel = $this->buildView($this->game); |
||
80 | if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
||
81 | $viewModel->setVariables($viewVariables); |
||
82 | } |
||
83 | |||
84 | return $viewModel; |
||
85 | } |
||
86 | |||
169 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.