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 | /** |
|
69 | 4 | * @param StatementId|null $id |
|
70 | 4 | * @param Actor $actor |
|
71 | 4 | * @param Verb $verb |
|
72 | * @param Object $object |
||
73 | * @param Result|null $result |
||
74 | * @param Actor|null $authority |
||
75 | * @param \DateTime|null $created |
||
76 | * @param \DateTime|null $stored |
||
77 | * @param Context|null $context |
||
78 | 2 | * @param Attachment[]|null $attachments |
|
79 | */ |
||
80 | 2 | 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) |
|
93 | |||
94 | public function withId(StatementId $id = null) |
||
101 | |||
102 | public function withActor(Actor $actor) |
||
109 | |||
110 | 3 | public function withVerb(Verb $verb) |
|
117 | |||
118 | public function withObject(Object $object) |
||
125 | |||
126 | public function withResult(Result $result) |
||
133 | |||
134 | /** |
||
135 | * Creates a new Statement based on the current one containing an Authority |
||
136 | * that asserts the Statement true. |
||
137 | * |
||
138 | * @param Actor $authority The Authority asserting the Statement true |
||
139 | * |
||
140 | * @return Statement The new Statement |
||
141 | */ |
||
142 | public function withAuthority(Actor $authority) |
||
149 | |||
150 | public function withTimestamp(\DateTime $timestamp) |
||
157 | |||
158 | public function withStored(\DateTime $stored) |
||
165 | |||
166 | public function withContext(Context $context) |
||
173 | |||
174 | 2 | /** |
|
175 | * Returns the Statement's unique identifier. |
||
176 | * |
||
177 | * @return StatementId|null The identifier |
||
178 | */ |
||
179 | public function getId() |
||
183 | |||
184 | 1 | /** |
|
185 | * Returns the Statement's {@link Verb}. |
||
186 | 1 | * |
|
187 | 1 | * @return Verb The Verb |
|
188 | */ |
||
189 | 1 | public function getVerb() |
|
193 | |||
194 | /** |
||
195 | * Returns the Statement's {@link Actor}. |
||
196 | * |
||
197 | * @return Actor The Actor |
||
198 | */ |
||
199 | public function getActor() |
||
203 | |||
204 | 2 | /** |
|
205 | * Returns the Statement's {@link Object}. |
||
206 | * |
||
207 | * @return \Xabbuh\XApi\Model\Object The Object |
||
208 | */ |
||
209 | public function getObject() |
||
213 | |||
214 | /** |
||
215 | * Returns the {@link Activity} {@link Result}. |
||
216 | 2 | * |
|
217 | * @return Result The Result |
||
218 | 2 | */ |
|
219 | public function getResult() |
||
223 | |||
224 | /** |
||
225 | * Returns the Authority that asserted the Statement true. |
||
226 | 2 | * |
|
227 | * @return Actor The Authority |
||
228 | */ |
||
229 | public function getAuthority() |
||
233 | |||
234 | 2 | /** |
|
235 | * Returns the timestamp of when the events described in this statement |
||
236 | * occurred. |
||
237 | * |
||
238 | 2 | * @return \DateTime The timestamp |
|
239 | */ |
||
240 | public function getTimestamp() |
||
244 | |||
245 | /** |
||
246 | 2 | * Returns the timestamp of when the events described in this statement |
|
247 | 1 | * occurred. |
|
248 | * |
||
249 | * @return \DateTime The timestamp |
||
250 | 1 | */ |
|
251 | public function getCreated() |
||
255 | 1 | ||
256 | /** |
||
257 | * Returns the timestamp of when this statement was recorded by the LRS. |
||
258 | * |
||
259 | * @return \DateTime The timestamp |
||
260 | */ |
||
261 | public function getStored() |
||
265 | |||
266 | /** |
||
267 | * Returns the context that gives the statement more meaning. |
||
268 | * |
||
269 | * @return Context|null |
||
270 | */ |
||
271 | public function getContext() |
||
275 | |||
276 | public function getAttachments() |
||
280 | |||
281 | /** |
||
282 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
283 | * another Statement). |
||
284 | * |
||
285 | * @return bool True if the Statement voids another Statement, false otherwise |
||
286 | */ |
||
287 | public function isVoidStatement() |
||
291 | |||
292 | /** |
||
293 | * Returns a {@link StatementReference} for the Statement. |
||
294 | * |
||
295 | * @return StatementReference The reference |
||
296 | */ |
||
297 | public function getStatementReference() |
||
303 | |||
304 | /** |
||
305 | * Returns a Statement that voids the current Statement. |
||
306 | * |
||
307 | * @param Actor $actor The Actor voiding this Statement |
||
308 | * |
||
309 | * @return Statement The voiding Statement |
||
310 | */ |
||
311 | public function getVoidStatement(Actor $actor) |
||
320 | |||
321 | /** |
||
322 | * Checks if another statement is equal. |
||
323 | * |
||
324 | * Two statements are equal if and only if all of their properties are equal. |
||
325 | * |
||
326 | * @param Statement $statement The statement to compare with |
||
327 | * |
||
328 | * @return bool True if the statements are equal, false otherwise |
||
329 | */ |
||
330 | public function equals(Statement $statement) |
||
382 | } |
||
383 |
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: