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 |
||
16 | View Code Duplication | final class UpdateProjectCommand |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * The project to update. |
||
20 | * |
||
21 | * @var \Gitamin\Models\Project |
||
22 | */ |
||
23 | public $project; |
||
24 | |||
25 | /** |
||
26 | * The project name. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | public $name; |
||
31 | |||
32 | /** |
||
33 | * The project description. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | public $description; |
||
38 | |||
39 | /** |
||
40 | * The project visibility_level. |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | public $visibility_level; |
||
45 | |||
46 | /** |
||
47 | * The project path. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | public $path; |
||
52 | |||
53 | /** |
||
54 | * The project creator id. |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | public $creator_id; |
||
59 | |||
60 | /** |
||
61 | * The project owner. |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | public $owner_id; |
||
66 | |||
67 | /** |
||
68 | * The validation rules. |
||
69 | * |
||
70 | * @var string[] |
||
71 | */ |
||
72 | public $rules = [ |
||
73 | 'name' => 'string', |
||
74 | 'description' => 'string', |
||
75 | 'visibility_level' => 'int|min:1|max:4', |
||
76 | 'path' => 'string', |
||
77 | 'creator_id' => 'int', |
||
78 | 'owner_id' => 'int', |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * Create a new update project command instance. |
||
83 | * |
||
84 | * @param \Gitamin\Models\Project $project |
||
85 | * @param string $name |
||
86 | * @param string $description |
||
87 | * @param int $visibility_level |
||
88 | * @param string $path |
||
89 | * @param int $creator_id |
||
90 | * @param int $owner_id |
||
91 | */ |
||
92 | public function __construct(Project $project, $name, $description, $visibility_level, $path, $creator_id, $owner_id) |
||
102 | } |
||
103 |
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.