Complex classes like SubStatement 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 SubStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | final class SubStatement extends StatementObject |
||
| 20 | { |
||
| 21 | private $verb; |
||
| 22 | private $actor; |
||
| 23 | private $object; |
||
| 24 | private $result; |
||
| 25 | private $created; |
||
| 26 | private $context; |
||
| 27 | private $attachments; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param Attachment[]|null $attachments |
||
| 31 | */ |
||
| 32 | public function __construct(Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Context $context = null, \DateTime $created = null, array $attachments = null) |
||
| 46 | |||
| 47 | public function withActor(Actor $actor): self |
||
| 54 | |||
| 55 | public function withVerb(Verb $verb): self |
||
| 62 | |||
| 63 | public function withObject(StatementObject $object): self |
||
| 70 | |||
| 71 | public function withResult(Result $result): self |
||
| 78 | |||
| 79 | public function withCreated(\DateTime $created = null): self |
||
| 86 | |||
| 87 | public function withContext(Context $context): self |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param Attachment[]|null $attachments |
||
| 97 | */ |
||
| 98 | public function withAttachments(array $attachments = null): self |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the Statement's {@link Verb}. |
||
| 108 | */ |
||
| 109 | public function getVerb(): Verb |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the Statement's {@link Actor}. |
||
| 116 | */ |
||
| 117 | public function getActor(): Actor |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns the Statement's {@link StatementObject}. |
||
| 124 | */ |
||
| 125 | public function getObject(): StatementObject |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the {@link Activity} {@link Result}. |
||
| 132 | */ |
||
| 133 | public function getResult(): ?Result |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the timestamp of when the events described in this statement |
||
| 140 | * occurred. |
||
| 141 | */ |
||
| 142 | public function getCreated(): ?\DateTime |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the {@link Statement} {@link Context}. |
||
| 149 | */ |
||
| 150 | public function getContext(): ?Context |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return Attachment[]|null |
||
| 157 | */ |
||
| 158 | public function getAttachments(): ?array |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
| 165 | * another Statement). |
||
| 166 | */ |
||
| 167 | public function isVoidStatement(): bool |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function equals(StatementObject $statement): bool |
||
| 235 | } |
||
| 236 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: