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 Prize extends EventProvider |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var prizeMapper |
||
| 16 | */ |
||
| 17 | protected $prizeMapper; |
||
| 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, $prize, $formClass) |
||
| 49 | { |
||
| 50 | $form = $this->serviceLocator->get($formClass); |
||
| 51 | $form->bind($prize); |
||
| 52 | |||
| 53 | // If the identifier has not been set, I use the title to create one. |
||
| 54 | View Code Duplication | if (empty($data['identifier']) && !empty($data['title'])) { |
|
| 55 | $data['identifier'] = $data['title']; |
||
| 56 | } |
||
| 57 | |||
| 58 | $form->setData($data); |
||
| 59 | |||
| 60 | if (!$form->isValid()) { |
||
| 61 | return false; |
||
| 62 | } |
||
| 63 | |||
| 64 | $prize = $this->getPrizeMapper()->insert($prize); |
||
| 65 | |||
| 66 | return $prize; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @param array $data |
||
| 72 | * @param string $formClass |
||
| 73 | * @return \PlaygroundGame\Entity\Game |
||
| 74 | */ |
||
| 75 | public function edit(array $data, $prize, $formClass) |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * getPrizeMapper |
||
| 106 | * |
||
| 107 | * @return PrizeMapper |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function getPrizeMapper() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * setPrizeMapper |
||
| 120 | * |
||
| 121 | * @param PrizeMapper $prizeMapper |
||
| 122 | * @return Prize |
||
| 123 | */ |
||
| 124 | public function setPrizeMapper(PrizeMapper $prizeMapper) |
||
| 130 | |||
| 131 | public function setOptions(ModuleOptions $options) |
||
| 137 | |||
| 138 | public function getOptions() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Retrieve service manager instance |
||
| 149 | * |
||
| 150 | * @return ServiceManager |
||
| 151 | */ |
||
| 152 | public function getServiceManager() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set service manager instance |
||
| 159 | * |
||
| 160 | * @param ServiceManager $serviceManager |
||
| 161 | * @return Prize |
||
| 162 | */ |
||
| 163 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 169 | } |
||
| 170 |
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.