Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | final class Context |
||
| 20 | { |
||
| 21 | private $registration; |
||
| 22 | private $instructor; |
||
| 23 | private $team; |
||
| 24 | private $contextActivities; |
||
| 25 | private $revision; |
||
| 26 | private $platform; |
||
| 27 | private $language; |
||
| 28 | private $statement; |
||
| 29 | private $extensions; |
||
| 30 | |||
| 31 | public function withRegistration(string $registration): self |
||
| 38 | |||
| 39 | public function withInstructor(Actor $instructor): self |
||
| 46 | |||
| 47 | public function withTeam(Group $team): self |
||
| 54 | |||
| 55 | public function withContextActivities(ContextActivities $contextActivities): self |
||
| 62 | |||
| 63 | public function withRevision(string $revision): self |
||
| 70 | |||
| 71 | public function withPlatform(string $platform): self |
||
| 78 | |||
| 79 | public function withLanguage(string $language): self |
||
| 86 | |||
| 87 | public function withStatement(StatementReference $statement): self |
||
| 94 | |||
| 95 | public function withExtensions(Extensions $extensions): self |
||
| 102 | |||
| 103 | public function getRegistration(): ?string |
||
| 107 | |||
| 108 | public function getInstructor(): ?Actor |
||
| 112 | |||
| 113 | public function getTeam(): ?Group |
||
| 117 | |||
| 118 | public function getContextActivities(): ?ContextActivities |
||
| 122 | |||
| 123 | public function getRevision(): ?string |
||
| 127 | |||
| 128 | public function getPlatform(): ?string |
||
| 132 | |||
| 133 | public function getLanguage(): ?string |
||
| 137 | |||
| 138 | public function getStatement(): ?StatementReference |
||
| 142 | |||
| 143 | public function getExtensions(): ?Extensions |
||
| 147 | |||
| 148 | public function equals(Context $context): bool |
||
| 204 | } |
||
| 205 |