Complex classes like Encoder 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 Encoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 38 | class Encoder |
||
| 39 | { |
||
| 40 | use DefaultTargetSerializeTrait; |
||
| 41 | |||
| 42 | /** Type index */ |
||
| 43 | const TYPE = 0; |
||
| 44 | |||
| 45 | /** Encoded type */ |
||
| 46 | const TYPE_TARGET = 0; |
||
| 47 | |||
| 48 | /** Encoded type */ |
||
| 49 | const TYPE_RULE = self::TYPE_TARGET + 1; |
||
| 50 | |||
| 51 | /** Encoded type */ |
||
| 52 | const TYPE_POLICY = self::TYPE_RULE + 1; |
||
| 53 | |||
| 54 | /** Encoded type */ |
||
| 55 | const TYPE_POLICY_SET = self::TYPE_POLICY + 1; |
||
| 56 | |||
| 57 | /** Encoded type */ |
||
| 58 | const TYPE_RULES = self::TYPE_POLICY_SET + 1; |
||
| 59 | |||
| 60 | /** Encoded type */ |
||
| 61 | const TYPE_POLICIES_AND_SETS = self::TYPE_RULES + 1; |
||
| 62 | |||
| 63 | /** Rule index */ |
||
| 64 | const TARGET_NAME = self::TYPE + 1; |
||
| 65 | |||
| 66 | /** Rule index */ |
||
| 67 | const TARGET_ANY_OFS = self::TARGET_NAME + 1; |
||
| 68 | |||
| 69 | /** Rule index */ |
||
| 70 | const RULE_NAME = self::TYPE + 1; |
||
| 71 | |||
| 72 | /** Rule index */ |
||
| 73 | const RULE_TARGET = self::RULE_NAME + 1; |
||
| 74 | |||
| 75 | /** Rule index */ |
||
| 76 | const RULE_CONDITION = self::RULE_TARGET + 1; |
||
| 77 | |||
| 78 | /** Rule index */ |
||
| 79 | const RULE_EFFECT = self::RULE_CONDITION + 1; |
||
| 80 | |||
| 81 | /** Rule index */ |
||
| 82 | const RULE_OBLIGATIONS = self::RULE_EFFECT + 1; |
||
| 83 | |||
| 84 | /** Rule index */ |
||
| 85 | const RULE_ADVICE = self::RULE_OBLIGATIONS + 1; |
||
| 86 | |||
| 87 | /** Policy index */ |
||
| 88 | const POLICY_NAME = self::TYPE + 1; |
||
| 89 | |||
| 90 | /** Policy index */ |
||
| 91 | const POLICY_TARGET = self::POLICY_NAME + 1; |
||
| 92 | |||
| 93 | /** Policy index */ |
||
| 94 | const POLICY_OBLIGATIONS = self::POLICY_TARGET + 1; |
||
| 95 | |||
| 96 | /** Policy index */ |
||
| 97 | const POLICY_ADVICE = self::POLICY_OBLIGATIONS + 1; |
||
| 98 | |||
| 99 | /** Policy index */ |
||
| 100 | const POLICY_RULES = self::POLICY_ADVICE + 1; |
||
| 101 | |||
| 102 | /** Policy index */ |
||
| 103 | const POLICY_SET_NAME = self::TYPE + 1; |
||
| 104 | |||
| 105 | /** Policy index */ |
||
| 106 | const POLICY_SET_TARGET = self::POLICY_SET_NAME + 1; |
||
| 107 | |||
| 108 | /** Policy index */ |
||
| 109 | const POLICY_SET_OBLIGATIONS = self::POLICY_SET_TARGET + 1; |
||
| 110 | |||
| 111 | /** Policy index */ |
||
| 112 | const POLICY_SET_ADVICE = self::POLICY_SET_OBLIGATIONS + 1; |
||
| 113 | |||
| 114 | /** Policy index */ |
||
| 115 | const POLICY_SET_CHILDREN = self::POLICY_SET_ADVICE + 1; |
||
| 116 | |||
| 117 | /** Rules index */ |
||
| 118 | const RULES_DATA = self::TYPE + 1; |
||
| 119 | |||
| 120 | /** Rules index */ |
||
| 121 | const POLICIES_AND_SETS_DATA = self::TYPE + 1; |
||
| 122 | |||
| 123 | /** |
||
| 124 | 36 | * @param RuleInterface $rule |
|
| 125 | * |
||
| 126 | * @return array |
||
| 127 | 36 | */ |
|
| 128 | 36 | public static function encodeRule(RuleInterface $rule): array |
|
| 142 | |||
| 143 | /** |
||
| 144 | 24 | * @param PolicyInterface $policy |
|
| 145 | * |
||
| 146 | * @return array |
||
| 147 | 24 | */ |
|
| 148 | 24 | public static function encodePolicy(PolicyInterface $policy): array |
|
| 161 | |||
| 162 | /** |
||
| 163 | 13 | * @param PolicySetInterface $set |
|
| 164 | * |
||
| 165 | 13 | * @return array |
|
| 166 | 13 | */ |
|
| 167 | public static function encodePolicySet(PolicySetInterface $set): array |
||
| 182 | |||
| 183 | /** |
||
| 184 | 34 | * @param array $encoded |
|
| 185 | * |
||
| 186 | 34 | * @return int |
|
| 187 | */ |
||
| 188 | 34 | public static function getType(array $encoded): int |
|
| 194 | |||
| 195 | /** |
||
| 196 | 26 | * @param array $encoded |
|
| 197 | * |
||
| 198 | 26 | * @return bool |
|
| 199 | */ |
||
| 200 | public static function isTarget(array $encoded): bool |
||
| 204 | |||
| 205 | /** |
||
| 206 | 25 | * @param array $encoded |
|
| 207 | * |
||
| 208 | 25 | * @return bool |
|
| 209 | */ |
||
| 210 | public static function isRule(array $encoded): bool |
||
| 214 | |||
| 215 | /** |
||
| 216 | 22 | * @param array $encoded |
|
| 217 | * |
||
| 218 | 22 | * @return bool |
|
| 219 | */ |
||
| 220 | public static function isPolicy(array $encoded): bool |
||
| 224 | |||
| 225 | /** |
||
| 226 | 16 | * @param array $encoded |
|
| 227 | * |
||
| 228 | 16 | * @return bool |
|
| 229 | */ |
||
| 230 | 16 | public static function isPolicySet(array $encoded): bool |
|
| 238 | |||
| 239 | /** |
||
| 240 | 8 | * @param array $encodedTarget |
|
| 241 | * |
||
| 242 | 8 | * @return string|null |
|
| 243 | */ |
||
| 244 | 8 | public static function targetName(array $encodedTarget): ?string |
|
| 250 | |||
| 251 | /** |
||
| 252 | 26 | * @param array $encodedTarget |
|
| 253 | * |
||
| 254 | 26 | * @return array|null |
|
| 255 | */ |
||
| 256 | 26 | public static function targetAnyOfs(array $encodedTarget): ?array |
|
| 262 | |||
| 263 | /** |
||
| 264 | 7 | * @param array $encodedRule |
|
| 265 | * |
||
| 266 | 7 | * @return string |
|
| 267 | */ |
||
| 268 | 7 | public static function ruleName(array $encodedRule): string |
|
| 274 | |||
| 275 | /** |
||
| 276 | 1 | * @param array $encodedRule |
|
| 277 | * |
||
| 278 | 1 | * @return array |
|
| 279 | */ |
||
| 280 | 1 | public static function ruleTarget(array $encodedRule): array |
|
| 286 | |||
| 287 | /** |
||
| 288 | 21 | * @param array $encodedRule |
|
| 289 | * |
||
| 290 | 21 | * @return array|null |
|
| 291 | */ |
||
| 292 | 21 | public static function ruleEffect(array $encodedRule): ?array |
|
| 298 | |||
| 299 | /** |
||
| 300 | 20 | * @param array $encodedRule |
|
| 301 | * |
||
| 302 | 20 | * @return callable|null |
|
| 303 | */ |
||
| 304 | 20 | public static function ruleCondition(array $encodedRule): ?callable |
|
| 310 | |||
| 311 | /** |
||
| 312 | 24 | * @param array $encodedRule |
|
| 313 | * |
||
| 314 | 24 | * @return callable[] |
|
| 315 | */ |
||
| 316 | 24 | public static function ruleObligations(array $encodedRule): array |
|
| 322 | |||
| 323 | /** |
||
| 324 | 24 | * @param array $encodedRule |
|
| 325 | * |
||
| 326 | 24 | * @return callable[] |
|
| 327 | */ |
||
| 328 | 24 | public static function ruleAdvice(array $encodedRule): array |
|
| 334 | |||
| 335 | /** |
||
| 336 | 14 | * @param array $encodedPolicy |
|
| 337 | * |
||
| 338 | 14 | * @return string|null |
|
| 339 | */ |
||
| 340 | 14 | public static function policyName(array $encodedPolicy): ?string |
|
| 346 | |||
| 347 | /** |
||
| 348 | 1 | * @param array $encodedPolicy |
|
| 349 | * |
||
| 350 | 1 | * @return array |
|
| 351 | */ |
||
| 352 | 1 | public static function policyTarget(array $encodedPolicy): array |
|
| 358 | |||
| 359 | /** |
||
| 360 | 13 | * @param array $encodedPolicy |
|
| 361 | * |
||
| 362 | 13 | * @return callable[] |
|
| 363 | */ |
||
| 364 | 13 | public static function policyObligations(array $encodedPolicy): array |
|
| 370 | |||
| 371 | /** |
||
| 372 | 13 | * @param array $encodedPolicy |
|
| 373 | * |
||
| 374 | 13 | * @return callable[] |
|
| 375 | */ |
||
| 376 | 13 | public static function policyAdvice(array $encodedPolicy): array |
|
| 382 | |||
| 383 | /** |
||
| 384 | 19 | * @param array $encodedPolicy |
|
| 385 | * |
||
| 386 | 19 | * @return array |
|
| 387 | */ |
||
| 388 | 19 | public static function policyRules(array $encodedPolicy): array |
|
| 394 | |||
| 395 | /** |
||
| 396 | 8 | * @param array $encodedPolicySet |
|
| 397 | * |
||
| 398 | 8 | * @return string |
|
| 399 | */ |
||
| 400 | 8 | public static function policySetName(array $encodedPolicySet): string |
|
| 406 | |||
| 407 | /** |
||
| 408 | 8 | * @param array $encodedPolicySet |
|
| 409 | * |
||
| 410 | 8 | * @return array |
|
| 411 | */ |
||
| 412 | 8 | public static function policySetTarget(array $encodedPolicySet): array |
|
| 418 | |||
| 419 | /** |
||
| 420 | 10 | * @param array $encodedPolicySet |
|
| 421 | * |
||
| 422 | 10 | * @return callable[] |
|
| 423 | */ |
||
| 424 | 10 | public static function policySetObligations(array $encodedPolicySet): array |
|
| 430 | |||
| 431 | /** |
||
| 432 | 10 | * @param array $encodedPolicySet |
|
| 433 | * |
||
| 434 | 10 | * @return callable[] |
|
| 435 | */ |
||
| 436 | 10 | public static function policySetAdvice(array $encodedPolicySet): array |
|
| 442 | |||
| 443 | /** |
||
| 444 | 11 | * @param array $encodedPolicySet |
|
| 445 | * |
||
| 446 | 11 | * @return array |
|
| 447 | */ |
||
| 448 | 11 | public static function policySetChildren(array $encodedPolicySet): array |
|
| 454 | |||
| 455 | /** |
||
| 456 | 19 | * @param array $rules |
|
| 457 | * |
||
| 458 | 19 | * @return array |
|
| 459 | */ |
||
| 460 | 19 | public static function rulesData(array $rules): array |
|
| 466 | |||
| 467 | /** |
||
| 468 | 11 | * @param array $policesAndSets |
|
| 469 | * |
||
| 470 | 11 | * @return array |
|
| 471 | */ |
||
| 472 | 11 | public static function policiesAndSetsData(array $policesAndSets): array |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @param int $evaluation |
||
| 481 | 26 | * @param array $obligations |
|
| 482 | * |
||
| 483 | 26 | * @return array |
|
| 484 | */ |
||
| 485 | 26 | public static function getFulfillObligations(int $evaluation, array $obligations): array |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @param int $evaluation |
||
| 494 | 26 | * @param array $advice |
|
| 495 | * |
||
| 496 | 26 | * @return array |
|
| 497 | */ |
||
| 498 | 26 | public static function getAppliedAdvice(int $evaluation, array $advice): array |
|
| 504 | |||
| 505 | /** |
||
| 506 | 36 | * @param MethodInterface|null $method |
|
| 507 | * |
||
| 508 | 36 | * @return callable|null |
|
| 509 | */ |
||
| 510 | private static function serializeMethod(MethodInterface $method = null): ?callable |
||
| 514 | |||
| 515 | /** |
||
| 516 | 36 | * @param ObligationInterface[] $obligations |
|
| 517 | * |
||
| 518 | 36 | * @return array |
|
| 519 | 36 | */ |
|
| 520 | private static function encodeObligations(array $obligations): array |
||
| 530 | |||
| 531 | /** |
||
| 532 | 36 | * @param AdviceInterface[] $advice |
|
| 533 | * |
||
| 534 | 36 | * @return array |
|
| 535 | 36 | */ |
|
| 536 | private static function encodeAdvice(array $advice): array |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param RuleCombiningAlgorithmInterface $algorithm |
||
| 549 | 24 | * @param array $rules |
|
| 550 | * |
||
| 551 | * @return array |
||
| 552 | 24 | */ |
|
| 553 | 24 | private static function encodeRules(RuleCombiningAlgorithmInterface $algorithm, array $rules): array |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @param PolicyCombiningAlgorithmInterface $algorithm |
||
| 563 | 13 | * @param array $policiesAndSets |
|
| 564 | * |
||
| 565 | * @return array |
||
| 566 | */ |
||
| 567 | private static function serializePoliciesAndSets( |
||
| 576 | } |
||
| 577 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: