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 |
||
| 31 | class BaseCircle { |
||
| 32 | |||
| 33 | const CIRCLES_SETTINGS_DEFAULT = [ |
||
| 34 | 'allow_links' => 'false', |
||
| 35 | 'allow_links_auto' => 'false', |
||
| 36 | 'allow_links_files' => 'false' |
||
| 37 | ]; |
||
| 38 | |||
| 39 | const CIRCLES_PERSONAL = 1; |
||
| 40 | const CIRCLES_HIDDEN = 2; |
||
| 41 | const CIRCLES_PRIVATE = 4; |
||
| 42 | const CIRCLES_PUBLIC = 8; |
||
| 43 | |||
| 44 | const CIRCLES_ALL = 15; |
||
| 45 | |||
| 46 | /** @var int */ |
||
| 47 | private $id; |
||
| 48 | |||
| 49 | /** @var L10N */ |
||
| 50 | protected $l10n; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $uniqueId; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $name; |
||
| 57 | |||
| 58 | /** @var Member */ |
||
| 59 | private $owner; |
||
| 60 | |||
| 61 | /** @var Member */ |
||
| 62 | private $viewer; |
||
| 63 | |||
| 64 | /** @var Member */ |
||
| 65 | private $viewerGroup; |
||
| 66 | |||
| 67 | /** @var string */ |
||
| 68 | private $description = ''; |
||
| 69 | |||
| 70 | /** @var array */ |
||
| 71 | private $settings = []; |
||
| 72 | |||
| 73 | /** @var int */ |
||
| 74 | private $type; |
||
| 75 | |||
| 76 | /** @var string */ |
||
| 77 | private $creation; |
||
| 78 | |||
| 79 | /** @var Member[] */ |
||
| 80 | private $members; |
||
| 81 | |||
| 82 | /** @var Member[] */ |
||
| 83 | private $groups; |
||
| 84 | |||
| 85 | /** @var FederatedLink[] */ |
||
| 86 | private $links; |
||
| 87 | |||
| 88 | public function __construct($l10n, $type = -1, $name = '') { |
||
| 98 | |||
| 99 | |||
| 100 | public function setId($id) { |
||
| 105 | |||
| 106 | public function getId() { |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $uniqueId |
||
| 113 | * |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | public function setUniqueId($uniqueId) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param bool $full |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getUniqueId($full = false) { |
||
| 134 | |||
| 135 | |||
| 136 | public function generateUniqueId() { |
||
| 140 | |||
| 141 | public function setName($name) { |
||
| 146 | |||
| 147 | public function getName() { |
||
| 150 | |||
| 151 | |||
| 152 | public function getOwner() { |
||
| 155 | |||
| 156 | public function setOwner($owner) { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * @return Member |
||
| 165 | */ |
||
| 166 | public function getViewer() { |
||
| 169 | |||
| 170 | public function setViewer($user) { |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @return Member |
||
| 179 | */ |
||
| 180 | public function getGroupViewer() { |
||
| 183 | |||
| 184 | public function setGroupViewer($group) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @return Member |
||
| 193 | */ |
||
| 194 | public function getHigherViewer() { |
||
| 212 | |||
| 213 | |||
| 214 | public function setDescription($description) { |
||
| 219 | |||
| 220 | public function getDescription() { |
||
| 223 | |||
| 224 | |||
| 225 | public function setSettings($settings) { |
||
| 234 | |||
| 235 | public function getSettings($json = false) { |
||
| 255 | |||
| 256 | |||
| 257 | public function setSetting($k, $v) { |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $k |
||
| 276 | * |
||
| 277 | * @return string|null |
||
| 278 | */ |
||
| 279 | public function getSetting($k) { |
||
| 289 | |||
| 290 | |||
| 291 | public function setType($type) { |
||
| 296 | |||
| 297 | public function getType() { |
||
| 300 | |||
| 301 | |||
| 302 | public function setCreation($creation) { |
||
| 307 | |||
| 308 | public function getCreation() { |
||
| 311 | |||
| 312 | |||
| 313 | public function setMembers($members) { |
||
| 318 | |||
| 319 | public function getMembers() { |
||
| 322 | |||
| 323 | |||
| 324 | public function setGroups($groups) { |
||
| 329 | |||
| 330 | public function getGroups() { |
||
| 333 | |||
| 334 | |||
| 335 | public function setLinks($links) { |
||
| 340 | |||
| 341 | public function getLinks() { |
||
| 344 | |||
| 345 | |||
| 346 | // public function getRemote() { |
||
|
|
|||
| 347 | // return $this->remote; |
||
| 348 | // } |
||
| 349 | // |
||
| 350 | // public function addRemote($link) { |
||
| 351 | // array_push($this->remote, $link); |
||
| 352 | // } |
||
| 353 | // |
||
| 354 | // public function getRemoteFromToken($token) { |
||
| 355 | // foreach ($this->links AS $link) { |
||
| 356 | // if ($link->getToken() === $token) { |
||
| 357 | // return $link; |
||
| 358 | // } |
||
| 359 | // } |
||
| 360 | // |
||
| 361 | // return null; |
||
| 362 | // } |
||
| 363 | // |
||
| 364 | // public function getRemoteFromAddressAndId($address, $id) { |
||
| 365 | // foreach ($this->links AS $link) { |
||
| 366 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
| 367 | // return $link; |
||
| 368 | // } |
||
| 369 | // } |
||
| 370 | // |
||
| 371 | // return null; |
||
| 372 | // } |
||
| 373 | |||
| 374 | |||
| 375 | public static function typeInt($type) { |
||
| 394 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.