Complex classes like Statement 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 Statement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class Statement |
||
20 | { |
||
21 | private $id; |
||
22 | private $verb; |
||
23 | private $actor; |
||
24 | private $object; |
||
25 | private $result; |
||
26 | private $authority; |
||
27 | private $created; |
||
28 | private $stored; |
||
29 | private $context; |
||
30 | private $attachments; |
||
31 | private $version; |
||
32 | |||
33 | /** |
||
34 | * @param Attachment[]|null $attachments |
||
35 | */ |
||
36 | public function __construct(StatementId $id = null, Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null, array $attachments = null, string $version = null) |
||
50 | |||
51 | public function withId(StatementId $id = null): self |
||
58 | |||
59 | public function withActor(Actor $actor): self |
||
66 | 4 | ||
67 | 4 | public function withVerb(Verb $verb): self |
|
74 | |||
75 | public function withObject(StatementObject $object): self |
||
82 | |||
83 | public function withResult(Result $result = null): self |
||
90 | 3 | ||
91 | /** |
||
92 | * Creates a new Statement based on the current one containing an Authority |
||
93 | * that asserts the Statement true. |
||
94 | */ |
||
95 | public function withAuthority(Actor $authority = null): self |
||
102 | |||
103 | public function withCreated(\DateTime $created = null): self |
||
110 | 3 | ||
111 | public function withStored(\DateTime $stored = null): self |
||
118 | |||
119 | public function withContext(Context $context = null): self |
||
126 | |||
127 | /** |
||
128 | 2 | * @param Attachment[]|null $attachments |
|
129 | */ |
||
130 | 2 | public function withAttachments(array $attachments = null): self |
|
137 | |||
138 | public function withVersion(string $version = null): self |
||
145 | |||
146 | /** |
||
147 | * Returns the Statement's unique identifier. |
||
148 | */ |
||
149 | public function getId(): ?StatementId |
||
153 | |||
154 | /** |
||
155 | * Returns the Statement's {@link Verb}. |
||
156 | */ |
||
157 | public function getVerb(): Verb |
||
161 | |||
162 | /** |
||
163 | * Returns the Statement's {@link Actor}. |
||
164 | */ |
||
165 | public function getActor(): Actor |
||
169 | |||
170 | 2 | /** |
|
171 | * Returns the Statement's {@link StatementObject}. |
||
172 | 2 | */ |
|
173 | public function getObject(): StatementObject |
||
177 | |||
178 | /** |
||
179 | * Returns the {@link Activity} {@link Result}. |
||
180 | */ |
||
181 | public function getResult(): ?Result |
||
185 | |||
186 | 1 | /** |
|
187 | 1 | * Returns the Authority that asserted the Statement true. |
|
188 | */ |
||
189 | 1 | public function getAuthority(): ?Actor |
|
193 | |||
194 | /** |
||
195 | * Returns the timestamp of when the events described in this statement |
||
196 | * occurred. |
||
197 | */ |
||
198 | public function getCreated(): ?\DateTime |
||
202 | 2 | ||
203 | /** |
||
204 | 2 | * Returns the timestamp of when this statement was recorded by the LRS. |
|
205 | */ |
||
206 | public function getStored(): ?\DateTime |
||
210 | |||
211 | /** |
||
212 | * Returns the context that gives the statement more meaning. |
||
213 | */ |
||
214 | public function getContext(): ?Context |
||
218 | 2 | ||
219 | /** |
||
220 | * @return Attachment[]|null |
||
221 | */ |
||
222 | 2 | public function getAttachments(): ?array |
|
226 | 2 | ||
227 | public function getVersion(): ?string |
||
231 | |||
232 | /** |
||
233 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
234 | 2 | * another Statement). |
|
235 | */ |
||
236 | public function isVoidStatement(): bool |
||
240 | |||
241 | /** |
||
242 | 2 | * Returns a {@link StatementReference} for the Statement. |
|
243 | */ |
||
244 | public function getStatementReference(): StatementReference |
||
248 | |||
249 | /** |
||
250 | 1 | * Returns a Statement that voids the current Statement. |
|
251 | */ |
||
252 | public function getVoidStatement(Actor $actor): self |
||
261 | |||
262 | /** |
||
263 | * Checks if another statement is equal. |
||
264 | * |
||
265 | * Two statements are equal if and only if all of their properties are equal. |
||
266 | */ |
||
267 | public function equals(Statement $statement): bool |
||
343 | } |
||
344 |
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: