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 | /** @var int */ |
||
| 44 | private $id; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | private $token; |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | private $address; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $localAddress; |
||
| 54 | |||
| 55 | /** @var int */ |
||
| 56 | private $status; |
||
| 57 | |||
| 58 | /** @var int */ |
||
| 59 | private $creation; |
||
| 60 | |||
| 61 | /** @var int */ |
||
| 62 | private $circleId; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $uniqueId = ''; |
||
| 66 | |||
| 67 | /** @var string */ |
||
| 68 | private $remoteCircleName; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | private $localCircleName; |
||
| 72 | |||
| 73 | /** @var bool */ |
||
| 74 | private $fullJson = false; |
||
| 75 | |||
| 76 | public function __construct() { |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $id |
||
| 82 | * |
||
| 83 | * @return FederatedLink |
||
| 84 | */ |
||
| 85 | public function setId($id) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return int |
||
| 93 | */ |
||
| 94 | public function getId() { |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param $token |
||
| 101 | * |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | public function setToken($token) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param bool $full |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getToken($full = false) { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function generateToken() { |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $address |
||
| 137 | * |
||
| 138 | * @return FederatedLink |
||
| 139 | */ |
||
| 140 | public function setAddress($address) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function getAddress() { |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $address |
||
| 156 | * |
||
| 157 | * @return FederatedLink |
||
| 158 | */ |
||
| 159 | public function setLocalAddress($address) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getLocalAddress() { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $circleId |
||
| 175 | * |
||
| 176 | * @return FederatedLink |
||
| 177 | */ |
||
| 178 | public function setCircleId($circleId) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | public function getCircleId() { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $uniqueId |
||
| 194 | * |
||
| 195 | * @return FederatedLink |
||
| 196 | */ |
||
| 197 | public function setUniqueId($uniqueId) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param bool $full |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getUniqueId($full = false) { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $circleName |
||
| 219 | * |
||
| 220 | * @return FederatedLink |
||
| 221 | */ |
||
| 222 | public function setRemoteCircleName($circleName) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getRemoteCircleName() { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $circleName |
||
| 238 | * |
||
| 239 | * @return FederatedLink |
||
| 240 | */ |
||
| 241 | public function setCircleName($circleName) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getCircleName() { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * @param int $status |
||
| 257 | * |
||
| 258 | * @return FederatedLink |
||
| 259 | */ |
||
| 260 | public function setStatus($status) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return int |
||
| 268 | */ |
||
| 269 | public function getStatus() { |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * @param int $creation |
||
| 276 | * |
||
| 277 | * @return FederatedLink |
||
| 278 | */ |
||
| 279 | public function setCreation($creation) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return int |
||
| 291 | */ |
||
| 292 | public function getCreation() { |
||
| 295 | |||
| 296 | |||
| 297 | public function hasToBeValidStatusUpdate($status) { |
||
| 329 | |||
| 330 | |||
| 331 | public function jsonSerialize() { |
||
| 342 | |||
| 343 | |||
| 344 | public function getJson($full = false) { |
||
| 351 | |||
| 352 | |||
| 353 | public static function fromArray($arr) { |
||
| 370 | |||
| 371 | |||
| 372 | public static function fromJSON($json) { |
||
| 375 | |||
| 376 | } |
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.