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 |
||
14 | class CoreUser extends \FOS\UserBundle\Model\User |
||
15 | { |
||
16 | /** |
||
17 | * @ORM\Id |
||
18 | * @ORM\Column(type="integer") |
||
19 | * @ORM\GeneratedValue(strategy="AUTO") |
||
20 | */ |
||
21 | protected $id; |
||
22 | |||
23 | /** |
||
24 | * Image de profil |
||
25 | * @ORM\OneToOne(targetEntity="KI\CoreBundle\Entity\Image", cascade={"persist", "remove"}) |
||
26 | * @Assert\Valid() |
||
27 | */ |
||
28 | protected $image; |
||
29 | |||
30 | /** |
||
31 | * Prénom |
||
32 | * @ORM\Column(name="firstName", type="string") |
||
33 | * @JMS\Expose |
||
34 | * @Assert\NotBlank() |
||
35 | */ |
||
36 | protected $firstName; |
||
37 | |||
38 | /** |
||
39 | * Nom |
||
40 | * @ORM\Column(name="lastName", type="string") |
||
41 | * @JMS\Expose |
||
42 | * @Assert\NotBlank() |
||
43 | */ |
||
44 | protected $lastName; |
||
45 | |||
46 | /** |
||
47 | * Surnom/pseudo |
||
48 | * @ORM\Column(name="nickname", type="string", nullable=true) |
||
49 | */ |
||
50 | protected $nickname; |
||
51 | |||
52 | /** |
||
53 | * Groupes de permissions FOSUserBundle |
||
54 | * @ORM\ManyToMany(targetEntity="KI\UserBundle\Entity\Group", inversedBy="users") |
||
55 | * @ORM\JoinTable(name="fos_user_user_group", |
||
56 | * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, |
||
57 | * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} |
||
58 | * ) |
||
59 | */ |
||
60 | protected $groups; |
||
61 | |||
62 | /** |
||
63 | * Date de dernière connexion (timestamp) |
||
64 | * @ORM\Column(name="lastConnect", type="integer", nullable=true) |
||
65 | * @Assert\Type("integer") |
||
66 | */ |
||
67 | protected $lastConnect; |
||
68 | |||
69 | /** |
||
70 | * Appareils mobiles enregistrés pour recevoir des notifications Push |
||
71 | * @ORM\OneToMany(targetEntity="KI\UserBundle\Entity\Device", mappedBy="owner") |
||
72 | */ |
||
73 | protected $devices; |
||
74 | |||
75 | /** |
||
76 | * Clubs auquels l'utilisateur n'est PAS abonné |
||
77 | * @ORM\ManyToMany(targetEntity="KI\UserBundle\Entity\Club") |
||
78 | */ |
||
79 | protected $clubsNotFollowed; |
||
80 | |||
81 | /** |
||
82 | * Tableau contenant les préférences utilisateurs. Les valeurs possibles des clés de ce tableau ainsi que |
||
83 | * leur valeurs par défaut sont définies dans $preferencesArray |
||
84 | * @ORM\Column(name="preferences", type="array", nullable=true) |
||
85 | * @Assert\Type("array") |
||
86 | */ |
||
87 | protected $preferences = []; |
||
88 | |||
89 | /** |
||
90 | * Token faible permettant de créer des urls personnalisées pour l'user |
||
91 | * @ORM\Column(name="token", type="string", nullable=true) |
||
92 | * @Assert\Type("string") |
||
93 | */ |
||
94 | protected $token; |
||
95 | |||
96 | /** |
||
97 | * Méthode d'authentification |
||
98 | * @ORM\Column(name="login_method", type="string") |
||
99 | * @Assert\Type("string") |
||
100 | */ |
||
101 | protected $loginMethod; |
||
102 | |||
103 | protected $preferencesArray = [ |
||
104 | 'notif_followed_event' => true, |
||
105 | 'notif_followed_news' => true, |
||
106 | 'notif_news_perso' => true, |
||
107 | 'notif_comments' => true, |
||
108 | 'notif_shotgun_freed' => true, |
||
109 | 'notif_ponthub' => false, |
||
110 | 'notif_fixs' => true, |
||
111 | 'notif_followed_annal' => true, |
||
112 | //'notif_achievement' => true, |
||
113 | //'notif_next_level' => true |
||
114 | ]; |
||
115 | |||
116 | /** |
||
117 | * @JMS\VirtualProperty() |
||
118 | */ |
||
119 | public function imageUrl() |
||
127 | |||
128 | /** |
||
129 | * @JMS\VirtualProperty() |
||
130 | */ |
||
131 | public function nick() |
||
135 | |||
136 | // On définit des alias pour le slug |
||
137 | public function getSlug() { return $this->getUsername(); } |
||
140 | |||
141 | |||
142 | |||
143 | |||
144 | //===== GENERATED AUTOMATICALLY =====// |
||
145 | |||
146 | /** |
||
147 | * Constructor |
||
148 | */ |
||
149 | public function __construct() |
||
155 | |||
156 | /** |
||
157 | * Get id |
||
158 | * |
||
159 | * @return integer |
||
160 | */ |
||
161 | public function getId() |
||
165 | |||
166 | /** |
||
167 | * Set firstName |
||
168 | * |
||
169 | * @param string $firstName |
||
170 | * @return User |
||
171 | */ |
||
172 | public function setFirstName($firstName) |
||
178 | |||
179 | /** |
||
180 | * Get firstName |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getFirstName() |
||
188 | |||
189 | /** |
||
190 | * Set lastName |
||
191 | * |
||
192 | * @param string $lastName |
||
193 | * @return User |
||
194 | */ |
||
195 | public function setLastName($lastName) |
||
201 | |||
202 | /** |
||
203 | * Get lastName |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getLastName() |
||
211 | |||
212 | /** |
||
213 | * Set nickname |
||
214 | * |
||
215 | * @param string $nickname |
||
216 | * @return User |
||
217 | */ |
||
218 | public function setNickname($nickname) |
||
224 | |||
225 | /** |
||
226 | * Add group |
||
227 | * |
||
228 | * @param \KI\UserBundle\Entity\User $group |
||
229 | * @return Comment |
||
230 | */ |
||
231 | public function addGroupUser(\KI\UserBundle\Entity\Group $group) |
||
238 | |||
239 | /** |
||
240 | * Remove group |
||
241 | * |
||
242 | * @param \KI\UserBundle\Entity\User $group |
||
243 | */ |
||
244 | public function removeGroupUser(\KI\UserBundle\Entity\Group $group) |
||
249 | |||
250 | /** |
||
251 | * Get lastConnect |
||
252 | * |
||
253 | * @return integer |
||
254 | */ |
||
255 | public function getLastConnect() |
||
259 | |||
260 | /** |
||
261 | * Set lastConnect |
||
262 | * |
||
263 | * @param integer $lastConnect |
||
264 | * @return User |
||
265 | */ |
||
266 | public function setLastConnect($lastConnect) |
||
272 | |||
273 | /** |
||
274 | * Get nickname |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getNickname() |
||
282 | |||
283 | /** |
||
284 | * Set image |
||
285 | * |
||
286 | * @param \KI\CoreBundle\Entity\Image $image |
||
287 | * @return User |
||
288 | */ |
||
289 | public function setImage(\KI\CoreBundle\Entity\Image $image = null) |
||
295 | |||
296 | /** |
||
297 | * Get image |
||
298 | * |
||
299 | * @return \KI\CoreBundle\Entity\Image |
||
300 | */ |
||
301 | public function getImage() |
||
305 | |||
306 | /** |
||
307 | * Add devices |
||
308 | * |
||
309 | * @param \KI\UserBundle\Entity\Device $devices |
||
310 | * @return User |
||
311 | */ |
||
312 | public function addDevice(\KI\UserBundle\Entity\Device $devices) |
||
318 | |||
319 | /** |
||
320 | * Remove devices |
||
321 | * |
||
322 | * @param \KI\UserBundle\Entity\Device $devices |
||
323 | */ |
||
324 | public function removeDevice(\KI\UserBundle\Entity\Device $devices) |
||
328 | |||
329 | /** |
||
330 | * Get devices |
||
331 | * |
||
332 | * @return \Doctrine\Common\Collections\Collection |
||
333 | */ |
||
334 | public function getDevices() |
||
338 | |||
339 | /** |
||
340 | * Add Club Not Followed |
||
341 | * |
||
342 | * @param \KI\UserBundle\Entity\Club $club |
||
343 | * @return User |
||
344 | */ |
||
345 | public function addClubNotFollowed(\KI\UserBundle\Entity\Club $club) |
||
351 | |||
352 | /** |
||
353 | * Remove Club Not Followed |
||
354 | * |
||
355 | * @param \KI\UserBundle\Entity\Club $club |
||
356 | */ |
||
357 | public function removeClubNotFollowed(\KI\UserBundle\Entity\Club $club) |
||
361 | |||
362 | /** |
||
363 | * Get Clubs Not Followed |
||
364 | * |
||
365 | * @return \Doctrine\Common\Collections\Collection |
||
366 | */ |
||
367 | public function getClubsNotFollowed() |
||
371 | |||
372 | /** |
||
373 | * Add Preference |
||
374 | * |
||
375 | * @param string $key |
||
376 | * @param string $value |
||
377 | * @return bool |
||
378 | */ |
||
379 | View Code Duplication | public function addPreference($key, $value) |
|
388 | |||
389 | /** |
||
390 | * Remove Preference |
||
391 | * |
||
392 | * @param string $key |
||
393 | * @return bool |
||
394 | */ |
||
395 | View Code Duplication | public function removePreference($key) |
|
403 | |||
404 | /** |
||
405 | * Get Preferences Array |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | public function getPreferences() |
||
413 | |||
414 | /** |
||
415 | * Set token |
||
416 | * |
||
417 | * @param string $token |
||
418 | * @return User |
||
419 | */ |
||
420 | public function setToken($token) |
||
426 | |||
427 | /** |
||
428 | * Get token |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getToken() |
||
436 | |||
437 | /** |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getLoginMethod() |
||
444 | |||
445 | /** |
||
446 | * @param string $loginMethod |
||
447 | * @return User |
||
448 | */ |
||
449 | public function setLoginMethod($loginMethod) |
||
455 | |||
456 | |||
457 | } |
||
458 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.