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 PrizeCategory extends EventProvider |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var prizeCategoryMapper |
||
| 16 | */ |
||
| 17 | protected $prizeCategoryMapper; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var ServiceManager |
||
| 21 | */ |
||
| 22 | protected $serviceManager; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var UserServiceOptionsInterface |
||
| 26 | */ |
||
| 27 | protected $options; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @var ServiceManager |
||
| 32 | */ |
||
| 33 | protected $serviceLocator; |
||
| 34 | |||
| 35 | public function __construct(ServiceLocatorInterface $locator) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * This service is ready for all types of games |
||
| 43 | * |
||
| 44 | * @param array $data |
||
| 45 | * @param string $formClass |
||
| 46 | * @return \PlaygroundGame\Entity\Game |
||
| 47 | */ |
||
| 48 | public function create(array $data, $prizeCategory, $formClass) |
||
| 49 | { |
||
| 50 | $form = $this->serviceLocator->get($formClass); |
||
| 51 | $form->bind($prizeCategory); |
||
| 52 | |||
| 53 | $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
||
| 54 | $media_url = $this->getOptions()->getMediaUrl() . '/'; |
||
| 55 | |||
| 56 | $form->setData($data); |
||
| 57 | |||
| 58 | if (!$form->isValid()) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | $prizeCategory = $this->getPrizeCategoryMapper()->insert($prizeCategory); |
||
| 63 | |||
| 64 | View Code Duplication | if (!empty($data['upload_picto']['tmp_name'])) { |
|
| 65 | ErrorHandler::start(); |
||
| 66 | move_uploaded_file( |
||
| 67 | $data['upload_picto']['tmp_name'], |
||
| 68 | $path . $prizeCategory->getId() . "-" . $data['upload_picto']['name'] |
||
| 69 | ); |
||
| 70 | $prizeCategory->setPicto($media_url . $prizeCategory->getId() . "-" . $data['upload_picto']['name']); |
||
| 71 | ErrorHandler::stop(true); |
||
| 72 | } |
||
| 73 | |||
| 74 | $prizeCategory = $this->getPrizeCategoryMapper()->update($prizeCategory); |
||
| 75 | |||
| 76 | return $prizeCategory; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @param array $data |
||
| 82 | * @param string $formClass |
||
| 83 | * @return \PlaygroundGame\Entity\Game |
||
| 84 | */ |
||
| 85 | public function edit(array $data, $prizeCategory, $formClass) |
||
| 86 | { |
||
| 87 | $form = $this->serviceLocator->get($formClass); |
||
| 88 | $form->bind($prizeCategory); |
||
| 89 | |||
| 90 | $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
||
| 91 | $media_url = $this->getOptions()->getMediaUrl() . '/'; |
||
| 92 | |||
| 93 | $form->setData($data); |
||
| 94 | |||
| 95 | if (!$form->isValid()) { |
||
| 96 | return false; |
||
| 97 | } |
||
| 98 | |||
| 99 | View Code Duplication | if (!empty($data['upload_picto']['tmp_name'])) { |
|
| 100 | ErrorHandler::start(); |
||
| 101 | move_uploaded_file( |
||
| 102 | $data['upload_picto']['tmp_name'], |
||
| 103 | $path . $prizeCategory->getId() . "-" . $data['upload_picto']['name'] |
||
| 104 | ); |
||
| 105 | $prizeCategory->setPicto($media_url . $prizeCategory->getId() . "-" . $data['upload_picto']['name']); |
||
| 106 | ErrorHandler::stop(true); |
||
| 107 | } |
||
| 108 | |||
| 109 | $prizeCategory = $this->getPrizeCategoryMapper()->update($prizeCategory); |
||
| 110 | |||
| 111 | return $prizeCategory; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function getActivePrizeCategories() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * getPrizeCategoryMapper |
||
| 126 | * |
||
| 127 | * @return PrizeCategoryMapper |
||
| 128 | */ |
||
| 129 | public function getPrizeCategoryMapper() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * setPrizeCategoryMapper |
||
| 140 | * |
||
| 141 | * @param PrizeCategoryMapper $prizeCategoryMapper |
||
| 142 | * @return PrizeCategory |
||
| 143 | */ |
||
| 144 | public function setPrizeCategoryMapper(PrizeCategoryMapper $prizeCategoryMapper) |
||
| 150 | |||
| 151 | public function setOptions(ModuleOptions $options) |
||
| 157 | |||
| 158 | public function getOptions() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Retrieve service manager instance |
||
| 169 | * |
||
| 170 | * @return ServiceManager |
||
| 171 | */ |
||
| 172 | public function getServiceManager() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set service manager instance |
||
| 179 | * |
||
| 180 | * @param ServiceManager $serviceManager |
||
| 181 | * @return PrizeCategory |
||
| 182 | */ |
||
| 183 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 189 | } |
||
| 190 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.