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 | 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) |
|
78 | 2 | ||
79 | public function withId(StatementId $id = null) |
||
86 | |||
87 | public function withActor(Actor $actor) |
||
94 | |||
95 | public function withVerb(Verb $verb) |
||
102 | |||
103 | public function withObject(Object $object) |
||
110 | 3 | ||
111 | public function withResult(Result $result) |
||
118 | |||
119 | /** |
||
120 | * Creates a new Statement based on the current one containing an Authority |
||
121 | * that asserts the Statement true. |
||
122 | * |
||
123 | * @param Actor $authority The Authority asserting the Statement true |
||
124 | * |
||
125 | * @return Statement The new Statement |
||
126 | */ |
||
127 | public function withAuthority(Actor $authority) |
||
134 | |||
135 | public function withTimestamp(\DateTime $timestamp) |
||
142 | |||
143 | public function withStored(\DateTime $stored) |
||
150 | |||
151 | public function withContext(Context $context) |
||
158 | |||
159 | /** |
||
160 | * Returns the Statement's unique identifier. |
||
161 | * |
||
162 | * @return StatementId|null The identifier |
||
163 | */ |
||
164 | public function getId() |
||
168 | |||
169 | /** |
||
170 | 2 | * Returns the Statement's {@link Verb}. |
|
171 | * |
||
172 | 2 | * @return Verb The Verb |
|
173 | */ |
||
174 | 2 | public function getVerb() |
|
178 | |||
179 | /** |
||
180 | * Returns the Statement's {@link Actor}. |
||
181 | * |
||
182 | * @return Actor The Actor |
||
183 | */ |
||
184 | 1 | public function getActor() |
|
188 | |||
189 | 1 | /** |
|
190 | 1 | * Returns the Statement's {@link Object}. |
|
191 | * |
||
192 | * @return \Xabbuh\XApi\Model\Object The Object |
||
193 | */ |
||
194 | public function getObject() |
||
198 | |||
199 | /** |
||
200 | * Returns the {@link Activity} {@link Result}. |
||
201 | * |
||
202 | 2 | * @return Result The Result |
|
203 | */ |
||
204 | 2 | public function getResult() |
|
208 | |||
209 | /** |
||
210 | * Returns the Authority that asserted the Statement true. |
||
211 | * |
||
212 | * @return Actor The Authority |
||
213 | */ |
||
214 | public function getAuthority() |
||
218 | 2 | ||
219 | /** |
||
220 | * Returns the timestamp of when the events described in this statement |
||
221 | * occurred. |
||
222 | 2 | * |
|
223 | * @return \DateTime The timestamp |
||
224 | */ |
||
225 | public function getTimestamp() |
||
229 | |||
230 | 2 | /** |
|
231 | * Returns the timestamp of when the events described in this statement |
||
232 | * occurred. |
||
233 | * |
||
234 | 2 | * @return \DateTime The timestamp |
|
235 | */ |
||
236 | public function getCreated() |
||
240 | |||
241 | /** |
||
242 | 2 | * Returns the timestamp of when this statement was recorded by the LRS. |
|
243 | * |
||
244 | * @return \DateTime The timestamp |
||
245 | */ |
||
246 | 2 | public function getStored() |
|
250 | 1 | ||
251 | /** |
||
252 | * Returns the context that gives the statement more meaning. |
||
253 | * |
||
254 | 1 | * @return Context|null |
|
255 | 1 | */ |
|
256 | public function getContext() |
||
260 | |||
261 | /** |
||
262 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
263 | * another Statement). |
||
264 | * |
||
265 | * @return bool True if the Statement voids another Statement, false otherwise |
||
266 | */ |
||
267 | public function isVoidStatement() |
||
271 | |||
272 | /** |
||
273 | * Returns a {@link StatementReference} for the Statement. |
||
274 | * |
||
275 | * @return StatementReference The reference |
||
276 | */ |
||
277 | public function getStatementReference() |
||
283 | |||
284 | /** |
||
285 | * Returns a Statement that voids the current Statement. |
||
286 | * |
||
287 | * @param Actor $actor The Actor voiding this Statement |
||
288 | * |
||
289 | * @return Statement The voiding Statement |
||
290 | */ |
||
291 | public function getVoidStatement(Actor $actor) |
||
300 | |||
301 | /** |
||
302 | * Checks if another statement is equal. |
||
303 | * |
||
304 | * Two statements are equal if and only if all of their properties are equal. |
||
305 | * |
||
306 | * @param Statement $statement The statement to compare with |
||
307 | * |
||
308 | * @return bool True if the statements are equal, false otherwise |
||
309 | */ |
||
310 | public function equals(Statement $statement) |
||
362 | } |
||
363 |
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: