Complex classes like Token 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 Token, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 33 | abstract class Token implements TokenInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | abstract protected function getDbDateFormat(): string; |
||
| 39 | |||
| 40 | /** Field name */ |
||
| 41 | const FIELD_ID = 'id_token'; |
||
| 42 | |||
| 43 | /** Field name */ |
||
| 44 | const FIELD_ID_CLIENT = Client::FIELD_ID; |
||
| 45 | |||
| 46 | /** Field name */ |
||
| 47 | const FIELD_ID_USER = 'id_user'; |
||
| 48 | |||
| 49 | /** Field name */ |
||
| 50 | const FIELD_SCOPES = 'scopes'; |
||
| 51 | |||
| 52 | /** Field name */ |
||
| 53 | const FIELD_IS_SCOPE_MODIFIED = 'is_scope_modified'; |
||
| 54 | |||
| 55 | /** Field name */ |
||
| 56 | const FIELD_IS_ENABLED = 'is_enabled'; |
||
| 57 | |||
| 58 | /** Field name */ |
||
| 59 | const FIELD_REDIRECT_URI = 'redirect_uri'; |
||
| 60 | |||
| 61 | /** Field name */ |
||
| 62 | const FIELD_CODE = 'code'; |
||
| 63 | |||
| 64 | /** Field name */ |
||
| 65 | const FIELD_VALUE = 'value'; |
||
| 66 | |||
| 67 | /** Field name */ |
||
| 68 | const FIELD_TYPE = 'type'; |
||
| 69 | |||
| 70 | /** Field name */ |
||
| 71 | const FIELD_REFRESH = 'refresh'; |
||
| 72 | |||
| 73 | /** Field name */ |
||
| 74 | const FIELD_CODE_CREATED_AT = 'code_created_at'; |
||
| 75 | |||
| 76 | /** Field name */ |
||
| 77 | const FIELD_VALUE_CREATED_AT = 'value_created_at'; |
||
| 78 | |||
| 79 | /** Field name */ |
||
| 80 | const FIELD_REFRESH_CREATED_AT = 'refresh_created_at'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var int|null |
||
| 84 | */ |
||
| 85 | private $identifierField; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $clientIdentifierField = ''; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int|string|null |
||
| 94 | */ |
||
| 95 | private $userIdentifierField; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string[] |
||
| 99 | */ |
||
| 100 | private $scopeIdentifiers = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string|null |
||
| 104 | */ |
||
| 105 | private $scopeList = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | private $isScopeModified = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | private $isEnabled = true; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string|null |
||
| 119 | */ |
||
| 120 | private $redirectUriString = null; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string|null |
||
| 124 | */ |
||
| 125 | private $codeField = null; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string|null |
||
| 129 | */ |
||
| 130 | private $valueField = null; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var string|null |
||
| 134 | */ |
||
| 135 | private $typeField = null; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var string|null |
||
| 139 | */ |
||
| 140 | private $refreshValueField = null; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var DateTimeInterface|null |
||
| 144 | */ |
||
| 145 | private $codeCreatedAtField = null; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var DateTimeInterface|null |
||
| 149 | */ |
||
| 150 | private $valueCreatedAtField = null; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var DateTimeInterface|null |
||
| 154 | */ |
||
| 155 | private $refreshCreatedAtField = null; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Constructor. |
||
| 159 | */ |
||
| 160 | 22 | public function __construct() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | 13 | public function getIdentifier(): ?int |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | */ |
||
| 189 | 13 | public function setIdentifier(int $identifier): TokenInterface |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @inheritdoc |
||
| 198 | */ |
||
| 199 | 15 | public function getClientIdentifier(): string |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | 15 | public function setClientIdentifier(string $identifier): TokenInterface |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritdoc |
||
| 216 | */ |
||
| 217 | 13 | public function getUserIdentifier() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @inheritdoc |
||
| 224 | */ |
||
| 225 | 15 | public function setUserIdentifier($identifier): TokenInterface |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | 15 | public function getScopeIdentifiers(): array |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @inheritdoc |
||
| 244 | */ |
||
| 245 | 15 | public function setScopeIdentifiers(array $identifiers): TokenInterface |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | 6 | public function getScopeList(): string |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @inheritdoc |
||
| 268 | */ |
||
| 269 | 4 | public function getRedirectUriString(): ?string |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @inheritdoc |
||
| 276 | */ |
||
| 277 | 15 | public function setRedirectUriString(?string $uri): TokenInterface |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritdoc |
||
| 286 | */ |
||
| 287 | 13 | public function isScopeModified(): bool |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @inheritdoc |
||
| 294 | */ |
||
| 295 | 8 | public function setScopeModified(): TokenInterface |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @inheritdoc |
||
| 304 | */ |
||
| 305 | 7 | public function setScopeUnmodified(): TokenInterface |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @inheritdoc |
||
| 314 | */ |
||
| 315 | 10 | public function isEnabled(): bool |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @inheritdoc |
||
| 322 | */ |
||
| 323 | 11 | public function setEnabled(): TokenInterface |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @inheritdoc |
||
| 332 | */ |
||
| 333 | 2 | public function setDisabled(): TokenInterface |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @inheritdoc |
||
| 342 | */ |
||
| 343 | 4 | public function getCode(): string |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @inheritdoc |
||
| 350 | */ |
||
| 351 | 14 | public function setCode(?string $code): TokenInterface |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @inheritdoc |
||
| 360 | */ |
||
| 361 | 14 | public function getValue(): ?string |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @inheritdoc |
||
| 368 | */ |
||
| 369 | 13 | public function setValue(?string $value): TokenInterface |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @inheritdoc |
||
| 378 | */ |
||
| 379 | 13 | public function getType(): ?string |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @inheritdoc |
||
| 386 | */ |
||
| 387 | 13 | public function setType(?string $type): TokenInterface |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @inheritdoc |
||
| 396 | */ |
||
| 397 | 14 | public function getRefreshValue(): ?string |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @inheritdoc |
||
| 404 | */ |
||
| 405 | 13 | public function setRefreshValue(?string $refreshValue): TokenInterface |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @inheritdoc |
||
| 414 | */ |
||
| 415 | 1 | public function getCodeCreatedAt(): ?DateTimeInterface |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @inheritdoc |
||
| 426 | */ |
||
| 427 | 2 | public function setCodeCreatedAt(DateTimeInterface $codeCreatedAt): TokenInterface |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @inheritdoc |
||
| 436 | */ |
||
| 437 | 2 | public function getValueCreatedAt(): ?DateTimeInterface |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @inheritdoc |
||
| 450 | */ |
||
| 451 | 9 | public function setValueCreatedAt(DateTimeInterface $valueCreatedAt): TokenInterface |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @inheritdoc |
||
| 460 | */ |
||
| 461 | 1 | public function getRefreshCreatedAt(): ?DateTimeInterface |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @inheritdoc |
||
| 474 | */ |
||
| 475 | 6 | public function setRefreshCreatedAt(DateTimeInterface $refreshCreatedAt): TokenInterface |
|
| 481 | |||
| 482 | /** |
||
| 483 | * @inheritdoc |
||
| 484 | */ |
||
| 485 | 1 | public function hasBeenUsedEarlier(): bool |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $value |
||
| 492 | * |
||
| 493 | * @return Token |
||
| 494 | */ |
||
| 495 | 12 | protected function parseIsScopeModified(string $value): Token |
|
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $value |
||
| 504 | * |
||
| 505 | * @return Token |
||
| 506 | */ |
||
| 507 | 12 | protected function parseIsEnabled(string $value): Token |
|
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $createdAt |
||
| 516 | * |
||
| 517 | * @return DateTimeInterface |
||
| 518 | * |
||
| 519 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 520 | */ |
||
| 521 | 2 | protected function parseDateTime(string $createdAt): DateTimeInterface |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @param string $name |
||
| 528 | * |
||
| 529 | * @return bool |
||
| 530 | */ |
||
| 531 | 22 | protected function hasDynamicProperty(string $name): bool |
|
| 535 | } |
||
| 536 |