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 |
||
| 48 | class RemoteEvent implements JsonSerializable { |
||
| 49 | |||
| 50 | |||
| 51 | const SEVERITY_LOW = 1; |
||
| 52 | const SEVERITY_HIGH = 3; |
||
| 53 | |||
| 54 | const BYPASS_LOCALCIRCLECHECK = 1; |
||
| 55 | const BYPASS_VIEWERCHECK = 2; |
||
| 56 | |||
| 57 | |||
| 58 | use TArrayTools; |
||
| 59 | |||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $class; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $source = ''; |
||
| 66 | |||
| 67 | /** @var Circle */ |
||
| 68 | private $circle; |
||
| 69 | |||
| 70 | /** @var Member */ |
||
| 71 | private $member; |
||
| 72 | |||
| 73 | /** @var SimpleDataStore */ |
||
| 74 | private $data; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | private $severity = self::SEVERITY_LOW; |
||
| 78 | |||
| 79 | /** @var SimpleDataStore */ |
||
| 80 | private $result; |
||
| 81 | |||
| 82 | /** @var bool */ |
||
| 83 | private $async = false; |
||
| 84 | |||
| 85 | /** @var string */ |
||
| 86 | private $incomingOrigin = ''; |
||
| 87 | |||
| 88 | |||
| 89 | /** @var string */ |
||
| 90 | private $wrapperToken = ''; |
||
| 91 | |||
| 92 | /** @var bool */ |
||
| 93 | private $verifiedViewer = false; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | private $verifiedCircle = false; |
||
| 97 | |||
| 98 | /** @var int */ |
||
| 99 | private $bypass = 0; |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * RemoteEvent constructor. |
||
| 104 | * |
||
| 105 | * @param string $class |
||
| 106 | */ |
||
| 107 | function __construct(string $class = '') { |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getClass(): string { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param mixed $class |
||
| 123 | * |
||
| 124 | * @return self |
||
| 125 | */ |
||
| 126 | public function setClass($class): self { |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getSource(): string { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $source |
||
| 142 | * |
||
| 143 | * @return self |
||
| 144 | */ |
||
| 145 | public function setSource(string $source): self { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function isAsync(): bool { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param bool $async |
||
| 176 | * |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | public function setAsync(bool $async): self { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $incomingOrigin |
||
| 187 | * |
||
| 188 | * @return self |
||
| 189 | */ |
||
| 190 | public function setIncomingOrigin(string $incomingOrigin): self { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getIncomingOrigin(): string { |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @param bool $verifiedViewer |
||
| 206 | * |
||
| 207 | * @return RemoteEvent |
||
| 208 | */ |
||
| 209 | public function setVerifiedViewer(bool $verifiedViewer): self { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function isVerifiedViewer(): bool { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @throws ViewerNotConfirmedException |
||
| 224 | */ |
||
| 225 | public function confirmVerifiedViewer(): void { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param bool $verifiedCircle |
||
| 236 | * |
||
| 237 | * @return RemoteEvent |
||
| 238 | */ |
||
| 239 | public function setVerifiedCircle(bool $verifiedCircle): self { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function isVerifiedCircle(): bool { |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $wrapperToken |
||
| 255 | * |
||
| 256 | * @return RemoteEvent |
||
| 257 | */ |
||
| 258 | public function setWrapperToken(string $wrapperToken): self { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getWrapperToken(): string { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function hasCircle(): bool { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param Circle $circle |
||
| 281 | * |
||
| 282 | * @return self |
||
| 283 | */ |
||
| 284 | public function setCircle(Circle $circle): self { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return Circle |
||
| 292 | */ |
||
| 293 | public function getCircle(): Circle { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @return Member |
||
| 300 | */ |
||
| 301 | public function getMember(): Member { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param Member $member |
||
| 307 | * |
||
| 308 | * @return self |
||
| 309 | */ |
||
| 310 | public function setMember(Member $member): self { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function hasMember(): bool { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * @param SimpleDataStore $data |
||
| 326 | * |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function setData(SimpleDataStore $data): self { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return SimpleDataStore |
||
| 337 | */ |
||
| 338 | public function getData(): SimpleDataStore { |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | public function getSeverity(): int { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param int $severity |
||
| 352 | * |
||
| 353 | * @return self |
||
| 354 | */ |
||
| 355 | public function setSeverity(int $severity): self { |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * @return SimpleDataStore |
||
| 364 | */ |
||
| 365 | public function getResult(): SimpleDataStore { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param SimpleDataStore $result |
||
| 371 | * |
||
| 372 | * @return self |
||
| 373 | */ |
||
| 374 | public function setResult(SimpleDataStore $result): self { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param array $data |
||
| 383 | * |
||
| 384 | * @return self |
||
| 385 | */ |
||
| 386 | public function import(array $data): self { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | function jsonSerialize(): array { |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * @param int $flag |
||
| 436 | * |
||
| 437 | * @return RemoteEvent |
||
| 438 | */ |
||
| 439 | public function bypass(int $flag): self { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param int $flag |
||
| 449 | * |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function canBypass(int $flag): bool { |
||
| 455 | |||
| 456 | } |
||
| 457 | |||
| 458 |
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.