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:
Complex classes like MissionGame often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MissionGame, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class MissionGame extends EventProvider |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var missionMapper |
||
| 16 | */ |
||
| 17 | protected $missionMapper; |
||
| 18 | /** |
||
| 19 | * @var missionGameMapper |
||
| 20 | */ |
||
| 21 | protected $missionGameMapper; |
||
| 22 | /** |
||
| 23 | * @var missionGameConditionMapper |
||
| 24 | */ |
||
| 25 | protected $missionGameConditionMapper; |
||
| 26 | /** |
||
| 27 | * @var gameMapper |
||
| 28 | */ |
||
| 29 | protected $gameMapper; |
||
| 30 | /** |
||
| 31 | * @var options |
||
| 32 | */ |
||
| 33 | protected $options; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * |
||
| 37 | * @var ServiceManager |
||
| 38 | */ |
||
| 39 | protected $serviceLocator; |
||
| 40 | |||
| 41 | public function __construct(ServiceLocatorInterface $locator) |
||
| 45 | |||
| 46 | public function checkGames($dataGames) |
||
| 67 | |||
| 68 | |||
| 69 | public function checkGamesInMission($dataGames) |
||
| 91 | /** |
||
| 92 | * associate : Permet d'associer des jeux et des conditions à une mission |
||
| 93 | * @param array $data |
||
| 94 | * @param Mission $mission |
||
| 95 | * |
||
| 96 | * @return MissionGameEntity $missionGameEntity |
||
| 97 | */ |
||
| 98 | public function associate($data, $mission) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * clear : Permet de supprimer l'association des jeux et des conditions à une mission |
||
| 118 | * @param Mission $mission |
||
| 119 | */ |
||
| 120 | public function clear($mission) |
||
| 127 | |||
| 128 | |||
| 129 | View Code Duplication | public function checkCondition($game, $winner, $prediction, $entry) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
| 200 | * @param Mission $mission |
||
| 201 | * |
||
| 202 | * @return Collection de MissionGame $missionGames |
||
| 203 | */ |
||
| 204 | public function findMissionGameByMission($mission) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
| 211 | * |
||
| 212 | * @return Collection de MissionGame $missionGames |
||
| 213 | */ |
||
| 214 | public function findMissionGameByGame($game) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * findMissionGameConditionByMissionGame : Permet de recuperer les missionsGameCondition à partir d'une missionGame |
||
| 221 | * @param MissionGame $missionGame |
||
| 222 | * |
||
| 223 | * @return Collection de MissionGameCondition $missionGameConditions |
||
| 224 | */ |
||
| 225 | public function findMissionGameConditionByMissionGame($missionGame) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Retrieve service manager instance |
||
| 232 | * |
||
| 233 | * @return ServiceManager |
||
| 234 | */ |
||
| 235 | public function getServiceManager() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set service manager instance |
||
| 242 | * |
||
| 243 | * @return MissionGame |
||
| 244 | */ |
||
| 245 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * getMissionGameConditionMapper : retrieve missionGameCondition mapper instance |
||
| 254 | * |
||
| 255 | * @return Mapper/missionGameCondition $missionGameConditionMapper |
||
| 256 | */ |
||
| 257 | public function getMissionGameConditionMapper() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * getMissionGameMapper : retrieve missionGame mapper instance |
||
| 270 | * |
||
| 271 | * @return Mapper/MissionGameMapper $missionGameMapper |
||
| 272 | */ |
||
| 273 | View Code Duplication | public function getMissionGameMapper() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * getGameMapper : retrieve game mapper instance |
||
| 284 | * |
||
| 285 | * @return Mapper/GameMapper $gameMapper |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function getGameMapper() |
|
| 295 | } |
||
| 296 |
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.