Conditions | 14 |
Paths | 16 |
Total Lines | 125 |
Code Lines | 80 |
Lines | 44 |
Ratio | 35.2 % |
Changes | 5 | ||
Bugs | 1 | 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 | $sg = $this->getGameService(); |
||
15 | |||
16 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
17 | $channel = $this->getEvent()->getRouteMatch()->getParam('channel'); |
||
18 | |||
19 | $game = $sg->checkGame($identifier); |
||
20 | if (!$game || $game->isClosed()) { |
||
21 | return $this->notFoundAction(); |
||
22 | } |
||
23 | |||
24 | $redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $game, $channel); |
||
|
|||
25 | if ($redirectFb) { |
||
26 | return $redirectFb; |
||
27 | } |
||
28 | |||
29 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
30 | |||
31 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
32 | $redirect = urlencode( |
||
33 | $this->frontendUrl()->fromRoute( |
||
34 | $game->getClassType() . '/play', |
||
35 | array( |
||
36 | 'id' => $game->getIdentifier(), |
||
37 | 'channel' => $channel |
||
38 | ), |
||
39 | array('force_canonical' => true) |
||
40 | ) |
||
41 | ); |
||
42 | |||
43 | return $this->redirect()->toUrl( |
||
44 | $this->frontendUrl()->fromRoute( |
||
45 | 'zfcuser/register', |
||
46 | array('channel' => $channel) |
||
47 | ) . '?redirect='.$redirect |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | if ($game->getOccurrenceType()=='datetime') { |
||
52 | |||
53 | $entry = $sg->play($game, $user); |
||
54 | View Code Duplication | if (!$entry) { |
|
55 | // the user has already taken part of this game and the participation limit has been reached |
||
56 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
57 | |||
58 | return $this->redirect()->toUrl( |
||
59 | $this->frontendUrl()->fromRoute( |
||
60 | 'instantwin/result', |
||
61 | array( |
||
62 | 'id' => $game->getIdentifier(), |
||
63 | 'channel' => $channel |
||
64 | ) |
||
65 | ) |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | // update the winner attribute in entry. |
||
70 | $winner = $sg->IsInstantWinner($game, $user); |
||
71 | |||
72 | $prize = null; |
||
73 | if ($winner) { |
||
74 | $prize = $winner->getPrize(); |
||
75 | } |
||
76 | $viewVariables = array( |
||
77 | 'winner' => $winner, |
||
78 | 'prize' => $prize, |
||
79 | 'over' => false, |
||
80 | ); |
||
81 | } elseif ($game->getOccurrenceType()=='code') { |
||
82 | $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrencecode_form'); |
||
83 | $form->setAttribute( |
||
84 | 'action', |
||
85 | $this->frontendUrl()->fromRoute( |
||
86 | 'instantwin/play', |
||
87 | array( |
||
88 | 'id' => $game->getIdentifier(), |
||
89 | 'channel' => $channel |
||
90 | ), |
||
91 | array('force_canonical' => true) |
||
92 | ) |
||
93 | ); |
||
94 | |||
95 | $occurrence = null; |
||
96 | if ($this->getRequest()->isPost()) { |
||
97 | $form->setData($this->getRequest()->getPost()); |
||
98 | if ($form->isValid()) { |
||
99 | $data = $form->getData('code-input'); |
||
100 | $code = filter_var($data['code-input'], FILTER_SANITIZE_STRING); |
||
101 | $occurrence = $this->getGameService()->isInstantWinner($game, $user, $code); |
||
102 | if (!$occurrence) { |
||
103 | $this->flashMessenger()->addMessage('Le code entré est invalide ou a déjà été utilisé !'); |
||
104 | return $this->redirect()->toUrl( |
||
105 | $this->frontendUrl()->fromRoute( |
||
106 | 'instantwin/play', |
||
107 | array( |
||
108 | 'id' => $game->getIdentifier(), |
||
109 | 'channel' => $channel |
||
110 | ), |
||
111 | array('force_canonical' => true) |
||
112 | ) |
||
113 | ); |
||
114 | View Code Duplication | } else { |
|
115 | return $this->redirect()->toUrl( |
||
116 | $this->frontendUrl()->fromRoute( |
||
117 | 'instantwin/result', |
||
118 | array( |
||
119 | 'id' => $game->getIdentifier(), |
||
120 | 'channel' => $channel |
||
121 | ) |
||
122 | ) |
||
123 | ); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | $viewVariables = array('form' => $form); |
||
128 | } |
||
129 | |||
130 | $viewModel = $this->buildView($game); |
||
131 | if ($viewModel instanceof \Zend\View\Model\ViewModel) { |
||
132 | $viewModel->setVariables($viewVariables); |
||
133 | } |
||
134 | |||
135 | return $viewModel; |
||
136 | } |
||
137 | |||
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: