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:
| 1 | <?php |
||
| 14 | class TradingCardController extends GameController |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var \PlaygroundGame\Service\Game |
||
| 18 | */ |
||
| 19 | protected $adminGameService; |
||
| 20 | |||
| 21 | View Code Duplication | public function createTradingcardAction() |
|
| 65 | |||
| 66 | public function editTradingcardAction() |
||
| 75 | |||
| 76 | View Code Duplication | public function listModelAction() |
|
| 97 | |||
| 98 | View Code Duplication | public function addModelAction() |
|
| 99 | { |
||
| 100 | $this->checkGame(); |
||
| 101 | |||
| 102 | $viewModel = new ViewModel(); |
||
| 103 | $viewModel->setTemplate('playground-game/trading-card/model'); |
||
| 104 | |||
| 105 | $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form'); |
||
| 106 | $form->get('submit')->setAttribute('label', 'Add'); |
||
| 107 | |||
| 108 | // $form->get('availability')->setOptions(array( |
||
| 109 | // 'format' => 'Y-m-d H:i:s' |
||
| 110 | // )); |
||
| 111 | |||
| 112 | $form->setAttribute( |
||
| 113 | 'action', |
||
| 114 | $this->adminUrl()->fromRoute( |
||
| 115 | 'playgroundgame/tradingcard-model-add', |
||
| 116 | array('gameId' => $this->game->getId()) |
||
| 117 | ) |
||
| 118 | ); |
||
| 119 | $form->setAttribute('method', 'post'); |
||
| 120 | $form->get('trading_card_id')->setAttribute('value', $this->game->getId()); |
||
| 121 | $model = new TradingCardModel(); |
||
| 122 | $form->bind($model); |
||
| 123 | |||
| 124 | if ($this->getRequest()->isPost()) { |
||
| 125 | $data = array_merge( |
||
| 126 | $this->getRequest()->getPost()->toArray(), |
||
| 127 | $this->getRequest()->getFiles()->toArray() |
||
| 128 | ); |
||
| 129 | |||
| 130 | $model = $this->getAdminGameService()->updateModel($data, $model); |
||
| 131 | if ($model) { |
||
| 132 | // Redirect to list of games |
||
| 133 | $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been created'); |
||
| 134 | return $this->redirect()->toUrl( |
||
| 135 | $this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $this->game->getId())) |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | return $viewModel->setVariables( |
||
| 140 | array( |
||
| 141 | 'form' => $form, |
||
| 142 | 'game' => $this->game, |
||
| 143 | 'model_id' => 0, |
||
| 144 | 'title' => 'Add model', |
||
| 145 | ) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | View Code Duplication | public function editModelAction() |
|
| 150 | { |
||
| 151 | $this->checkGame(); |
||
| 152 | |||
| 153 | $viewModel = new ViewModel(); |
||
| 154 | $viewModel->setTemplate('playground-game/trading-card/model'); |
||
| 155 | $service = $this->getAdminGameService(); |
||
| 156 | |||
| 157 | $modelId = $this->getEvent()->getRouteMatch()->getParam('modelId'); |
||
| 158 | $model = $service->getTradingCardModelMapper()->findById($modelId); |
||
| 159 | |||
| 160 | $form = $this->getServiceLocator()->get('playgroundgame_tradingcardmodel_form'); |
||
| 161 | $form->remove('models_file'); |
||
| 162 | |||
| 163 | $form->get('submit')->setAttribute('label', 'Edit'); |
||
| 164 | $form->setAttribute('action', ''); |
||
| 165 | |||
| 166 | $form->get('trading_card_id')->setAttribute('value', $this->game->getId()); |
||
| 167 | |||
| 168 | $form->bind($model); |
||
| 169 | |||
| 170 | if ($this->getRequest()->isPost()) { |
||
| 171 | $data = array_merge( |
||
| 172 | $this->getRequest()->getPost()->toArray(), |
||
| 173 | $this->getRequest()->getFiles()->toArray() |
||
| 174 | ); |
||
| 175 | $model = $service->updateModel($data, $model); |
||
| 176 | |||
| 177 | if ($model) { |
||
| 178 | // Redirect to list of games |
||
| 179 | $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The model has been edited'); |
||
| 180 | return $this->redirect()->toUrl( |
||
| 181 | $this->adminUrl()->fromRoute('playgroundgame/tradingcard-model-list', array('gameId' => $this->game->getId())) |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | return $viewModel->setVariables( |
||
| 186 | array( |
||
| 187 | 'form' => $form, |
||
| 188 | 'game' => $this->game, |
||
| 189 | 'model_id' => $modelId, |
||
| 190 | 'title' => 'Edit model', |
||
| 191 | ) |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * This function helps the admin to position UGC image on a card model |
||
| 197 | */ |
||
| 198 | public function cooAction() |
||
| 249 | |||
| 250 | View Code Duplication | public function removeModelAction() |
|
| 271 | |||
| 272 | public function getAdminGameService() |
||
| 280 | |||
| 281 | public function setAdminGameService(AdminGameService $adminGameService) |
||
| 287 | } |
||
| 288 |
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.