| Conditions | 8 |
| Paths | 36 |
| Total Lines | 59 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 65 | public function editLotteryAction() |
||
| 66 | { |
||
| 67 | $this->checkGame(); |
||
| 68 | |||
| 69 | $viewModel = new ViewModel(); |
||
| 70 | $viewModel->setTemplate('playground-game/lottery/lottery'); |
||
| 71 | |||
| 72 | $gameForm = new ViewModel(); |
||
| 73 | $gameForm->setTemplate('playground-game/game/game-form'); |
||
| 74 | |||
| 75 | $form = $this->getServiceLocator()->get('playgroundgame_lottery_form'); |
||
| 76 | $form->setAttribute( |
||
| 77 | 'action', |
||
| 78 | $this->url()->fromRoute( |
||
| 79 | 'admin/playgroundgame/edit-lottery', |
||
| 80 | array('gameId' => $this->game->getId()) |
||
| 81 | ) |
||
| 82 | ); |
||
| 83 | $form->setAttribute('method', 'post'); |
||
| 84 | if ($this->game->getFbAppId()) { |
||
| 85 | $appIds = $form->get('fbAppId')->getOption('value_options'); |
||
| 86 | $appIds[$this->game->getFbAppId()] = $this->game->getFbAppId(); |
||
| 87 | $form->get('fbAppId')->setAttribute('options', $appIds); |
||
| 88 | } |
||
| 89 | |||
| 90 | $gameOptions = $this->getAdminGameService()->getOptions(); |
||
| 91 | $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css'; |
||
| 92 | if (is_file($gameStylesheet)) { |
||
| 93 | $values = $form->get('stylesheet')->getValueOptions(); |
||
| 94 | $values[$gameStylesheet] = 'Style personnalisé de ce jeu'; |
||
| 95 | |||
| 96 | $form->get('stylesheet')->setAttribute('options', $values); |
||
| 97 | } |
||
| 98 | |||
| 99 | $form->bind($this->game); |
||
| 100 | |||
| 101 | if ($this->getRequest()->isPost()) { |
||
| 102 | $data = array_replace_recursive( |
||
| 103 | $this->getRequest()->getPost()->toArray(), |
||
| 104 | $this->getRequest()->getFiles()->toArray() |
||
| 105 | ); |
||
| 106 | if (empty($data['prizes'])) { |
||
| 107 | $data['prizes'] = array(); |
||
| 108 | } |
||
| 109 | if (isset($data['drawDate']) && $data['drawDate']) { |
||
| 110 | $data['drawDate'] = \DateTime::createFromFormat('d/m/Y', $data['drawDate']); |
||
| 111 | } |
||
| 112 | $result = $service->edit($data, $this->game, 'playgroundgame_lottery_form'); |
||
| 113 | |||
| 114 | if ($result) { |
||
| 115 | return $this->redirect()->toRoute('admin/playgroundgame/list'); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $gameForm->setVariables(array('form' => $form, 'game' => $this->game)); |
||
| 120 | $viewModel->addChild($gameForm, 'game_form'); |
||
| 121 | |||
| 122 | return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit lottery')); |
||
| 123 | } |
||
| 124 | |||
| 141 |
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: