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 FederatedLink 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 FederatedLink, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class FederatedLink implements \JsonSerializable { |
||
| 32 | |||
| 33 | |||
| 34 | const STATUS_ERROR = -1; |
||
| 35 | const STATUS_LINK_REMOVE = 0; |
||
| 36 | const STATUS_LINK_DOWN = 1; |
||
| 37 | const STATUS_LINK_SETUP = 2; |
||
| 38 | const STATUS_REQUEST_DECLINED = 4; |
||
| 39 | const STATUS_REQUEST_SENT = 5; |
||
| 40 | const STATUS_LINK_REQUESTED = 6; |
||
| 41 | const STATUS_LINK_UP = 9; |
||
| 42 | |||
| 43 | const SHORT_UNIQUE_ID_LENGTH = 12; |
||
| 44 | |||
| 45 | /** @var int */ |
||
| 46 | private $id; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $token; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $address; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | private $localAddress; |
||
| 56 | |||
| 57 | /** @var int */ |
||
| 58 | private $status; |
||
| 59 | |||
| 60 | /** @var int */ |
||
| 61 | private $creation; |
||
| 62 | |||
| 63 | /** @var int */ |
||
| 64 | private $circleUniqueId; |
||
| 65 | |||
| 66 | /** @var string */ |
||
| 67 | private $uniqueId = ''; |
||
| 68 | |||
| 69 | /** @var string */ |
||
| 70 | private $remoteCircleName; |
||
| 71 | |||
| 72 | /** @var string */ |
||
| 73 | private $localCircleName; |
||
| 74 | |||
| 75 | /** @var bool */ |
||
| 76 | private $fullJson = false; |
||
| 77 | |||
| 78 | public function __construct() { |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param int $id |
||
| 84 | * |
||
| 85 | * @return FederatedLink |
||
| 86 | */ |
||
| 87 | public function setId($id) { |
||
| 88 | $this->id = (int)$id; |
||
| 89 | |||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | public function getId() { |
||
| 97 | return $this->id; |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $token |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function setToken($token) { |
||
| 107 | $this->token = (string)$token; |
||
| 108 | |||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param bool $full |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getToken($full = false) { |
||
| 118 | if ($full) { |
||
| 119 | return $this->token; |
||
| 120 | } |
||
| 121 | |||
| 122 | return substr($this->token, 0, FederatedLink::SHORT_UNIQUE_ID_LENGTH); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function generateToken() { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $address |
||
| 139 | * |
||
| 140 | * @return FederatedLink |
||
| 141 | */ |
||
| 142 | public function setAddress($address) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getAddress() { |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $address |
||
| 158 | * |
||
| 159 | * @return FederatedLink |
||
| 160 | */ |
||
| 161 | public function setLocalAddress($address) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getLocalAddress() { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $circleUniqueId |
||
| 177 | * |
||
| 178 | * @return FederatedLink |
||
| 179 | */ |
||
| 180 | public function setCircleId($circleUniqueId) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param bool $full |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getCircleId($full = false) { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $uniqueId |
||
| 203 | * |
||
| 204 | * @return FederatedLink |
||
| 205 | */ |
||
| 206 | public function setUniqueId($uniqueId) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param bool $full |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getUniqueId($full = false) { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $circleName |
||
| 228 | * |
||
| 229 | * @return FederatedLink |
||
| 230 | */ |
||
| 231 | public function setRemoteCircleName($circleName) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getRemoteCircleName() { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $circleName |
||
| 247 | * |
||
| 248 | * @return FederatedLink |
||
| 249 | */ |
||
| 250 | public function setCircleName($circleName) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getCircleName() { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param int $status |
||
| 266 | * |
||
| 267 | * @return FederatedLink |
||
| 268 | */ |
||
| 269 | public function setStatus($status) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return int |
||
| 277 | */ |
||
| 278 | public function getStatus() { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @param int $creation |
||
| 285 | * |
||
| 286 | * @return FederatedLink |
||
| 287 | */ |
||
| 288 | public function setCreation($creation) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return int |
||
| 300 | */ |
||
| 301 | public function getCreation() { |
||
| 304 | |||
| 305 | |||
| 306 | public function hasToBeValidStatusUpdate($status) { |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $status |
||
| 321 | * |
||
| 322 | * @throws FederatedCircleStatusUpdateException |
||
| 323 | */ |
||
| 324 | private function hasToBeValidStatusUpdateWhileLinkDown($status) { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param $status |
||
| 338 | * |
||
| 339 | * @throws FederatedCircleStatusUpdateException |
||
| 340 | */ |
||
| 341 | View Code Duplication | private function hasToBeValidStatusUpdateWhileRequestDeclined($status) { |
|
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * @param $status |
||
| 355 | * |
||
| 356 | * @throws FederatedCircleStatusUpdateException |
||
| 357 | */ |
||
| 358 | View Code Duplication | private function hasToBeValidStatusUpdateWhileLinkRequested($status) { |
|
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * @param $status |
||
| 371 | * |
||
| 372 | * @throws FederatedCircleStatusUpdateException |
||
| 373 | */ |
||
| 374 | View Code Duplication | private function hasToBeValidStatusUpdateWhileRequestSent($status) { |
|
| 385 | |||
| 386 | |||
| 387 | public function jsonSerialize() { |
||
| 398 | |||
| 399 | |||
| 400 | public function getJson($full = false) { |
||
| 407 | |||
| 408 | |||
| 409 | public static function fromArray($arr) { |
||
| 426 | |||
| 427 | |||
| 428 | public static function fromJSON($json) { |
||
| 431 | |||
| 432 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.