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 | View Code Duplication | final class SignupUserCommand |
|
|
|||
15 | { |
||
16 | /** |
||
17 | * The user username. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $username; |
||
22 | |||
23 | /** |
||
24 | * The user password. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $password; |
||
29 | |||
30 | /** |
||
31 | * The user email. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | public $email; |
||
36 | |||
37 | /** |
||
38 | * The user level. |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | public $level; |
||
43 | |||
44 | /** |
||
45 | * The validation rules. |
||
46 | * |
||
47 | * @var string[] |
||
48 | */ |
||
49 | public $rules = [ |
||
50 | 'username' => 'required|string', |
||
51 | 'password' => 'string', |
||
52 | 'email' => 'required|string|email', |
||
53 | 'level' => 'int', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Create a new signup user command instance. |
||
58 | * |
||
59 | * @param string $username |
||
60 | * @param string $password |
||
61 | * @param string $email |
||
62 | * @param int $level |
||
63 | */ |
||
64 | public function __construct($username, $password, $email, $level) |
||
71 | } |
||
72 |
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.