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 GameController 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 GameController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class GameController extends AbstractActionController |
||
16 | { |
||
17 | /** |
||
18 | * @var gameService |
||
19 | */ |
||
20 | protected $gameService; |
||
21 | |||
22 | protected $prizeService; |
||
23 | |||
24 | protected $options; |
||
25 | |||
26 | /** |
||
27 | * Action called if matched action does not exist |
||
28 | * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy |
||
29 | * it has to be rendered in the controller. Hence the code below. |
||
30 | * |
||
31 | * This action is injected as a catchall action for each custom_games definition |
||
32 | * This way, when a custom_game is created, the 404 is it's responsability and the |
||
33 | * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml |
||
34 | * |
||
35 | * |
||
36 | * @return \Zend\Stdlib\ResponseInterface |
||
37 | */ |
||
38 | public function notFoundAction() |
||
59 | |||
60 | /** |
||
61 | * This action acts as a hub : Depending on the first step of the game, it will forward the action to this step |
||
62 | */ |
||
63 | public function homeAction() |
||
97 | |||
98 | /** |
||
99 | * Homepage of the game |
||
100 | */ |
||
101 | public function indexAction() |
||
144 | |||
145 | /** |
||
146 | * leaderboardAction |
||
147 | * |
||
148 | * @return ViewModel $viewModel |
||
149 | */ |
||
150 | public function leaderboardAction() |
||
177 | |||
178 | /** |
||
179 | * This action has been designed to be called by other controllers |
||
180 | * It gives the ability to display an information form and persist it in the game entry |
||
181 | * |
||
182 | * @return \Zend\View\Model\ViewModel |
||
183 | */ |
||
184 | public function registerAction() |
||
185 | { |
||
186 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
187 | $sg = $this->getGameService(); |
||
188 | |||
189 | $game = $sg->checkGame($identifier); |
||
190 | if (!$game || $game->isClosed()) { |
||
191 | return $this->notFoundAction(); |
||
192 | } |
||
193 | |||
194 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
195 | |||
196 | $form = $sg->createFormFromJson($game->getPlayerForm()->getForm(), 'playerForm'); |
||
197 | |||
198 | if ($this->getRequest()->isPost()) { |
||
199 | // POST Request: Process form |
||
200 | $data = array_merge_recursive( |
||
201 | $this->getRequest()->getPost()->toArray(), |
||
202 | $this->getRequest()->getFiles()->toArray() |
||
203 | ); |
||
204 | |||
205 | $form->setData($data); |
||
206 | |||
207 | if ($form->isValid()) { |
||
208 | $steps = $game->getStepsArray(); |
||
209 | $viewSteps = $game->getStepsViewsArray(); |
||
210 | $key = array_search($this->params('action'), $viewSteps); |
||
211 | if (!$key) { |
||
212 | $key = array_search($this->params('action'), $steps); |
||
213 | } |
||
214 | $keyplay = array_search('play', $steps); |
||
215 | |||
216 | // If register step before play, I don't have no entry yet. I have to create one |
||
217 | // If register after play step, I search for the last entry created by play step. |
||
218 | |||
219 | if ($key && $key < $keyplay) { |
||
220 | $entry = $sg->play($game, $user); |
||
221 | if (!$entry) { |
||
222 | // the user has already taken part of this game and the participation limit has been reached |
||
223 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
224 | |||
225 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute($game->getClassType().'/result', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
226 | } |
||
227 | } else { |
||
228 | // I'm looking for an entry without anonymousIdentifier (the active entry in fact). |
||
229 | $entry = $sg->findLastEntry($game, $user); |
||
230 | if ($sg->hasReachedPlayLimit($game, $user)) { |
||
231 | // the user has already taken part of this game and the participation limit has been reached |
||
232 | $this->flashMessenger()->addMessage('Vous avez déjà participé'); |
||
233 | |||
234 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute($game->getClassType().'/result', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | $sg->updateEntryPlayerForm($form->getData(), $game, $user, $entry); |
||
239 | |||
240 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute($game->getClassType() .'/' . $game->nextStep($this->params('action')), array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')), array('force_canonical' => true))); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | $viewModel = $this->buildView($game); |
||
245 | $viewModel->setVariables(array( |
||
246 | 'form' => $form, |
||
247 | 'title' => $game->getPlayerForm()->getTitle(), |
||
248 | 'description' => $game->getPlayerForm()->getDescription(), |
||
249 | )); |
||
250 | |||
251 | return $viewModel; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * This action takes care of the terms of the game |
||
256 | */ |
||
257 | View Code Duplication | public function termsAction() |
|
258 | { |
||
259 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
260 | $sg = $this->getGameService(); |
||
261 | |||
262 | $game = $sg->checkGame($identifier, false); |
||
263 | if (!$game) { |
||
264 | return $this->notFoundAction(); |
||
265 | } |
||
266 | |||
267 | $viewModel = $this->buildView($game); |
||
268 | |||
269 | return $viewModel; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * This action takes care of the conditions of the game |
||
274 | */ |
||
275 | View Code Duplication | public function conditionsAction() |
|
276 | { |
||
277 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
278 | $sg = $this->getGameService(); |
||
279 | |||
280 | $game = $sg->checkGame($identifier, false); |
||
281 | if (!$game) { |
||
282 | return $this->notFoundAction(); |
||
283 | } |
||
284 | |||
285 | $viewModel = $this->buildView($game); |
||
286 | |||
287 | return $viewModel; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * This action takes care of bounce page of the game |
||
292 | */ |
||
293 | public function bounceAction() |
||
294 | { |
||
295 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
296 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
297 | $sg = $this->getGameService(); |
||
298 | |||
299 | $game = $sg->checkGame($identifier); |
||
300 | if (!$game || $game->isClosed()) { |
||
301 | return $this->notFoundAction(); |
||
302 | } |
||
303 | |||
304 | $availableGames = $sg->getAvailableGames($user); |
||
305 | |||
306 | $rssUrl = ''; |
||
307 | $config = $sg->getServiceManager()->get('config'); |
||
308 | if (isset($config['rss']['url'])) { |
||
309 | $rssUrl = $config['rss']['url']; |
||
310 | } |
||
311 | |||
312 | $viewModel = $this->buildView($game); |
||
313 | $viewModel->setVariables(array( |
||
314 | 'rssUrl' => $rssUrl, |
||
315 | 'user' => $user, |
||
316 | 'availableGames' => $availableGames, |
||
317 | )); |
||
318 | |||
319 | return $viewModel; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * |
||
324 | * @param \PlaygroundGame\Entity\Game $game |
||
325 | * @param \PlaygroundUser\Entity\User $user |
||
326 | * @param string $channel |
||
327 | */ |
||
328 | public function checkFbRegistration($user, $game, $channel) |
||
382 | |||
383 | /** |
||
384 | * This method create the basic Game view |
||
385 | * @param \PlaygroundGame\Entity\Game $game |
||
386 | */ |
||
387 | public function buildView($game) |
||
388 | { |
||
389 | if ($this->getRequest()->isXmlHttpRequest()) { |
||
390 | $viewModel = new JsonModel(); |
||
391 | } else { |
||
392 | $viewModel = new ViewModel(); |
||
393 | |||
394 | if ($game) { |
||
395 | $this->addMetaTitle($game); |
||
396 | $this->addMetaBitly(); |
||
397 | $this->addGaEvent($game); |
||
398 | |||
399 | $this->customizeGameDesign($game); |
||
400 | |||
401 | // this is possible to create a specific game design in /design/frontend/default/custom. It will precede all others templates. |
||
402 | $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack'); |
||
403 | $l = $templatePathResolver->getPaths(); |
||
404 | $templatePathResolver->addPath($l[0].'custom/'.$game->getIdentifier()); |
||
405 | |||
406 | $view = $this->addAdditionalView($game); |
||
407 | if ($view && $view instanceof \Zend\View\Model\ViewModel) { |
||
408 | $viewModel->addChild($view, 'additional'); |
||
409 | } elseif ($view && $view instanceof \Zend\Http\PhpEnvironment\Response) { |
||
410 | return $view; |
||
411 | } |
||
412 | |||
413 | $this->layout()->setVariables( |
||
414 | array( |
||
415 | 'action' => $this->params('action'), |
||
416 | 'game' => $game, |
||
417 | 'flashMessages' => $this->flashMessenger()->getMessages(), |
||
418 | 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel') |
||
419 | ) |
||
420 | ); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | if ($game) { |
||
425 | $viewModel->setVariables($this->getShareData($game)); |
||
426 | $viewModel->setVariables(array('game' => $game)); |
||
427 | } |
||
428 | |||
429 | return $viewModel; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * @param \PlaygroundGame\Entity\Game $game |
||
434 | */ |
||
435 | public function addAdditionalView($game) |
||
436 | { |
||
437 | $view = false; |
||
438 | |||
439 | $actionName = $this->getEvent()->getRouteMatch()->getParam('action', 'not-found'); |
||
440 | $stepsViews = json_decode($game->getStepsViews(), true); |
||
441 | if ($stepsViews && isset($stepsViews[$actionName])) { |
||
442 | $beforeLayout = $this->layout()->getTemplate(); |
||
443 | $actionData = $stepsViews[$actionName]; |
||
444 | if (is_string($actionData)) { |
||
445 | $action = $actionData; |
||
446 | $controller = $this->getEvent()->getRouteMatch()->getParam('controller', 'playgroundgame_game'); |
||
447 | $view = $this->forward()->dispatch($controller, array('action' => $action, 'id' => $game->getIdentifier())); |
||
448 | } elseif (is_array($actionData) && count($actionData)>0) { |
||
449 | $action = key($actionData); |
||
450 | $controller = $actionData[$action]; |
||
451 | $view = $this->forward()->dispatch($controller, array('action' => $action, 'id' => $game->getIdentifier())); |
||
452 | } |
||
453 | // suite au forward, le template de layout a changé, je dois le rétablir... |
||
454 | $this->layout()->setTemplate($beforeLayout); |
||
455 | } |
||
456 | |||
457 | return $view; |
||
458 | } |
||
459 | |||
460 | public function addMetaBitly() |
||
461 | { |
||
462 | $bitlyclient = $this->getOptions()->getBitlyUrl(); |
||
463 | $bitlyuser = $this->getOptions()->getBitlyUsername(); |
||
464 | $bitlykey = $this->getOptions()->getBitlyApiKey(); |
||
465 | |||
466 | $this->getViewHelper('HeadMeta')->setProperty('bt:client', $bitlyclient); |
||
467 | $this->getViewHelper('HeadMeta')->setProperty('bt:user', $bitlyuser); |
||
468 | $this->getViewHelper('HeadMeta')->setProperty('bt:key', $bitlykey); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @param \PlaygroundGame\Entity\Game $game |
||
473 | */ |
||
474 | public function addGaEvent($game) |
||
475 | { |
||
476 | // Google Analytics event |
||
477 | $ga = $this->getServiceLocator()->get('google-analytics'); |
||
478 | $event = new \PlaygroundCore\Analytics\Event($game->getClassType(), $this->params('action')); |
||
479 | $event->setLabel($game->getTitle()); |
||
480 | $ga->addEvent($event); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param \PlaygroundGame\Entity\Game $game |
||
485 | */ |
||
486 | public function addMetaTitle($game) |
||
507 | |||
508 | /** |
||
509 | * @param \PlaygroundGame\Entity\Game $game |
||
510 | */ |
||
511 | public function customizeGameDesign($game) |
||
524 | |||
525 | /** |
||
526 | * @param \PlaygroundGame\Entity\Game $game |
||
527 | */ |
||
528 | public function getShareData($game) |
||
529 | { |
||
530 | $fo = $this->getServiceLocator()->get('facebook-opengraph'); |
||
531 | // I change the fbappid if i'm in fb |
||
532 | if ($this->getEvent()->getRouteMatch()->getParam('channel') === 'facebook') { |
||
533 | $fo->setId($game->getFbAppId()); |
||
534 | } |
||
535 | |||
536 | // If I want to add a share block in my view |
||
537 | View Code Duplication | if ($game->getFbShareMessage()) { |
|
538 | $fbShareMessage = $game->getFbShareMessage(); |
||
539 | } else { |
||
540 | $fbShareMessage = str_replace('__placeholder__', $game->getTitle(), $this->getOptions()->getDefaultShareMessage()); |
||
541 | } |
||
542 | |||
543 | if ($game->getFbShareImage()) { |
||
544 | $fbShareImage = $this->frontendUrl()->fromRoute('', array('channel' => ''), array('force_canonical' => true), false) . $game->getFbShareImage(); |
||
545 | } else { |
||
546 | $fbShareImage = $this->frontendUrl()->fromRoute('', array('channel' => ''), array('force_canonical' => true), false) . $game->getMainImage(); |
||
547 | } |
||
548 | |||
549 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
550 | |||
551 | // Without bit.ly shortener |
||
552 | $socialLinkUrl = $this->frontendUrl()->fromRoute($game->getClassType(), array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')), array('force_canonical' => true)); |
||
553 | // With core shortener helper |
||
554 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
555 | |||
556 | // FB Requests only work when it's a FB app |
||
557 | if ($game->getFbRequestMessage()) { |
||
558 | $fbRequestMessage = urlencode($game->getFbRequestMessage()); |
||
559 | } else { |
||
560 | $fbRequestMessage = str_replace('__placeholder__', $game->getTitle(), $this->getOptions()->getDefaultShareMessage()); |
||
561 | } |
||
562 | |||
563 | View Code Duplication | if ($game->getTwShareMessage()) { |
|
564 | $twShareMessage = $game->getTwShareMessage() . $socialLinkUrl; |
||
565 | } else { |
||
566 | $twShareMessage = str_replace('__placeholder__', $game->getTitle(), $this->getOptions()->getDefaultShareMessage()) . $socialLinkUrl; |
||
567 | } |
||
568 | |||
569 | $ogTitle = new \PlaygroundCore\Opengraph\Tag('og:title', $fbShareMessage); |
||
570 | $ogImage = new \PlaygroundCore\Opengraph\Tag('og:image', $fbShareImage); |
||
571 | |||
572 | $fo->addTag($ogTitle); |
||
573 | $fo->addTag($ogImage); |
||
574 | |||
575 | $data = array( |
||
576 | 'socialLinkUrl' => $socialLinkUrl, |
||
577 | 'secretKey' => $secretKey, |
||
578 | 'fbShareMessage' => $fbShareMessage, |
||
579 | 'fbShareImage' => $fbShareImage, |
||
580 | 'fbRequestMessage' => $fbRequestMessage, |
||
581 | 'twShareMessage' => $twShareMessage, |
||
582 | ); |
||
583 | |||
584 | return $data; |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * This action displays the Prizes page associated to the game |
||
589 | */ |
||
590 | public function prizesAction() |
||
608 | |||
609 | /** |
||
610 | * This action displays a specific Prize page among those associated to the game |
||
611 | */ |
||
612 | public function prizeAction() |
||
636 | |||
637 | public function gameslistAction() |
||
689 | |||
690 | View Code Duplication | public function fangateAction() |
|
703 | |||
704 | public function shareAction() |
||
705 | { |
||
706 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
707 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
708 | |||
709 | $statusMail = null; |
||
710 | |||
711 | if (!$identifier) { |
||
712 | return $this->notFoundAction(); |
||
713 | } |
||
714 | |||
715 | $gameMapper = $this->getGameService()->getGameMapper(); |
||
716 | $game = $gameMapper->findByIdentifier($identifier); |
||
717 | |||
718 | if (!$game || $game->isClosed()) { |
||
719 | return $this->notFoundAction(); |
||
720 | } |
||
721 | |||
722 | // Has the user finished the game ? |
||
723 | $lastEntry = $this->getGameService()->findLastInactiveEntry($game, $user); |
||
724 | |||
725 | View Code Duplication | if ($lastEntry === null) { |
|
726 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('postvote', array('id' => $identifier, 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
727 | } |
||
728 | |||
729 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
730 | $redirect = urlencode($this->frontendUrl()->fromRoute('postvote/result', array('id' => $game->getIdentifier(), 'channel' => $channel))); |
||
731 | return $this->redirect()->toUrl($this->frontendUrl()->fromRoute('zfcuser/register', array('channel' => $channel)) . '?redirect='.$redirect); |
||
732 | } |
||
733 | |||
734 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
735 | $form->setAttribute('method', 'post'); |
||
736 | |||
737 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
738 | $data = $this->getRequest()->getPost()->toArray(); |
||
739 | $form->setData($data); |
||
740 | if ($form->isValid()) { |
||
741 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
742 | if ($result) { |
||
743 | $statusMail = true; |
||
744 | } |
||
745 | } |
||
746 | } |
||
747 | |||
748 | // buildView must be before sendMail because it adds the game template path to the templateStack |
||
749 | $viewModel = $this->buildView($game); |
||
750 | |||
751 | $this->getGameService()->sendMail($game, $user, $lastEntry); |
||
752 | |||
753 | $viewModel->setVariables(array( |
||
754 | 'statusMail' => $statusMail, |
||
755 | 'form' => $form, |
||
756 | )); |
||
757 | |||
758 | return $viewModel; |
||
759 | } |
||
760 | |||
761 | View Code Duplication | public function fbshareAction() |
|
786 | |||
787 | public function fbrequestAction() |
||
813 | |||
814 | public function tweetAction() |
||
837 | |||
838 | View Code Duplication | public function googleAction() |
|
863 | |||
864 | public function optinAction() |
||
886 | |||
887 | public function loginAction() |
||
888 | { |
||
889 | $request = $this->getRequest(); |
||
890 | $form = $this->getServiceLocator()->get('zfcuser_login_form'); |
||
891 | |||
892 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
893 | |||
894 | $sg = $this->getGameService(); |
||
895 | |||
896 | $game = $sg->checkGame($identifier, false); |
||
897 | if (!$game) { |
||
898 | return $this->notFoundAction(); |
||
899 | } |
||
900 | |||
901 | if ($request->isPost()) { |
||
902 | $form->setData($request->getPost()); |
||
903 | |||
904 | if (!$form->isValid()) { |
||
905 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage('Authentication failed. Please try again.'); |
||
906 | |||
907 | return $this->redirect()->toUrl($this->url()->fromRoute('frontend/' . $game->getClassType() . '/login', array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel'))).($redirect ? '?redirect='.$redirect : '')); |
||
908 | } |
||
909 | |||
910 | // clear adapters |
||
911 | $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters(); |
||
912 | $this->zfcUserAuthentication()->getAuthService()->clearIdentity(); |
||
913 | |||
914 | $logged = $this->forward()->dispatch('playgrounduser_user', array('action' => 'ajaxauthenticate')); |
||
915 | |||
916 | View Code Duplication | if ($logged) { |
|
917 | return $this->redirect()->toUrl($this->url()->fromRoute('frontend/' . $game->getClassType() . '/' . $game->nextStep('index'), array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
918 | } else { |
||
919 | $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage('Authentication failed. Please try again.'); |
||
920 | return $this->redirect()->toUrl($this->url()->fromRoute('frontend/' . $game->getClassType() . '/login', array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
921 | } |
||
922 | } |
||
923 | |||
924 | $form->setAttribute('action', $this->url()->fromRoute('frontend/'.$game->getClassType().'/login', array('id' => $game->getIdentifier(), 'channel' => $this->getEvent()->getRouteMatch()->getParam('channel')))); |
||
925 | $viewModel = $this->buildView($game); |
||
926 | $viewModel->setVariables(array( |
||
927 | 'form' => $form, |
||
928 | )); |
||
929 | return $viewModel; |
||
930 | } |
||
931 | |||
932 | public function userregisterAction() |
||
1079 | |||
1080 | /** |
||
1081 | * return ajax response in json format |
||
1082 | * |
||
1083 | * @param array $data |
||
1084 | * @return \Zend\View\Model\JsonModel |
||
1085 | */ |
||
1086 | View Code Duplication | protected function successJson($data = null) |
|
1094 | |||
1095 | /** |
||
1096 | * return ajax response in json format |
||
1097 | * |
||
1098 | * @param string $message |
||
1099 | * @return \Zend\View\Model\JsonModel |
||
1100 | */ |
||
1101 | View Code Duplication | protected function errorJson($message = null) |
|
1109 | |||
1110 | /** |
||
1111 | * @param string $helperName |
||
1112 | */ |
||
1113 | protected function getViewHelper($helperName) |
||
1117 | |||
1118 | public function getGameService() |
||
1126 | |||
1127 | public function setGameService(GameService $gameService) |
||
1128 | { |
||
1129 | $this->gameService = $gameService; |
||
1130 | |||
1131 | return $this; |
||
1132 | } |
||
1133 | |||
1134 | public function getPrizeService() |
||
1135 | { |
||
1136 | if (!$this->prizeService) { |
||
1137 | $this->prizeService = $this->getServiceLocator()->get('playgroundgame_prize_service'); |
||
1138 | } |
||
1139 | |||
1140 | return $this->prizeService; |
||
1141 | } |
||
1142 | |||
1143 | public function setPrizeService(PrizeService $prizeService) |
||
1144 | { |
||
1145 | $this->prizeService = $prizeService; |
||
1146 | |||
1147 | return $this; |
||
1148 | } |
||
1149 | |||
1150 | public function getOptions() |
||
1158 | |||
1159 | public function setOptions($options) |
||
1165 | } |
||
1166 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: