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 |
||
12 | class UserIdentity extends Model implements IdentityInterface |
||
13 | { |
||
14 | public $id; |
||
15 | public $username; |
||
16 | public $state; |
||
17 | public $roles = []; |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | View Code Duplication | public function rules() |
|
31 | |||
32 | /** {@inheritdoc} */ |
||
33 | public static function findIdentity($id) |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public static function findIdentityByAccessToken($token, $type = null) |
||
45 | |||
46 | public static function fromArray(array $info): self |
||
61 | |||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public function getId() |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function getAuthKey() |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function validateAuthKey($authKey) |
||
86 | } |
||
87 |
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.