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:
Complex classes like TEntityTypeShapeType often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TEntityTypeShapeType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class TEntityTypeShapeType extends IsOK |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @property string $entityType |
||
18 | */ |
||
19 | private $entityType = null; |
||
20 | |||
21 | /** |
||
22 | * @property float $pointX |
||
23 | */ |
||
24 | private $pointX = null; |
||
25 | |||
26 | /** |
||
27 | * @property float $pointY |
||
28 | */ |
||
29 | private $pointY = null; |
||
30 | |||
31 | /** |
||
32 | * @property float $width |
||
33 | */ |
||
34 | private $width = null; |
||
35 | |||
36 | /** |
||
37 | * @property float $height |
||
38 | */ |
||
39 | private $height = null; |
||
40 | |||
41 | /** |
||
42 | * @property boolean $isExpanded |
||
43 | */ |
||
44 | private $isExpanded = null; |
||
45 | |||
46 | /** |
||
47 | * @property string $fillColor |
||
48 | */ |
||
49 | private $fillColor = null; |
||
50 | |||
51 | /** |
||
52 | * Gets as entityType |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getEntityType() |
||
60 | |||
61 | /** |
||
62 | * Sets a new entityType |
||
63 | * |
||
64 | * @param string $entityType |
||
65 | * @return self |
||
66 | */ |
||
67 | public function setEntityType($entityType) |
||
76 | |||
77 | /** |
||
78 | * Gets as pointX |
||
79 | * |
||
80 | * @return float |
||
81 | */ |
||
82 | public function getPointX() |
||
86 | |||
87 | /** |
||
88 | * Sets a new pointX |
||
89 | * |
||
90 | * @param float $pointX |
||
91 | * @return self |
||
92 | */ |
||
93 | View Code Duplication | public function setPointX($pointX) |
|
102 | |||
103 | /** |
||
104 | * Gets as pointY |
||
105 | * |
||
106 | * @return float |
||
107 | */ |
||
108 | public function getPointY() |
||
112 | |||
113 | /** |
||
114 | * Sets a new pointY |
||
115 | * |
||
116 | * @param float $pointY |
||
117 | * @return self |
||
118 | */ |
||
119 | View Code Duplication | public function setPointY($pointY) |
|
128 | |||
129 | /** |
||
130 | * Gets as width |
||
131 | * |
||
132 | * @return float |
||
133 | */ |
||
134 | public function getWidth() |
||
138 | |||
139 | /** |
||
140 | * Sets a new width |
||
141 | * |
||
142 | * @param float $width |
||
143 | * @return self |
||
144 | */ |
||
145 | View Code Duplication | public function setWidth($width) |
|
154 | |||
155 | /** |
||
156 | * Gets as height |
||
157 | * |
||
158 | * @return float |
||
159 | */ |
||
160 | public function getHeight() |
||
164 | |||
165 | /** |
||
166 | * Sets a new height |
||
167 | * |
||
168 | * @param float $height |
||
169 | * @return self |
||
170 | */ |
||
171 | View Code Duplication | public function setHeight($height) |
|
180 | |||
181 | /** |
||
182 | * Gets as isExpanded |
||
183 | * |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function getIsExpanded() |
||
190 | |||
191 | /** |
||
192 | * Sets a new isExpanded |
||
193 | * |
||
194 | * @param boolean $isExpanded |
||
195 | * @return self |
||
196 | */ |
||
197 | public function setIsExpanded($isExpanded) |
||
202 | |||
203 | /** |
||
204 | * Gets as fillColor |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getFillColor() |
||
212 | |||
213 | /** |
||
214 | * Sets a new fillColor |
||
215 | * |
||
216 | * @param string $fillColor |
||
217 | * @return self |
||
218 | */ |
||
219 | public function setFillColor($fillColor) |
||
228 | |||
229 | /** |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function isOK(&$msg = null) |
||
263 | } |
||
264 |
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.