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 DeprecatedCircle 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 DeprecatedCircle, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class DeprecatedCircle extends BaseCircle implements JsonSerializable { |
||
35 | |||
36 | |||
37 | use TArrayTools; |
||
38 | |||
39 | |||
40 | /** @var bool */ |
||
41 | private $fullJson = false; |
||
42 | |||
43 | /** @var bool */ |
||
44 | private $lightJson = false; |
||
45 | |||
46 | |||
47 | public function getTypeString() { |
||
63 | |||
64 | public function getTypeLongString() { |
||
67 | |||
68 | |||
69 | public function getInfo() { |
||
72 | |||
73 | |||
74 | /** |
||
75 | * @param bool $fullJson |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setFullJson(bool $fullJson): self { |
||
84 | |||
85 | |||
86 | public function jsonSerialize() { |
||
117 | |||
118 | |||
119 | public function getArray($full = false, $light = false) { |
||
124 | |||
125 | |||
126 | public function getJson($full = false, $light = false) { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * set all infos from an Array. |
||
139 | * |
||
140 | * @param $arr |
||
141 | * @param bool $allSettings |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public static function fromArray($arr, bool $allSettings = false) { |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @param array $arr |
||
181 | * @param $key |
||
182 | * @param int $type |
||
183 | * |
||
184 | * @return null|DeprecatedMember |
||
185 | */ |
||
186 | private static function getMemberFromArray($arr, $key, $type = DeprecatedMember::TYPE_USER) { |
||
199 | |||
200 | |||
201 | /** |
||
202 | * @param array $arr |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | private static function getLinksFromArray($arr) { |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @param array $arr |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | private static function getSettingsFromArray($arr) { |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param $json |
||
233 | * |
||
234 | * @return DeprecatedCircle |
||
235 | */ |
||
236 | public static function fromJSON($json) { |
||
239 | |||
240 | |||
241 | /** |
||
242 | * @throws CircleTypeNotValidException |
||
243 | */ |
||
244 | public function cantBePersonal() { |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @throws FederatedCircleNotAllowedException |
||
255 | */ |
||
256 | public function hasToBeFederated() { |
||
263 | |||
264 | /** |
||
265 | * @param $type |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public static function typeLongString($type) { |
||
285 | |||
286 | |||
287 | /** |
||
288 | * convert old type to new config (nc21) |
||
289 | * |
||
290 | * @param int $type |
||
291 | * |
||
292 | * @return int |
||
293 | */ |
||
294 | public static function convertTypeToConfig(int $type): int { |
||
308 | |||
309 | |||
310 | } |
||
311 | |||
313 |
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.