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 |
||
11 | View Code Duplication | class Action |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * @ORM\Column(type="integer") |
||
15 | * @ORM\Id |
||
16 | * @ORM\GeneratedValue(strategy="AUTO") |
||
17 | */ |
||
18 | private $id; |
||
19 | |||
20 | /** |
||
21 | * @ORM\Column(type="string", length=100) |
||
22 | */ |
||
23 | private $name; |
||
24 | |||
25 | /** |
||
26 | * @ORM\Column(type="string", length=100) |
||
27 | */ |
||
28 | private $alias; |
||
29 | |||
30 | /** |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function getAlias() |
||
37 | |||
38 | /** |
||
39 | * @param mixed $alias |
||
40 | * @return Action |
||
41 | */ |
||
42 | 1 | public function setAlias($alias) |
|
43 | { |
||
44 | 1 | $this->alias = $alias; |
|
45 | 1 | return $this; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @ORM\Column(type="string", length=100) |
||
50 | */ |
||
51 | private $executor; |
||
52 | |||
53 | /** |
||
54 | * @ORM\Column(type="text") |
||
55 | */ |
||
56 | private $arguments; |
||
57 | |||
58 | /** |
||
59 | * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Device", inversedBy="actions") |
||
60 | */ |
||
61 | private $device; |
||
62 | |||
63 | /** |
||
64 | * @ORM\Column(type="string", length=100) |
||
65 | */ |
||
66 | private $type; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @ORM\OneToMany(targetEntity="AppBundle\Entity\ActionHistory", mappedBy="action") |
||
71 | */ |
||
72 | public $history; |
||
73 | |||
74 | /** |
||
75 | * @ORM\OneToMany(targetEntity="AppBundle\Entity\VarHook", mappedBy="action") |
||
76 | */ |
||
77 | public $hooks; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * @return mixed |
||
82 | */ |
||
83 | public function getType() |
||
87 | |||
88 | /** |
||
89 | * @param mixed $type |
||
90 | * @return Action |
||
91 | */ |
||
92 | 1 | public function setType($type) |
|
93 | { |
||
94 | 1 | $this->type = $type; |
|
95 | 1 | return $this; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return Device |
||
100 | */ |
||
101 | public function getDevice() |
||
105 | |||
106 | /** |
||
107 | * @param mixed $device |
||
108 | * @return Action |
||
109 | */ |
||
110 | 1 | public function setDevice($device) |
|
111 | { |
||
112 | 1 | $this->device = $device; |
|
113 | 1 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function getArguments() |
||
123 | |||
124 | /** |
||
125 | * @param mixed $arguments |
||
126 | * @return Action |
||
127 | */ |
||
128 | 1 | public function setArguments($arguments) |
|
129 | { |
||
130 | 1 | $this->arguments = $arguments; |
|
131 | 1 | return $this; |
|
132 | } |
||
133 | |||
134 | |||
135 | /** |
||
136 | * @return mixed |
||
137 | */ |
||
138 | public function getId() |
||
142 | |||
143 | /** |
||
144 | * @param mixed $id |
||
145 | * @return Action |
||
146 | */ |
||
147 | public function setId($id) |
||
152 | |||
153 | /** |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function getName() |
||
160 | |||
161 | /** |
||
162 | * @param mixed $name |
||
163 | * @return Action |
||
164 | */ |
||
165 | 1 | public function setName($name) |
|
166 | { |
||
167 | 1 | $this->name = $name; |
|
168 | 1 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function getExecutor() |
||
178 | |||
179 | /** |
||
180 | * @param mixed $executor |
||
181 | * @return Action |
||
182 | */ |
||
183 | 1 | public function setExecutor($executor) |
|
184 | { |
||
185 | 1 | $this->executor = $executor; |
|
186 | 1 | return $this; |
|
187 | } |
||
188 | |||
189 | public function __toString() |
||
193 | } |
||
194 |
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.