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 RemoteEvent 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 RemoteEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class RemoteEvent implements JsonSerializable { |
||
| 48 | |||
| 49 | |||
| 50 | const SEVERITY_LOW = 1; |
||
| 51 | const SEVERITY_HIGH = 3; |
||
| 52 | |||
| 53 | const BYPASS_LOCALCIRCLECHECK = 1; |
||
| 54 | |||
| 55 | |||
| 56 | use TArrayTools; |
||
| 57 | |||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $class; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | private $source = ''; |
||
| 64 | |||
| 65 | /** @var Circle */ |
||
| 66 | private $circle; |
||
| 67 | |||
| 68 | /** @var Member */ |
||
| 69 | private $member; |
||
| 70 | |||
| 71 | /** @var SimpleDataStore */ |
||
| 72 | private $data; |
||
| 73 | |||
| 74 | /** @var int */ |
||
| 75 | private $severity = self::SEVERITY_LOW; |
||
| 76 | |||
| 77 | /** @var SimpleDataStore */ |
||
| 78 | private $result; |
||
| 79 | |||
| 80 | /** @var bool */ |
||
| 81 | private $local; |
||
| 82 | |||
| 83 | /** @var bool */ |
||
| 84 | private $async = false; |
||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | private $incomingOrigin = ''; |
||
| 88 | |||
| 89 | |||
| 90 | /** @var string */ |
||
| 91 | private $wrapperToken = ''; |
||
| 92 | |||
| 93 | /** @var bool */ |
||
| 94 | private $verifiedViewer = false; |
||
| 95 | |||
| 96 | /** @var bool */ |
||
| 97 | private $verifiedCircle = false; |
||
| 98 | |||
| 99 | /** @var int */ |
||
| 100 | private $bypass = 0; |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * RemoteEvent constructor. |
||
| 105 | * |
||
| 106 | * @param string $class |
||
| 107 | * @param bool $local |
||
| 108 | */ |
||
| 109 | View Code Duplication | function __construct(string $class = '', bool $local = false) { |
|
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getClass(): string { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param mixed $class |
||
| 126 | * |
||
| 127 | * @return self |
||
| 128 | */ |
||
| 129 | public function setClass($class): self { |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getSource(): string { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $source |
||
| 145 | * |
||
| 146 | * @return self |
||
| 147 | */ |
||
| 148 | public function setSource(string $source): self { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function isLocal(): bool { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param bool $local |
||
| 179 | * |
||
| 180 | * @return self |
||
| 181 | */ |
||
| 182 | public function setLocal(bool $local): self { |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function isAsync(): bool { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param bool $async |
||
| 198 | * |
||
| 199 | * @return self |
||
| 200 | */ |
||
| 201 | public function setAsync(bool $async): self { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $incomingOrigin |
||
| 209 | * |
||
| 210 | * @return self |
||
| 211 | */ |
||
| 212 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getIncomingOrigin(): string { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param bool $verifiedViewer |
||
| 228 | * |
||
| 229 | * @return RemoteEvent |
||
| 230 | */ |
||
| 231 | public function setVerifiedViewer(bool $verifiedViewer): self { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function isVerifiedViewer(): bool { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * @param bool $verifiedCircle |
||
| 247 | * |
||
| 248 | * @return RemoteEvent |
||
| 249 | */ |
||
| 250 | public function setVerifiedCircle(bool $verifiedCircle): self { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function isVerifiedCircle(): bool { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $wrapperToken |
||
| 266 | * |
||
| 267 | * @return RemoteEvent |
||
| 268 | */ |
||
| 269 | public function setWrapperToken(string $wrapperToken): self { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getWrapperToken(): string { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function hasCircle(): bool { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param Circle $circle |
||
| 292 | * |
||
| 293 | * @return self |
||
| 294 | */ |
||
| 295 | public function setCircle(Circle $circle): self { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return Circle |
||
| 303 | */ |
||
| 304 | public function getCircle(): Circle { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * @return Member |
||
| 311 | */ |
||
| 312 | public function getMember(): Member { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param Member $member |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | public function setMember(Member $member): self { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function hasMember(): bool { |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * @param SimpleDataStore $data |
||
| 337 | * |
||
| 338 | * @return self |
||
| 339 | */ |
||
| 340 | public function setData(SimpleDataStore $data): self { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return SimpleDataStore |
||
| 348 | */ |
||
| 349 | public function getData(): SimpleDataStore { |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * @return int |
||
| 356 | */ |
||
| 357 | public function getSeverity(): int { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param int $severity |
||
| 363 | * |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | public function setSeverity(int $severity): self { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * @return SimpleDataStore |
||
| 375 | */ |
||
| 376 | public function getResult(): SimpleDataStore { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param SimpleDataStore $result |
||
| 382 | * |
||
| 383 | * @return self |
||
| 384 | */ |
||
| 385 | public function setResult(SimpleDataStore $result): self { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @param array $data |
||
| 394 | * |
||
| 395 | * @return self |
||
| 396 | */ |
||
| 397 | public function import(array $data): self { |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | function jsonSerialize(): array { |
||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * @param int $flag |
||
| 447 | * |
||
| 448 | * @return RemoteEvent |
||
| 449 | */ |
||
| 450 | public function bypass(int $flag): self { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param int $flag |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | public function canBypass(int $flag): bool { |
||
| 466 | |||
| 467 | } |
||
| 468 | |||
| 469 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.