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 |
||
| 12 | class PrizeCategoryController extends AbstractActionController |
||
| 13 | { |
||
| 14 | protected $options; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var \PlaygroundGame\Service\PrizeCategory |
||
| 18 | */ |
||
| 19 | protected $prizeCategoryService; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * |
||
| 23 | * @var ServiceManager |
||
| 24 | */ |
||
| 25 | protected $serviceLocator; |
||
| 26 | |||
| 27 | public function __construct(ServiceLocatorInterface $locator) |
||
| 31 | |||
| 32 | public function getServiceLocator() |
||
| 37 | |||
| 38 | View Code Duplication | public function listAction() |
|
| 54 | |||
| 55 | public function addAction() |
||
| 56 | { |
||
| 57 | $form = $this->getServiceLocator()->get('playgroundgame_prizecategory_form'); |
||
| 58 | $request = $this->getRequest(); |
||
| 59 | |||
| 60 | $category = new PrizeCategory(); |
||
| 61 | $form->bind($category); |
||
| 62 | |||
| 63 | View Code Duplication | if ($request->isPost()) { |
|
| 64 | $data = array_merge( |
||
| 65 | $request->getPost()->toArray(), |
||
| 66 | $request->getFiles()->toArray() |
||
| 67 | ); |
||
| 68 | $category = $this->getPrizeCategoryService()->create( |
||
| 69 | $data, |
||
| 70 | $category, |
||
| 71 | 'playgroundgame_prizecategory_form' |
||
| 72 | ); |
||
| 73 | if ($category) { |
||
| 74 | $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('la catégorie a été créée'); |
||
| 75 | |||
| 76 | return $this->redirect()->toRoute('admin/playgroundgame/prize-category-list'); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | $viewModel = new ViewModel(); |
||
| 81 | $viewModel->setTemplate('playground-game/prize-category/prize-category'); |
||
| 82 | |||
| 83 | return $viewModel->setVariables(array('form' => $form)); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function editAction() |
||
| 87 | { |
||
| 88 | $prizeCategoryId = $this->getEvent()->getRouteMatch()->getParam('prizeCategoryId'); |
||
| 89 | $category = $this->getPrizeCategoryService()->getPrizeCategoryMapper()->findById($prizeCategoryId); |
||
| 90 | |||
| 91 | $form = $this->getServiceLocator()->get('playgroundgame_prizecategory_form'); |
||
| 92 | |||
| 93 | $request = $this->getRequest(); |
||
| 94 | |||
| 95 | $form->bind($category); |
||
| 96 | |||
| 97 | View Code Duplication | if ($request->isPost()) { |
|
| 98 | $data = array_merge( |
||
| 99 | $request->getPost()->toArray(), |
||
| 100 | $request->getFiles()->toArray() |
||
| 101 | ); |
||
| 102 | $category = $this->getPrizeCategoryService()->edit( |
||
| 103 | $data, |
||
| 104 | $category, |
||
| 105 | 'playgroundgame_prizecategory_form' |
||
| 106 | ); |
||
| 107 | if ($category) { |
||
| 108 | $this->flashMessenger()->setNamespace('playgroundgame')->addMessage( |
||
| 109 | 'La catégorie a été mise à jour' |
||
| 110 | ); |
||
| 111 | |||
| 112 | return $this->redirect()->toRoute('admin/playgroundgame/prize-category-list'); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $viewModel = new ViewModel(); |
||
| 117 | $viewModel->setTemplate('playground-game/prize-category/prize-category'); |
||
| 118 | |||
| 119 | return $viewModel->setVariables(array('form' => $form)); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function setOptions(ModuleOptions $options) |
||
| 128 | |||
| 129 | public function getOptions() |
||
| 130 | { |
||
| 131 | if (!$this->options instanceof ModuleOptions) { |
||
| 132 | $this->setOptions($this->getServiceLocator()->get('playgroundgame_module_options')); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $this->options; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function getPrizeCategoryService() |
||
| 139 | { |
||
| 140 | if (!$this->prizeCategoryService) { |
||
| 141 | $this->prizeCategoryService = $this->getServiceLocator()->get( |
||
| 142 | 'playgroundgame_prizecategory_service' |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->prizeCategoryService; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function setPrizeCategoryService(PrizeCategoryService $prizeCategoryService) |
||
| 155 | } |
||
| 156 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..