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 BaseCircle 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 BaseCircle, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class BaseCircle { |
||
33 | |||
34 | const CIRCLES_SETTINGS_DEFAULT = [ |
||
35 | 'allow_links' => 'false', |
||
36 | 'allow_links_auto' => 'false', |
||
37 | 'allow_links_files' => 'false' |
||
38 | ]; |
||
39 | |||
40 | const CIRCLES_PERSONAL = 1; |
||
41 | const CIRCLES_SECRET = 2; |
||
42 | const CIRCLES_CLOSED = 4; |
||
43 | const CIRCLES_PUBLIC = 8; |
||
44 | |||
45 | const CIRCLES_ALL = 15; |
||
46 | |||
47 | const SHORT_UNIQUE_ID_LENGTH = 14; |
||
48 | |||
49 | /** @var int */ |
||
50 | private $id; |
||
51 | |||
52 | /** @var IL10N */ |
||
53 | protected $l10n; |
||
54 | |||
55 | /** @var string */ |
||
56 | private $uniqueId; |
||
57 | |||
58 | /** @var string */ |
||
59 | private $name; |
||
60 | |||
61 | /** @var Member */ |
||
62 | private $owner; |
||
63 | |||
64 | /** @var Member */ |
||
65 | private $viewer; |
||
66 | |||
67 | /** @var Member */ |
||
68 | private $viewerGroup; |
||
69 | |||
70 | /** @var string */ |
||
71 | private $description = ''; |
||
72 | |||
73 | /** @var array */ |
||
74 | private $settings = []; |
||
75 | |||
76 | /** @var int */ |
||
77 | private $type; |
||
78 | |||
79 | /** @var string */ |
||
80 | private $contactGroupName = ''; |
||
81 | |||
82 | /** @var int */ |
||
83 | private $contactAddressBook = 0; |
||
84 | |||
85 | /** @var string */ |
||
86 | private $groupId = ''; |
||
87 | |||
88 | /** @var string */ |
||
89 | private $creation; |
||
90 | |||
91 | /** @var Member[] */ |
||
92 | private $members; |
||
93 | |||
94 | /** @var Member[] */ |
||
95 | private $groups; |
||
96 | |||
97 | /** @var FederatedLink[] */ |
||
98 | private $links; |
||
99 | |||
100 | public function __construct($type = -1, $name = '') { |
||
110 | |||
111 | /** |
||
112 | * @param integer $id |
||
113 | * |
||
114 | * @return BaseCircle |
||
115 | */ |
||
116 | public function setId($id) { |
||
121 | |||
122 | /** |
||
123 | * @return integer |
||
124 | */ |
||
125 | public function getId() { |
||
128 | |||
129 | |||
130 | /** |
||
131 | * @param string $uniqueId |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setUniqueId($uniqueId) { |
||
140 | |||
141 | /** |
||
142 | * @param bool $full |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getUniqueId($full = false) { |
||
153 | |||
154 | public function generateUniqueId() { |
||
159 | |||
160 | /** |
||
161 | * @param string $name |
||
162 | * |
||
163 | * @return BaseCircle |
||
164 | */ |
||
165 | public function setName($name) { |
||
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getName() { |
||
177 | |||
178 | /** |
||
179 | * @return Member |
||
180 | */ |
||
181 | public function getOwner() { |
||
184 | |||
185 | /** |
||
186 | * @param Member $owner |
||
187 | * |
||
188 | * @return BaseCircle |
||
189 | */ |
||
190 | public function setOwner($owner) { |
||
195 | |||
196 | |||
197 | /** |
||
198 | * @return Member |
||
199 | */ |
||
200 | public function getViewer() { |
||
203 | |||
204 | /** |
||
205 | * @param Member $user |
||
206 | * |
||
207 | * @return BaseCircle |
||
208 | */ |
||
209 | public function setViewer($user) { |
||
214 | |||
215 | /** |
||
216 | * @return Member |
||
217 | */ |
||
218 | public function getGroupViewer() { |
||
221 | |||
222 | /** |
||
223 | * @param Member $group |
||
224 | * |
||
225 | * @return BaseCircle |
||
226 | */ |
||
227 | public function setGroupViewer($group) { |
||
232 | |||
233 | /** |
||
234 | * @return Member |
||
235 | */ |
||
236 | public function getHigherViewer() { |
||
254 | |||
255 | |||
256 | /** |
||
257 | * @param string $description |
||
258 | * |
||
259 | * @return BaseCircle |
||
260 | */ |
||
261 | public function setDescription($description) { |
||
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | public function getDescription() { |
||
273 | |||
274 | |||
275 | /** |
||
276 | * @param int $contactAddressBook |
||
277 | * |
||
278 | * @return BaseCircle |
||
279 | */ |
||
280 | public function setContactAddressBook(int $contactAddressBook) { |
||
285 | |||
286 | /** |
||
287 | * @return int |
||
288 | */ |
||
289 | public function getContactAddressBook() { |
||
292 | |||
293 | |||
294 | /** |
||
295 | * @param string $contactGroupName |
||
296 | * |
||
297 | * @return BaseCircle |
||
298 | */ |
||
299 | public function setContactGroupName($contactGroupName) { |
||
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getContactGroupName() { |
||
311 | |||
312 | /** |
||
313 | * @param string $groupId |
||
314 | * |
||
315 | * @return BaseCircle |
||
316 | */ |
||
317 | public function setGroupId($groupId) { |
||
322 | |||
323 | /** |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getGroupId() { |
||
329 | |||
330 | /** |
||
331 | * @param string|array $settings |
||
332 | * |
||
333 | * @return $this |
||
334 | */ |
||
335 | public function setSettings($settings) { |
||
344 | |||
345 | /** |
||
346 | * @param bool $json |
||
347 | * |
||
348 | * @return array|string |
||
349 | */ |
||
350 | public function getSettings($json = false) { |
||
370 | |||
371 | |||
372 | /** |
||
373 | * @param string $k |
||
374 | * @param mixed $v |
||
375 | */ |
||
376 | public function setSetting($k, $v) { |
||
391 | |||
392 | |||
393 | /** |
||
394 | * @param string $k |
||
395 | * |
||
396 | * @return string|null |
||
397 | */ |
||
398 | public function getSetting($k) { |
||
408 | |||
409 | /** |
||
410 | * |
||
411 | * @param string $type |
||
412 | * |
||
413 | * @return \OCA\Circles\Model\BaseCircle |
||
414 | */ |
||
415 | public function setType($type) { |
||
420 | |||
421 | /** |
||
422 | * @return string |
||
|
|||
423 | */ |
||
424 | public function getType() { |
||
427 | |||
428 | /** |
||
429 | * @param string $creation |
||
430 | * |
||
431 | * @return \OCA\Circles\Model\BaseCircle |
||
432 | */ |
||
433 | public function setCreation($creation) { |
||
438 | |||
439 | /** |
||
440 | * @return string |
||
441 | */ |
||
442 | public function getCreation() { |
||
445 | |||
446 | /** |
||
447 | * @param array $members |
||
448 | * |
||
449 | * @return BaseCircle |
||
450 | */ |
||
451 | public function setMembers($members) { |
||
456 | |||
457 | /** |
||
458 | * @return array |
||
459 | */ |
||
460 | public function getMembers() { |
||
463 | |||
464 | /** |
||
465 | * @param array $groups |
||
466 | * |
||
467 | * @return BaseCircle |
||
468 | */ |
||
469 | public function setGroups($groups) { |
||
474 | |||
475 | /** |
||
476 | * @return array |
||
477 | */ |
||
478 | public function getGroups() { |
||
481 | |||
482 | /** |
||
483 | * @param array $links |
||
484 | * |
||
485 | * @return BaseCircle |
||
486 | */ |
||
487 | public function setLinks($links) { |
||
492 | |||
493 | /** |
||
494 | * @return array |
||
495 | */ |
||
496 | public function getLinks() { |
||
499 | |||
500 | |||
501 | // public function getRemote() { |
||
502 | // return $this->remote; |
||
503 | // } |
||
504 | // |
||
505 | // public function addRemote($link) { |
||
506 | // array_push($this->remote, $link); |
||
507 | // } |
||
508 | // |
||
509 | // public function getRemoteFromToken($token) { |
||
510 | // foreach ($this->links AS $link) { |
||
511 | // if ($link->getToken() === $token) { |
||
512 | // return $link; |
||
513 | // } |
||
514 | // } |
||
515 | // |
||
516 | // return null; |
||
517 | // } |
||
518 | // |
||
519 | // public function getRemoteFromAddressAndId($address, $id) { |
||
520 | // foreach ($this->links AS $link) { |
||
521 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
522 | // return $link; |
||
523 | // } |
||
524 | // } |
||
525 | // |
||
526 | // return null; |
||
527 | // } |
||
528 | |||
529 | /** |
||
530 | * @param integer|string $type |
||
531 | * |
||
532 | * @return integer |
||
533 | */ |
||
534 | public static function typeInt($type) { |
||
553 | } |
||
554 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.