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 | /** |
||
22 | * @var StatementId|null The unique identifier |
||
23 | */ |
||
24 | private $id; |
||
25 | |||
26 | /** |
||
27 | * @var Verb $verb The {@link Verb} |
||
28 | */ |
||
29 | private $verb; |
||
30 | |||
31 | /** |
||
32 | * @var Actor The {@link Actor} |
||
33 | */ |
||
34 | private $actor; |
||
35 | |||
36 | /** |
||
37 | * @var Object The {@link Object} |
||
38 | */ |
||
39 | private $object; |
||
40 | |||
41 | /** |
||
42 | * @var Result The {@link Activity} {@link Result} |
||
43 | */ |
||
44 | private $result; |
||
45 | |||
46 | /** |
||
47 | * @var Actor The Authority that asserted the Statement true |
||
48 | */ |
||
49 | private $authority; |
||
50 | |||
51 | /** |
||
52 | * @var \DateTime The timestamp of when the events described in this statement occurred |
||
53 | */ |
||
54 | private $created; |
||
55 | |||
56 | /** |
||
57 | * @var \DateTime The timestamp of when this statement was recorded by the LRS |
||
58 | */ |
||
59 | private $stored; |
||
60 | |||
61 | 4 | /** |
|
62 | * @var Context|null A context giving the statement more meaning |
||
63 | 4 | */ |
|
64 | 4 | private $context; |
|
65 | 4 | ||
66 | 4 | private $attachments; |
|
67 | 4 | ||
68 | 4 | private $version; |
|
69 | 4 | ||
70 | 4 | /** |
|
71 | 4 | * @param StatementId|null $id |
|
72 | * @param Actor $actor |
||
73 | * @param Verb $verb |
||
74 | * @param Object $object |
||
75 | * @param Result|null $result |
||
76 | * @param Actor|null $authority |
||
77 | * @param \DateTime|null $created |
||
78 | 2 | * @param \DateTime|null $stored |
|
79 | * @param Context|null $context |
||
80 | 2 | * @param Attachment[]|null $attachments |
|
81 | * @param string $version |
||
82 | */ |
||
83 | public function __construct(StatementId $id = null, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null, array $attachments = null, $version = '1.0.0') |
||
97 | |||
98 | 3 | public function withId(StatementId $id = null) |
|
105 | |||
106 | public function withActor(Actor $actor) |
||
113 | |||
114 | public function withVerb(Verb $verb) |
||
121 | |||
122 | public function withObject(Object $object) |
||
129 | |||
130 | 2 | public function withResult(Result $result = null) |
|
137 | |||
138 | /** |
||
139 | * Creates a new Statement based on the current one containing an Authority |
||
140 | * that asserts the Statement true. |
||
141 | * |
||
142 | * @param Actor $authority The Authority asserting the Statement true |
||
143 | * |
||
144 | * @return Statement The new Statement |
||
145 | */ |
||
146 | public function withAuthority(Actor $authority = null) |
||
153 | |||
154 | /** |
||
155 | * @deprecated since 1.2, to be removed in 3.0 |
||
156 | */ |
||
157 | public function withTimestamp(\DateTime $timestamp = null) |
||
166 | |||
167 | public function withCreated(\DateTime $created = null) |
||
174 | 2 | ||
175 | public function withStored(\DateTime $stored = null) |
||
182 | |||
183 | public function withContext(Context $context = null) |
||
190 | 1 | ||
191 | /** |
||
192 | * @param Attachment[]|null $attachments |
||
193 | * |
||
194 | * @return self |
||
195 | */ |
||
196 | public function withAttachments(array $attachments = null) |
||
203 | |||
204 | 2 | /** |
|
205 | * @param string $version |
||
206 | * |
||
207 | * @return self |
||
208 | */ |
||
209 | public function withVersion($version) |
||
216 | 2 | ||
217 | /** |
||
218 | 2 | * Returns the Statement's unique identifier. |
|
219 | * |
||
220 | * @return StatementId|null The identifier |
||
221 | */ |
||
222 | 2 | public function getId() |
|
226 | 2 | ||
227 | /** |
||
228 | * Returns the Statement's {@link Verb}. |
||
229 | * |
||
230 | 2 | * @return Verb The Verb |
|
231 | */ |
||
232 | public function getVerb() |
||
236 | |||
237 | /** |
||
238 | 2 | * Returns the Statement's {@link Actor}. |
|
239 | * |
||
240 | * @return Actor The Actor |
||
241 | */ |
||
242 | 2 | public function getActor() |
|
246 | 2 | ||
247 | 1 | /** |
|
248 | * Returns the Statement's {@link Object}. |
||
249 | * |
||
250 | 1 | * @return \Xabbuh\XApi\Model\Object The Object |
|
251 | */ |
||
252 | public function getObject() |
||
256 | |||
257 | /** |
||
258 | * Returns the {@link Activity} {@link Result}. |
||
259 | * |
||
260 | * @return Result The Result |
||
261 | */ |
||
262 | public function getResult() |
||
266 | |||
267 | /** |
||
268 | * Returns the Authority that asserted the Statement true. |
||
269 | * |
||
270 | * @return Actor The Authority |
||
271 | */ |
||
272 | public function getAuthority() |
||
276 | |||
277 | /** |
||
278 | * Returns the timestamp of when the events described in this statement |
||
279 | * occurred. |
||
280 | * |
||
281 | * @return \DateTime The timestamp |
||
282 | * |
||
283 | * @deprecated since 1.2, to be removed in 3.0 |
||
284 | */ |
||
285 | public function getTimestamp() |
||
291 | |||
292 | /** |
||
293 | * Returns the timestamp of when the events described in this statement |
||
294 | * occurred. |
||
295 | * |
||
296 | * @return \DateTime The timestamp |
||
297 | */ |
||
298 | public function getCreated() |
||
302 | |||
303 | /** |
||
304 | * Returns the timestamp of when this statement was recorded by the LRS. |
||
305 | * |
||
306 | * @return \DateTime The timestamp |
||
307 | */ |
||
308 | public function getStored() |
||
312 | |||
313 | /** |
||
314 | * Returns the context that gives the statement more meaning. |
||
315 | * |
||
316 | * @return Context|null |
||
317 | */ |
||
318 | public function getContext() |
||
322 | |||
323 | public function getAttachments() |
||
327 | |||
328 | public function getVersion() |
||
332 | |||
333 | /** |
||
334 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
335 | * another Statement). |
||
336 | * |
||
337 | * @return bool True if the Statement voids another Statement, false otherwise |
||
338 | */ |
||
339 | public function isVoidStatement() |
||
343 | |||
344 | /** |
||
345 | * Returns a {@link StatementReference} for the Statement. |
||
346 | * |
||
347 | * @return StatementReference The reference |
||
348 | */ |
||
349 | public function getStatementReference() |
||
355 | |||
356 | /** |
||
357 | * Returns a Statement that voids the current Statement. |
||
358 | * |
||
359 | * @param Actor $actor The Actor voiding this Statement |
||
360 | * |
||
361 | * @return Statement The voiding Statement |
||
362 | */ |
||
363 | public function getVoidStatement(Actor $actor) |
||
372 | |||
373 | /** |
||
374 | * Checks if another statement is equal. |
||
375 | * |
||
376 | * Two statements are equal if and only if all of their properties are equal. |
||
377 | * |
||
378 | * @param Statement $statement The statement to compare with |
||
379 | * |
||
380 | * @return bool True if the statements are equal, false otherwise |
||
381 | */ |
||
382 | public function equals(Statement $statement) |
||
458 | } |
||
459 |
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: