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:
Complex classes like PostVoteController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PostVoteController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class PostVoteController extends GameController |
||
8 | { |
||
9 | /** |
||
10 | * @var gameService |
||
11 | */ |
||
12 | protected $gameService; |
||
13 | |||
14 | /** |
||
15 | * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu) |
||
16 | * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller) |
||
17 | * --DONE-- 3. don't answer to questions (form is checked controller side) |
||
18 | * 4. try to game the chrono |
||
19 | * 5. try to play again |
||
20 | * 6. try to change answers |
||
21 | * --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu) |
||
22 | */ |
||
23 | public function playAction() |
||
24 | { |
||
25 | $redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $this->game); |
||
|
|||
26 | if ($redirectFb) { |
||
27 | return $redirectFb; |
||
28 | } |
||
29 | |||
30 | $entry = $this->getGameService()->play($this->game, $user); |
||
31 | |||
32 | if (!$entry) { |
||
33 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $user); |
||
34 | if ($lastEntry === null) { |
||
35 | return $this->redirect()->toUrl( |
||
36 | $this->frontendUrl()->fromRoute( |
||
37 | 'postvote', |
||
38 | array( |
||
39 | 'id' => $this->game->getIdentifier(), |
||
40 | |||
41 | ) |
||
42 | ) |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | $lastEntryId = $lastEntry->getId(); |
||
47 | $lastPost = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId)); |
||
48 | $postId = $lastPost->getId(); |
||
49 | if ($lastPost->getStatus() == 2) { |
||
50 | // the user has already taken part of this game and the participation limit has been reached |
||
51 | $this->flashMessenger()->addMessage( |
||
52 | $this->getServiceLocator()->get('translator')->translate('You have already a Post') |
||
53 | ); |
||
54 | |||
55 | return $this->redirect()->toUrl( |
||
56 | $this->frontendUrl()->fromRoute( |
||
57 | 'postvote/post', |
||
58 | array( |
||
59 | 'id' => $this->game->getIdentifier(), |
||
60 | 'post' => $postId, |
||
61 | |||
62 | ) |
||
63 | ) |
||
64 | ); |
||
65 | } else { |
||
66 | $this->flashMessenger()->addMessage( |
||
67 | $this->getServiceLocator()->get('translator')->translate('Your Post is waiting for validation') |
||
68 | ); |
||
69 | |||
70 | return $this->redirect()->toUrl( |
||
71 | $this->frontendUrl()->fromRoute( |
||
72 | 'postvote/post', |
||
73 | array( |
||
74 | 'id' => $this->game->getIdentifier(), |
||
75 | 'post' => $postId, |
||
76 | |||
77 | ) |
||
78 | ) |
||
79 | ); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | if (! $this->game->getForm()) { |
||
84 | return $this->redirect()->toUrl( |
||
85 | $this->frontendUrl()->fromRoute( |
||
86 | 'postvote', |
||
87 | array( |
||
88 | 'id' => $this->game->getIdentifier(), |
||
89 | |||
90 | ) |
||
91 | ) |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | $form = $this->getGameService()->createFormFromJson($this->game->getForm()->getForm(), 'postvoteForm'); |
||
96 | |||
97 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
98 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
99 | if ($post) { |
||
100 | foreach ($post->getPostElements() as $element) { |
||
101 | try { |
||
102 | $form->get($element->getName())->setValue($element->getValue()); |
||
103 | |||
104 | $elementType = $form->get($element->getName())->getAttribute('type'); |
||
105 | if ($elementType == 'file' && $element->getValue() != '') { |
||
106 | $filter = $form->getInputFilter(); |
||
107 | $elementInput = $filter->get($element->getName()); |
||
108 | $elementInput->setRequired(false); |
||
109 | $form->get($element->getName())->setAttribute('required', false); |
||
110 | } |
||
111 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $viewModel = $this->buildView($this->game); |
||
117 | |||
118 | if ($this->getRequest()->isPost()) { |
||
119 | // POST Request: Process form |
||
120 | $data = array_merge_recursive( |
||
121 | $this->getRequest()->getPost()->toArray(), |
||
122 | $this->getRequest()->getFiles()->toArray() |
||
123 | ); |
||
124 | |||
125 | $form->setData($data); |
||
126 | |||
127 | if ($form->isValid()) { |
||
128 | $data = $form->getData(); |
||
129 | $post = $this->getGameService()->createPost($data, $this->game, $user, $form); |
||
130 | |||
131 | if ($post && !empty($this->game->nextStep('play'))) { |
||
132 | // determine the route where the user should go |
||
133 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
134 | 'postvote/'.$this->game->nextStep('play'), |
||
135 | array( |
||
136 | 'id' => $this->game->getIdentifier(), |
||
137 | |||
138 | ) |
||
139 | ); |
||
140 | |||
141 | return $this->redirect()->toUrl($redirectUrl); |
||
142 | } |
||
143 | } else { |
||
144 | $messages = $form->getMessages(); |
||
145 | $viewModel = $this->buildView($this->game); |
||
146 | $viewModel->setVariables(array( |
||
147 | 'success' => false, |
||
148 | 'message' => implode(',', $messages['title']), |
||
149 | )); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $viewModel->setVariables(array( |
||
154 | 'playerData' => $entry->getPlayerData(), |
||
155 | 'form' => $form, |
||
156 | 'post' => $post, |
||
157 | )); |
||
158 | |||
159 | return $viewModel; |
||
160 | } |
||
161 | |||
162 | public function previewAction() |
||
163 | { |
||
164 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
165 | $entry = $this->getGameService()->findLastActiveEntry($this->game, $user); |
||
166 | |||
167 | View Code Duplication | if (!$entry) { |
|
168 | // the user has already taken part of this game and the participation limit has been reached |
||
169 | return $this->redirect()->toUrl( |
||
170 | $this->frontendUrl()->fromRoute( |
||
171 | 'postvote/'.$this->game->nextStep('preview'), |
||
172 | array('id' => $this->game->getIdentifier()) |
||
173 | ) |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
178 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
179 | |||
180 | if (! $post) { |
||
181 | return $this->redirect()->toUrl( |
||
182 | $this->frontendUrl()->fromRoute( |
||
183 | 'postvote', |
||
184 | array('id' => $this->game->getIdentifier()) |
||
185 | ) |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | if ($this->getRequest()->isPost()) { |
||
190 | $post = $this->getGameService()->confirmPost($this->game, $user); |
||
191 | |||
192 | if ($post) { |
||
193 | if (!($step = $this->game->nextStep('play'))) { |
||
194 | $step = 'result'; |
||
195 | } |
||
196 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
197 | 'postvote/'.$step, |
||
198 | array('id' => $this->game->getIdentifier()) |
||
199 | ); |
||
200 | |||
201 | return $this->redirect()->toUrl($redirectUrl); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | $viewModel = $this->buildView($this->game); |
||
206 | $viewModel->setVariables(array('post' => $post)); |
||
207 | |||
208 | return $viewModel; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * View the Post page |
||
213 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
214 | */ |
||
215 | public function postAction() |
||
216 | { |
||
217 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
218 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
219 | $voted = false; |
||
220 | $statusMail = false; |
||
221 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
222 | $to = ''; |
||
223 | $skinUrl = $this->getGameService()->getServiceManager()->get('ViewRenderer')->url( |
||
224 | 'frontend', |
||
225 | array(), |
||
226 | array('force_canonical' => true) |
||
227 | ); |
||
228 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
229 | if (isset($config['moderation']['email'])) { |
||
230 | $to = $config['moderation']['email']; |
||
231 | } |
||
232 | |||
233 | if (! $postId) { |
||
234 | return $this->notFoundAction(); |
||
235 | } |
||
236 | |||
237 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
238 | $post = $this->getGameService()->getPostVotePostMapper()->findById($postId); |
||
239 | |||
240 | View Code Duplication | if (! $post || $post->getStatus() === 9) { |
|
241 | return $this->redirect()->toUrl( |
||
242 | $this->frontendUrl()->fromRoute( |
||
243 | 'postvote', |
||
244 | array('id' => $this->game->getIdentifier()) |
||
245 | ) |
||
246 | ); |
||
247 | } |
||
248 | |||
249 | $formModeration = new Form(); |
||
250 | $formModeration->setAttribute('method', 'post'); |
||
251 | |||
252 | $formModeration->add(array( |
||
253 | 'name' => 'moderation', |
||
254 | 'attributes' => array( |
||
255 | 'type' => 'hidden', |
||
256 | 'value' => '1' |
||
257 | ), |
||
258 | )); |
||
259 | |||
260 | $form = new \PlaygroundGame\Form\Frontend\PostVoteVote( |
||
261 | $this->frontendUrl()->fromRoute( |
||
262 | 'postvote/post/captcha', |
||
263 | array( |
||
264 | 'id' => $this->game->getIdentifier(), |
||
265 | 'post' => $postId, |
||
266 | ) |
||
267 | ) |
||
268 | ); |
||
269 | |||
270 | if ($user) { |
||
271 | $form->remove('captcha'); |
||
272 | } |
||
273 | |||
274 | $alreadyVoted = ''; |
||
275 | $reportId = ''; |
||
276 | |||
277 | $request = $this->getRequest(); |
||
278 | if ($request->isPost()) { |
||
279 | $data = $request->getPost()->toArray(); |
||
280 | if (isset($data['moderation'])) { |
||
281 | $formModeration->setData($data); |
||
282 | if ($formModeration->isValid()) { |
||
283 | $from = $to; |
||
284 | $subject= 'Moderation Post and Vote'; |
||
285 | $result = $mailService->createHtmlMessage( |
||
286 | $from, |
||
287 | $to, |
||
288 | $subject, |
||
289 | 'playground-game/email/moderation', |
||
290 | array('data' => $data, 'skinUrl' => $skinUrl) |
||
291 | ); |
||
292 | $mailService->send($result); |
||
293 | if ($result) { |
||
294 | $statusMail = true; |
||
295 | $reportId = $data['reportid']; |
||
296 | } |
||
297 | } |
||
298 | } else { |
||
299 | $form->setData($request->getPost()); |
||
300 | if ($form->isValid()) { |
||
301 | if ($this->getGameService()->addVote($user, $this->getRequest()->getServer('REMOTE_ADDR'), $post)) { |
||
302 | $voted = true; |
||
303 | } else { |
||
304 | $alreadyVoted = 'Vous avez déjà voté!'; |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | } |
||
309 | |||
310 | $viewModel = $this->buildView($this->game); |
||
311 | $viewModel->setVariables( |
||
312 | array( |
||
313 | 'post' => $post, |
||
314 | 'voted' => $voted, |
||
315 | 'form' => $form, |
||
316 | 'formModeration' => $formModeration, |
||
317 | 'statusMail' => $statusMail, |
||
318 | 'alreadyVoted' => $alreadyVoted, |
||
319 | 'reportId' => $reportId, |
||
320 | ) |
||
321 | ); |
||
322 | |||
323 | return $viewModel; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * |
||
328 | */ |
||
329 | public function resultAction() |
||
330 | { |
||
331 | $statusMail = null; |
||
332 | |||
333 | // Has the user finished the game ? |
||
334 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $user); |
||
335 | |||
336 | if ($lastEntry === null) { |
||
337 | return $this->redirect()->toUrl( |
||
338 | $this->frontendUrl()->fromRoute( |
||
339 | 'postvote', |
||
340 | array('id' => $this->game->getIdentifier()) |
||
341 | ) |
||
342 | ); |
||
343 | } |
||
344 | |||
345 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
346 | $form->setAttribute('method', 'post'); |
||
347 | |||
348 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
349 | $data = $this->getRequest()->getPost()->toArray(); |
||
350 | $form->setData($data); |
||
351 | if ($form->isValid()) { |
||
352 | $result = $this->getGameService()->sendShareMail($data, $this->game, $user, $lastEntry); |
||
353 | if ($result) { |
||
354 | $statusMail = true; |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
360 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
361 | |||
362 | if (! $post) { |
||
363 | return $this->redirect()->toUrl( |
||
364 | $this->frontendUrl()->fromRoute( |
||
365 | 'postvote', |
||
366 | array('id' => $this->game->getIdentifier()) |
||
367 | ) |
||
368 | ); |
||
369 | } |
||
370 | |||
371 | $viewModel = $this->buildView($this->game); |
||
372 | |||
373 | $viewModel->setVariables(array( |
||
374 | 'statusMail' => $statusMail, |
||
375 | 'post' => $post, |
||
376 | 'form' => $form, |
||
377 | )); |
||
378 | |||
379 | return $viewModel; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
384 | * It's now possible to send a base64 image in this case the call is the form : |
||
385 | * this._ajax( |
||
386 | * { |
||
387 | * url: url.dataset.url, |
||
388 | * method: 'post', |
||
389 | * body: 'photo=' + image |
||
390 | * }, |
||
391 | * |
||
392 | * @return \Zend\Stdlib\ResponseInterface |
||
393 | */ |
||
394 | public function ajaxuploadAction() |
||
443 | |||
444 | public function ajaxdeleteAction() |
||
478 | |||
479 | public function listAction() |
||
593 | |||
594 | public function ajaxVoteAction() |
||
632 | |||
633 | public function captchaAction() |
||
657 | |||
658 | public function shareAction() |
||
659 | { |
||
660 | $statusMail = null; |
||
661 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
662 | |||
663 | // Has the user finished the game ? |
||
664 | $lastEntry = $this->getGameService()->findLastInactiveEntry($this->game, $user); |
||
665 | |||
666 | if ($lastEntry === null) { |
||
667 | return $this->redirect()->toUrl( |
||
668 | $this->frontendUrl()->fromRoute( |
||
669 | 'postvote', |
||
670 | array('id' => $this->game->getIdentifier()) |
||
671 | ) |
||
672 | ); |
||
673 | } |
||
674 | |||
675 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
676 | |||
677 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
678 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
679 | 'postvote/post', |
||
680 | array( |
||
681 | 'id' => $this->game->getIdentifier(), |
||
682 | 'post' => $post->getId(), |
||
683 | ), |
||
684 | array('force_canonical' => true) |
||
685 | ).'?key='.$secretKey; |
||
686 | // With core shortener helper |
||
687 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
688 | |||
689 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
690 | $form->setAttribute('method', 'post'); |
||
691 | |||
692 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
693 | $data = $this->getRequest()->getPost()->toArray(); |
||
694 | $form->setData($data); |
||
695 | if ($form->isValid()) { |
||
696 | $result = $this->getGameService()->sendShareMail($data, $this->game, $user, $lastEntry); |
||
697 | if ($result) { |
||
698 | $statusMail = true; |
||
699 | } |
||
700 | } |
||
701 | } |
||
702 | |||
703 | $viewModel = $this->buildView($this->game); |
||
704 | |||
705 | View Code Duplication | foreach ($post->getPostElements() as $element) { |
|
706 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
707 | '', |
||
708 | array(), |
||
709 | array('force_canonical' => true), |
||
710 | false |
||
711 | ) . $element->getValue(); |
||
712 | break; |
||
713 | } |
||
714 | |||
715 | $this->getViewHelper('HeadMeta')->setProperty('og:image', $fbShareImage); |
||
716 | $this->getViewHelper('HeadMeta')->setProperty('twitter:card', "photo"); |
||
717 | $this->getViewHelper('HeadMeta')->setProperty('twitter:site', "@playground"); |
||
718 | $this->getViewHelper('HeadMeta')->setProperty('twitter:title', $this->game->getTwShareMessage()); |
||
719 | $this->getViewHelper('HeadMeta')->setProperty('twitter:description', ""); |
||
720 | $this->getViewHelper('HeadMeta')->setProperty('twitter:image', $fbShareImage); |
||
721 | $this->getViewHelper('HeadMeta')->setProperty('twitter:url', $socialLinkUrl); |
||
722 | |||
723 | $viewModel->setVariables(array( |
||
724 | 'statusMail' => $statusMail, |
||
725 | 'form' => $form, |
||
726 | 'socialLinkUrl' => $socialLinkUrl, |
||
727 | 'post' => $post |
||
728 | )); |
||
729 | |||
730 | return $viewModel; |
||
731 | } |
||
732 | |||
733 | public function getGameService() |
||
741 | } |
||
742 |
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: