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 | class Role |
|
|
|||
15 | { |
||
16 | /** |
||
17 | * @var integer |
||
18 | * |
||
19 | * @ORM\Column(name="id", type="integer", nullable=false) |
||
20 | * @ORM\Id |
||
21 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
22 | */ |
||
23 | private $id; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | * |
||
28 | * @ORM\Column(name="name", type="string", length=50, nullable=false) |
||
29 | */ |
||
30 | private $name; |
||
31 | |||
32 | /** |
||
33 | * @var boolean |
||
34 | * |
||
35 | * @ORM\Column(name="status", type="boolean", nullable=false) |
||
36 | */ |
||
37 | private $status = '1'; |
||
38 | |||
39 | /** |
||
40 | * @var \DateTime |
||
41 | * |
||
42 | * @ORM\Column(name="created_at", type="datetime", nullable=false) |
||
43 | */ |
||
44 | private $createdAt; |
||
45 | |||
46 | /** |
||
47 | * @var \DateTime |
||
48 | * |
||
49 | * @ORM\Column(name="updated_at", type="datetime", nullable=true) |
||
50 | */ |
||
51 | private $updatedAt; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | * |
||
56 | * @ORM\Column(name="description", type="string", nullable=true) |
||
57 | */ |
||
58 | private $description; |
||
59 | |||
60 | /** |
||
61 | * @return int |
||
62 | */ |
||
63 | public function getId() |
||
67 | |||
68 | /** |
||
69 | * @param int $id |
||
70 | */ |
||
71 | public function setId($id) |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getName() |
||
83 | |||
84 | /** |
||
85 | * @param string $name |
||
86 | */ |
||
87 | public function setName($name) |
||
91 | |||
92 | /** |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function isStatus() |
||
99 | |||
100 | /** |
||
101 | * @param bool $status |
||
102 | */ |
||
103 | public function setStatus($status) |
||
107 | |||
108 | /** |
||
109 | * @return \DateTime |
||
110 | */ |
||
111 | public function getCreatedAt() |
||
115 | |||
116 | /** |
||
117 | * @param \DateTime $createdAt |
||
118 | */ |
||
119 | public function setCreatedAt($createdAt) |
||
123 | |||
124 | /** |
||
125 | * @return \DateTime |
||
126 | */ |
||
127 | public function getUpdatedAt() |
||
131 | |||
132 | /** |
||
133 | * @param \DateTime $updatedAt |
||
134 | */ |
||
135 | public function setUpdatedAt($updatedAt) |
||
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getDescription() |
||
147 | |||
148 | /** |
||
149 | * @param string $description |
||
150 | */ |
||
151 | public function setDescription($description) |
||
155 | |||
156 | /** @ORM\PrePersist */ |
||
157 | public function prePersist() |
||
161 | |||
162 | /** @ORM\PreUpdate */ |
||
163 | public function preUpdate() |
||
167 | } |
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.