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 Sofa\Revisionable\Adapters; |
||
7 | View Code Duplication | class JwtAuth implements UserProvider |
|
|
|||
8 | { |
||
9 | |||
10 | /** |
||
11 | * Auth provider instance. |
||
12 | * |
||
13 | * @var \Tymon\JWTAuth\JWTAuth |
||
14 | */ |
||
15 | protected $provider; |
||
16 | |||
17 | /** |
||
18 | * Field from the user to be saved as author of the action. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $field; |
||
23 | |||
24 | /** |
||
25 | * Create adapter instance for Illuminate Guard. |
||
26 | * |
||
27 | * @param \Tymon\JWTAuth\JWTAuth $provider |
||
28 | * @param string $field |
||
29 | */ |
||
30 | public function __construct(JWT $provider, $field = null) |
||
31 | { |
||
32 | $this->provider = $provider; |
||
33 | $this->field = $field; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Get identifier of the currently logged in user. |
||
38 | * |
||
39 | * @return string|null |
||
40 | */ |
||
41 | public function getUser() |
||
45 | |||
46 | /** |
||
47 | * Get value from the user to be saved as the author. |
||
48 | * |
||
49 | * @return string|null |
||
50 | */ |
||
51 | protected function getUserFieldValue() |
||
57 | } |
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.