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 string 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($id, Actor $actor, Verb $verb, Object $object, Result $result = null, Actor $authority = null, \DateTime $created = null, \DateTime $stored = null, Context $context = null) |
|
78 | 2 | ||
79 | /** |
||
80 | 2 | * @param string $id |
|
81 | * |
||
82 | * @return Statement |
||
83 | */ |
||
84 | public function withId($id) |
||
91 | |||
92 | public function withActor(Actor $actor) |
||
99 | |||
100 | 3 | public function withVerb(Verb $verb) |
|
107 | |||
108 | 3 | public function withObject(Object $object) |
|
115 | |||
116 | public function withResult(Result $result) |
||
123 | |||
124 | /** |
||
125 | * Creates a new Statement based on the current one containing an Authority |
||
126 | * that asserts the Statement true. |
||
127 | * |
||
128 | 2 | * @param Actor $authority The Authority asserting the Statement true |
|
129 | * |
||
130 | 2 | * @return Statement The new Statement |
|
131 | */ |
||
132 | public function withAuthority(Actor $authority) |
||
139 | |||
140 | public function withTimestamp(\DateTime $timestamp) |
||
147 | |||
148 | public function withStored(\DateTime $stored) |
||
155 | |||
156 | public function withContext(Context $context) |
||
163 | |||
164 | /** |
||
165 | * Returns the Statement's unique identifier. |
||
166 | * |
||
167 | * @return string The identifier |
||
168 | */ |
||
169 | public function getId() |
||
173 | |||
174 | 2 | /** |
|
175 | * Returns the Statement's {@link Verb}. |
||
176 | * |
||
177 | * @return Verb The Verb |
||
178 | */ |
||
179 | public function getVerb() |
||
183 | |||
184 | 1 | /** |
|
185 | * Returns the Statement's {@link Actor}. |
||
186 | 1 | * |
|
187 | 1 | * @return Actor The Actor |
|
188 | */ |
||
189 | 1 | public function getActor() |
|
193 | |||
194 | /** |
||
195 | * Returns the Statement's {@link Object}. |
||
196 | * |
||
197 | * @return \Xabbuh\XApi\Model\Object The Object |
||
198 | */ |
||
199 | public function getObject() |
||
203 | |||
204 | 2 | /** |
|
205 | * Returns the {@link Activity} {@link Result}. |
||
206 | * |
||
207 | * @return Result The Result |
||
208 | */ |
||
209 | public function getResult() |
||
213 | |||
214 | /** |
||
215 | * Returns the Authority that asserted the Statement true. |
||
216 | 2 | * |
|
217 | * @return Actor The Authority |
||
218 | 2 | */ |
|
219 | public function getAuthority() |
||
223 | |||
224 | /** |
||
225 | * Returns the timestamp of when the events described in this statement |
||
226 | 2 | * occurred. |
|
227 | * |
||
228 | * @return \DateTime The timestamp |
||
229 | */ |
||
230 | 2 | public function getTimestamp() |
|
234 | 2 | ||
235 | /** |
||
236 | * Returns the timestamp of when the events described in this statement |
||
237 | * occurred. |
||
238 | 2 | * |
|
239 | * @return \DateTime The timestamp |
||
240 | */ |
||
241 | public function getCreated() |
||
245 | |||
246 | 2 | /** |
|
247 | 1 | * Returns the timestamp of when this statement was recorded by the LRS. |
|
248 | * |
||
249 | * @return \DateTime The timestamp |
||
250 | 1 | */ |
|
251 | public function getStored() |
||
255 | 1 | ||
256 | /** |
||
257 | * Returns the context that gives the statement more meaning. |
||
258 | * |
||
259 | * @return Context|null |
||
260 | */ |
||
261 | public function getContext() |
||
265 | |||
266 | /** |
||
267 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
268 | * another Statement). |
||
269 | * |
||
270 | * @return bool True if the Statement voids another Statement, false otherwise |
||
271 | */ |
||
272 | public function isVoidStatement() |
||
276 | |||
277 | /** |
||
278 | * Returns a {@link StatementReference} for the Statement. |
||
279 | * |
||
280 | * @return StatementReference The reference |
||
281 | */ |
||
282 | public function getStatementReference() |
||
288 | |||
289 | /** |
||
290 | * Returns a Statement that voids the current Statement. |
||
291 | * |
||
292 | * @param Actor $actor The Actor voiding this Statement |
||
293 | * |
||
294 | * @return Statement The voiding Statement |
||
295 | */ |
||
296 | public function getVoidStatement(Actor $actor) |
||
305 | |||
306 | /** |
||
307 | * Checks if another statement is equal. |
||
308 | * |
||
309 | * Two statements are equal if and only if all of their properties are equal. |
||
310 | * |
||
311 | * @param Statement $statement The statement to compare with |
||
312 | * |
||
313 | * @return bool True if the statements are equal, false otherwise |
||
314 | */ |
||
315 | public function equals(Statement $statement) |
||
363 | } |
||
364 |