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 namespace Anomaly\UsersModule\User; |
||
21 | class UserAuthenticator |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Laravel's authentication. |
||
26 | * |
||
27 | * @var Guard |
||
28 | */ |
||
29 | protected $guard; |
||
30 | |||
31 | /** |
||
32 | * The event dispatcher. |
||
33 | * |
||
34 | * @var Dispatcher |
||
35 | */ |
||
36 | protected $events; |
||
37 | |||
38 | /** |
||
39 | * The service container. |
||
40 | * |
||
41 | * @var Container |
||
42 | */ |
||
43 | protected $container; |
||
44 | |||
45 | /** |
||
46 | * The extension collection. |
||
47 | * |
||
48 | * @var ExtensionCollection |
||
49 | */ |
||
50 | protected $extensions; |
||
51 | |||
52 | /** |
||
53 | * Create a new Authenticator instance. |
||
54 | * |
||
55 | * @param Guard $guard |
||
56 | * @param Dispatcher $events |
||
57 | * @param Container $container |
||
58 | * @param ExtensionCollection $extensions |
||
59 | */ |
||
60 | View Code Duplication | public function __construct(Guard $guard, Dispatcher $events, Container $container, ExtensionCollection $extensions) |
|
67 | |||
68 | /** |
||
69 | * Attempt to login a user. |
||
70 | * |
||
71 | * @param array $credentials |
||
72 | * @param bool $remember |
||
73 | * @return bool|UserInterface |
||
74 | */ |
||
75 | public function attempt(array $credentials, $remember = false) |
||
88 | |||
89 | /** |
||
90 | * Attempt to authenticate the credentials. |
||
91 | * |
||
92 | * @param array $credentials |
||
93 | * @return bool|UserInterface |
||
94 | */ |
||
95 | public function authenticate(array $credentials) |
||
115 | |||
116 | /** |
||
117 | * Force login a user. |
||
118 | * |
||
119 | * @param UserInterface $user |
||
120 | * @param bool $remember |
||
121 | */ |
||
122 | public function login(UserInterface $user, $remember = false) |
||
128 | |||
129 | /** |
||
130 | * Logout a user. |
||
131 | * |
||
132 | * @param UserInterface $user |
||
133 | */ |
||
134 | public function logout(UserInterface $user = null) |
||
148 | |||
149 | /** |
||
150 | * Kick out a user. They've been bad. |
||
151 | * |
||
152 | * @param UserInterface $user |
||
153 | */ |
||
154 | public function kickOut(UserInterface $user, $reason) |
||
160 | } |
||
161 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.