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) |
||
82 | /** |
||
83 | * associate : Permet d'associer des jeux et des conditions à une mission |
||
84 | * @param array $data |
||
85 | * @param Mission $mission |
||
86 | * |
||
87 | * @return MissionGameEntity $missionGameEntity |
||
88 | */ |
||
89 | public function associate($data, $mission) |
||
106 | |||
107 | /** |
||
108 | * clear : Permet de supprimer l'association des jeux et des conditions à une mission |
||
109 | * @param Mission $mission |
||
110 | */ |
||
111 | public function clear($mission) |
||
118 | |||
119 | |||
120 | View Code Duplication | public function checkCondition($game, $winner, $prediction, $entry) |
|
189 | |||
190 | /** |
||
191 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
192 | * @param Mission $mission |
||
193 | * |
||
194 | * @return Collection de MissionGame $missionGames |
||
195 | */ |
||
196 | public function findMissionGameByMission($mission) |
||
200 | |||
201 | /** |
||
202 | * findMissionGameByMission : Permet de recuperer les missionsGame à partir d'une mission |
||
203 | * |
||
204 | * @return Collection de MissionGame $missionGames |
||
205 | */ |
||
206 | public function findMissionGameByGame($game) |
||
210 | |||
211 | /** |
||
212 | * findMissionGameConditionByMissionGame : Permet de recuperer les missionsGameCondition à partir d'une missionGame |
||
213 | * @param MissionGame $missionGame |
||
214 | * |
||
215 | * @return Collection de MissionGameCondition $missionGameConditions |
||
216 | */ |
||
217 | public function findMissionGameConditionByMissionGame($missionGame) |
||
221 | |||
222 | /** |
||
223 | * Retrieve service manager instance |
||
224 | * |
||
225 | * @return ServiceManager |
||
226 | */ |
||
227 | public function getServiceManager() |
||
231 | |||
232 | /** |
||
233 | * Set service manager instance |
||
234 | * |
||
235 | * @return MissionGame |
||
236 | */ |
||
237 | public function setServiceManager(ServiceManager $serviceManager) |
||
243 | |||
244 | /** |
||
245 | * getMissionGameConditionMapper : retrieve missionGameCondition mapper instance |
||
246 | * |
||
247 | * @return Mapper/missionGameCondition $missionGameConditionMapper |
||
248 | */ |
||
249 | public function getMissionGameConditionMapper() |
||
259 | |||
260 | /** |
||
261 | * getMissionGameMapper : retrieve missionGame mapper instance |
||
262 | * |
||
263 | * @return Mapper/MissionGameMapper $missionGameMapper |
||
264 | */ |
||
265 | View Code Duplication | public function getMissionGameMapper() |
|
273 | |||
274 | /** |
||
275 | * getGameMapper : retrieve game mapper instance |
||
276 | * |
||
277 | * @return Mapper/GameMapper $gameMapper |
||
278 | */ |
||
279 | View Code Duplication | public function getGameMapper() |
|
287 | } |
||
288 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.