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 |
||
9 | class PostVoteController extends GameController |
||
10 | { |
||
11 | /** |
||
12 | * @var GameService |
||
13 | */ |
||
14 | protected $adminGameService; |
||
15 | |||
16 | View Code Duplication | public function formAction() |
|
|
|||
17 | { |
||
18 | $service = $this->getAdminGameService(); |
||
19 | $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId'); |
||
20 | if (!$gameId) { |
||
21 | return $this->redirect()->toRoute('admin/playgroundgame/list'); |
||
22 | } |
||
23 | $game = $service->getGameMapper()->findById($gameId); |
||
24 | $form = $service->getPostVoteFormMapper()->findByGame($game); |
||
25 | |||
26 | // I use the wonderful Form Generator to create the Post & Vote form |
||
27 | $this->forward()->dispatch( |
||
28 | 'PlaygroundCore\Controller\Formgen', |
||
29 | array( |
||
30 | 'controller' => 'PlaygroundCore\Controller\Formgen', |
||
31 | 'action' => 'create' |
||
32 | ) |
||
33 | ); |
||
34 | |||
35 | if ($this->getRequest()->isPost()) { |
||
36 | $data = $this->getRequest()->getPost()->toArray(); |
||
37 | $form = $service->createForm($data, $game, $form); |
||
38 | if ($form) { |
||
39 | $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The form was created'); |
||
40 | } |
||
41 | } |
||
42 | $formTemplate=''; |
||
43 | if ($form) { |
||
44 | $formTemplate = $form->getFormTemplate(); |
||
45 | } |
||
46 | |||
47 | return array( |
||
48 | 'form' => $form, |
||
49 | 'formTemplate' => $formTemplate, |
||
50 | 'gameId' => $gameId, |
||
51 | 'game' => $game, |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | public function createPostVoteAction() |
|
56 | { |
||
57 | $service = $this->getAdminGameService(); |
||
58 | $viewModel = new ViewModel(); |
||
59 | $viewModel->setTemplate('playground-game/post-vote/postvote'); |
||
60 | |||
61 | $gameForm = new ViewModel(); |
||
62 | $gameForm->setTemplate('playground-game/game/game-form'); |
||
63 | |||
64 | $postVote = new PostVote(); |
||
65 | |||
66 | $form = $this->getServiceLocator()->get('playgroundgame_postvote_form'); |
||
67 | $form->bind($postVote); |
||
68 | $form->get('submit')->setAttribute('label', 'Add'); |
||
69 | $form->setAttribute( |
||
70 | 'action', |
||
71 | $this->url()->fromRoute( |
||
72 | 'admin/playgroundgame/create-postvote', |
||
73 | array('gameId' => 0) |
||
74 | ) |
||
75 | ); |
||
76 | $form->setAttribute('method', 'post'); |
||
77 | |||
78 | $request = $this->getRequest(); |
||
79 | if ($request->isPost()) { |
||
80 | $data = array_replace_recursive( |
||
81 | $this->getRequest()->getPost()->toArray(), |
||
82 | $this->getRequest()->getFiles()->toArray() |
||
83 | ); |
||
84 | if (empty($data['prizes'])) { |
||
85 | $data['prizes'] = array(); |
||
86 | } |
||
87 | $game = $service->create($data, $postVote, 'playgroundgame_postvote_form'); |
||
88 | if ($game) { |
||
89 | $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created'); |
||
90 | |||
91 | return $this->redirect()->toRoute('admin/playgroundgame/list'); |
||
92 | } |
||
93 | } |
||
94 | $gameForm->setVariables(array('form' => $form, 'game' => $postVote)); |
||
95 | $viewModel->addChild($gameForm, 'game_form'); |
||
96 | |||
97 | return $viewModel->setVariables(array('form' => $form, 'title' => 'Create Post & Vote')); |
||
98 | } |
||
99 | |||
100 | View Code Duplication | public function editPostVoteAction() |
|
163 | |||
164 | View Code Duplication | public function modListAction() |
|
179 | |||
180 | public function moderationEditAction() |
||
220 | |||
221 | public function pushAction() |
||
247 | |||
248 | public function getAdminGameService() |
||
249 | { |
||
256 | |||
257 | public function setAdminGameService(AdminGameService $adminGameService) |
||
263 | } |
||
264 |
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.