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 $user; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $description = ''; |
||
| 66 | |||
| 67 | /** @var array */ |
||
| 68 | private $settings = []; |
||
| 69 | |||
| 70 | /** @var int */ |
||
| 71 | private $type; |
||
| 72 | |||
| 73 | /** @var string */ |
||
| 74 | private $creation; |
||
| 75 | |||
| 76 | /** @var Member[] */ |
||
| 77 | private $members; |
||
| 78 | |||
| 79 | /** @var FederatedLink[] */ |
||
| 80 | private $links; |
||
| 81 | |||
| 82 | /** @var FederatedLink[] */ |
||
| 83 | private $remote; |
||
| 84 | |||
| 85 | public function __construct($l10n, $type = -1, $name = '') { |
||
| 95 | |||
| 96 | |||
| 97 | public function setId($id) { |
||
| 102 | |||
| 103 | public function getId() { |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $uniqueId |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function setUniqueId($uniqueId) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param bool $full |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getUniqueId($full = false) { |
||
| 131 | |||
| 132 | |||
| 133 | public function generateUniqueId() { |
||
| 137 | |||
| 138 | public function setName($name) { |
||
| 143 | |||
| 144 | public function getName() { |
||
| 147 | |||
| 148 | |||
| 149 | public function getOwner() { |
||
| 152 | |||
| 153 | public function setOwner($owner) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @return Member |
||
| 162 | */ |
||
| 163 | public function getUser() { |
||
| 166 | |||
| 167 | public function setUser($user) { |
||
| 172 | |||
| 173 | |||
| 174 | public function setDescription($description) { |
||
| 179 | |||
| 180 | public function getDescription() { |
||
| 183 | |||
| 184 | |||
| 185 | public function setSettings($settings) { |
||
| 194 | |||
| 195 | public function getSettings($json = false) { |
||
| 215 | |||
| 216 | |||
| 217 | public function setSetting($k, $v) { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $k |
||
| 224 | * |
||
| 225 | * @return string|null |
||
| 226 | */ |
||
| 227 | public function getSetting($k) { |
||
| 237 | |||
| 238 | |||
| 239 | public function setType($type) { |
||
| 244 | |||
| 245 | public function getType() { |
||
| 248 | |||
| 249 | |||
| 250 | public function setCreation($creation) { |
||
| 255 | |||
| 256 | public function getCreation() { |
||
| 259 | |||
| 260 | |||
| 261 | public function setMembers($members) { |
||
| 266 | |||
| 267 | public function getMembers() { |
||
| 270 | |||
| 271 | |||
| 272 | public function setLinks($links) { |
||
| 277 | |||
| 278 | public function getLinks() { |
||
| 281 | |||
| 282 | |||
| 283 | // public function getRemote() { |
||
| 284 | // return $this->remote; |
||
| 285 | // } |
||
| 286 | // |
||
| 287 | // public function addRemote($link) { |
||
| 288 | // array_push($this->remote, $link); |
||
| 289 | // } |
||
| 290 | // |
||
| 291 | // public function getRemoteFromToken($token) { |
||
| 292 | // foreach ($this->links AS $link) { |
||
| 293 | // if ($link->getToken() === $token) { |
||
| 294 | // return $link; |
||
| 295 | // } |
||
| 296 | // } |
||
| 297 | // |
||
| 298 | // return null; |
||
| 299 | // } |
||
| 300 | // |
||
| 301 | // public function getRemoteFromAddressAndId($address, $id) { |
||
| 302 | // foreach ($this->links AS $link) { |
||
| 303 | // if ($link->getAddress() === $address && $link->getUniqueId() === $id) { |
||
| 304 | // return $link; |
||
| 305 | // } |
||
| 306 | // } |
||
| 307 | // |
||
| 308 | // return null; |
||
| 309 | // } |
||
| 310 | |||
| 311 | |||
| 312 | public static function typeInt($type) { |
||
| 331 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..