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 |
||
7 | class Category extends Model |
||
8 | { |
||
9 | /** |
||
10 | * @var int |
||
11 | */ |
||
12 | protected $id; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $color; |
||
23 | |||
24 | View Code Duplication | public function __construct(array $categoryData = []) |
|
36 | |||
37 | /** |
||
38 | * @return array |
||
39 | */ |
||
40 | View Code Duplication | public function toArray() |
|
54 | |||
55 | /** |
||
56 | * @return bool |
||
57 | * @throws LPTrackerSDKException |
||
58 | */ |
||
59 | public function validate() |
||
67 | |||
68 | /** |
||
69 | * @return int |
||
70 | */ |
||
71 | public function getId() |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getName() |
||
83 | |||
84 | /** |
||
85 | * @param string $name |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function setName($name) |
||
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | public function getColor() |
||
101 | |||
102 | /** |
||
103 | * @param string $color |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function setColor($color) |
||
111 | } |
||
112 |
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.