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 |
||
| 12 | { |
||
| 13 | use EventManagerAwareTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var missionMapper |
||
| 17 | */ |
||
| 18 | protected $missionMapper; |
||
| 19 | /** |
||
| 20 | * @var missionGameMapper |
||
| 21 | */ |
||
| 22 | protected $missionGameMapper; |
||
| 23 | /** |
||
| 24 | * @var missionGameConditionMapper |
||
| 25 | */ |
||
| 26 | protected $missionGameConditionMapper; |
||
| 27 | /** |
||
| 28 | * @var gameMapper |
||
| 29 | */ |
||
| 30 | protected $gameMapper; |
||
| 31 | /** |
||
| 32 | * @var options |
||
| 33 | */ |
||
| 34 | protected $options; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * |
||
| 38 | * @var ServiceManager |
||
| 39 | */ |
||
| 40 | protected $serviceLocator; |
||
| 41 | |||
| 42 | public function __construct(ServiceLocatorInterface $locator) |
||
| 46 | |||
| 47 | public function checkGames($dataGames) |
||
| 68 | |||
| 69 | |||
| 70 | public function checkGamesInMission($dataGames) |
||
| 92 | /** |
||
| 93 | * associate : Permet d'associer des jeux et des conditions à une mission |
||
| 94 | * @param array $data |
||
| 95 | * @param Mission $mission |
||
| 96 | * |
||
| 97 | * @return MissionGameEntity $missionGameEntity |
||
| 98 | */ |
||
| 99 | public function associate($data, $mission) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * clear : Permet de supprimer l'association des jeux et des conditions à une mission |
||
| 119 | * @param Mission $mission |
||
| 120 | */ |
||
| 121 | public function clear($mission) |
||
| 128 | |||
| 129 | |||
| 130 | View Code Duplication | public function checkCondition($game, $winner, $prediction, $entry) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
| 201 | * @param Mission $mission |
||
| 202 | * |
||
| 203 | * @return Collection de MissionGame $missionGames |
||
| 204 | */ |
||
| 205 | public function findMissionGameByMission($mission) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
| 212 | * |
||
| 213 | * @return Collection de MissionGame $missionGames |
||
| 214 | */ |
||
| 215 | public function findMissionGameByGame($game) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * findMissionGameConditionByMissionGame : Permet de recuperer les missionsGameCondition à partir d'une missionGame |
||
| 222 | * @param MissionGame $missionGame |
||
| 223 | * |
||
| 224 | * @return Collection de MissionGameCondition $missionGameConditions |
||
| 225 | */ |
||
| 226 | public function findMissionGameConditionByMissionGame($missionGame) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Retrieve service manager instance |
||
| 233 | * |
||
| 234 | * @return ServiceManager |
||
| 235 | */ |
||
| 236 | public function getServiceManager() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set service manager instance |
||
| 243 | * |
||
| 244 | * @return MissionGame |
||
| 245 | */ |
||
| 246 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * getMissionGameConditionMapper : retrieve missionGameCondition mapper instance |
||
| 255 | * |
||
| 256 | * @return Mapper/missionGameCondition $missionGameConditionMapper |
||
| 257 | */ |
||
| 258 | public function getMissionGameConditionMapper() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * getMissionGameMapper : retrieve missionGame mapper instance |
||
| 271 | * |
||
| 272 | * @return Mapper/MissionGameMapper $missionGameMapper |
||
| 273 | */ |
||
| 274 | View Code Duplication | public function getMissionGameMapper() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * getGameMapper : retrieve game mapper instance |
||
| 285 | * |
||
| 286 | * @return Mapper/GameMapper $gameMapper |
||
| 287 | */ |
||
| 288 | View Code Duplication | public function getGameMapper() |
|
| 296 | } |
||
| 297 |
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.