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 implements ServiceManagerAwareInterface |
||
| 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 | public function checkGames($dataGames) |
||
| 58 | |||
| 59 | |||
| 60 | public function checkGamesInMission($dataGames) |
||
| 83 | /** |
||
| 84 | * associate : Permet d'associer des jeux et des conditions à une mission |
||
| 85 | * @param array $data |
||
| 86 | * @param Mission $mission |
||
| 87 | * |
||
| 88 | * @return MissionGameEntity $missionGameEntity |
||
| 89 | */ |
||
| 90 | public function associate($data, $mission) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * clear : Permet de supprimer l'association des jeux et des conditions à une mission |
||
| 110 | * @param Mission $mission |
||
| 111 | */ |
||
| 112 | public function clear($mission) |
||
| 119 | |||
| 120 | |||
| 121 | View Code Duplication | public function checkCondition($game, $winner, $prediction, $entry) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
| 190 | * @param Mission $mission |
||
| 191 | * |
||
| 192 | * @return Collection de MissionGame $missionGames |
||
| 193 | */ |
||
| 194 | public function findMissionGameByMission($mission){ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
| 200 | * |
||
| 201 | * @return Collection de MissionGame $missionGames |
||
| 202 | */ |
||
| 203 | public function findMissionGameByGame($game){ |
||
| 206 | |||
| 207 | /** |
||
| 208 | * findMissionGameConditionByMissionGame : Permet de recuperer les missionsGameCondition à partir d'une missionGame |
||
| 209 | * @param MissionGame $missionGame |
||
| 210 | * |
||
| 211 | * @return Collection de MissionGameCondition $missionGameConditions |
||
| 212 | */ |
||
| 213 | public function findMissionGameConditionByMissionGame($missionGame) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Retrieve service manager instance |
||
| 220 | * |
||
| 221 | * @return ServiceManager |
||
| 222 | */ |
||
| 223 | public function getServiceManager() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Set service manager instance |
||
| 230 | * |
||
| 231 | * @return MissionGame |
||
| 232 | */ |
||
| 233 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * getMissionGameConditionMapper : retrieve missionGameCondition mapper instance |
||
| 242 | * |
||
| 243 | * @return Mapper/missionGameCondition $missionGameConditionMapper |
||
| 244 | */ |
||
| 245 | public function getMissionGameConditionMapper() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * getMissionGameMapper : retrieve missionGame mapper instance |
||
| 256 | * |
||
| 257 | * @return Mapper/MissionGameMapper $missionGameMapper |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function getMissionGameMapper() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * getGameMapper : retrieve game mapper instance |
||
| 270 | * |
||
| 271 | * @return Mapper/GameMapper $gameMapper |
||
| 272 | */ |
||
| 273 | View Code Duplication | public function getGameMapper() |
|
| 281 | } |
||
| 282 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.