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 StatementObject} |
||
| 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 StatementObject $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|null $version |
||
| 82 | */ |
||
| 83 | 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, $version = null) |
||
| 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(StatementObject $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 | public function withCreated(\DateTime $created = null) |
||
| 155 | { |
||
| 156 | $statement = clone $this; |
||
| 157 | $statement->created = $created; |
||
| 158 | |||
| 159 | return $statement; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function withStored(\DateTime $stored = null) |
||
| 169 | |||
| 170 | 2 | public function withContext(Context $context = null) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param Attachment[]|null $attachments |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | */ |
||
| 183 | public function withAttachments(array $attachments = null) |
||
| 190 | 1 | ||
| 191 | /** |
||
| 192 | * @param string $version |
||
| 193 | * |
||
| 194 | * @return self |
||
| 195 | */ |
||
| 196 | public function withVersion($version) |
||
| 203 | |||
| 204 | 2 | /** |
|
| 205 | * Returns the Statement's unique identifier. |
||
| 206 | * |
||
| 207 | * @return StatementId|null The identifier |
||
| 208 | */ |
||
| 209 | public function getId() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the Statement's {@link Verb}. |
||
| 216 | 2 | * |
|
| 217 | * @return Verb The Verb |
||
| 218 | 2 | */ |
|
| 219 | public function getVerb() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the Statement's {@link Actor}. |
||
| 226 | 2 | * |
|
| 227 | * @return Actor The Actor |
||
| 228 | */ |
||
| 229 | public function getActor() |
||
| 233 | |||
| 234 | 2 | /** |
|
| 235 | * Returns the Statement's {@link StatementObject}. |
||
| 236 | * |
||
| 237 | * @return \Xabbuh\XApi\Model\Object The Object |
||
| 238 | 2 | */ |
|
| 239 | public function getObject() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the {@link Activity} {@link Result}. |
||
| 246 | 2 | * |
|
| 247 | 1 | * @return Result The Result |
|
| 248 | */ |
||
| 249 | public function getResult() |
||
| 253 | |||
| 254 | 1 | /** |
|
| 255 | 1 | * Returns the Authority that asserted the Statement true. |
|
| 256 | * |
||
| 257 | * @return Actor The Authority |
||
| 258 | */ |
||
| 259 | public function getAuthority() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the timestamp of when the events described in this statement |
||
| 266 | * occurred. |
||
| 267 | * |
||
| 268 | * @return \DateTime The timestamp |
||
| 269 | */ |
||
| 270 | public function getCreated() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns the timestamp of when this statement was recorded by the LRS. |
||
| 277 | * |
||
| 278 | * @return \DateTime The timestamp |
||
| 279 | */ |
||
| 280 | public function getStored() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the context that gives the statement more meaning. |
||
| 287 | * |
||
| 288 | * @return Context|null |
||
| 289 | */ |
||
| 290 | public function getContext() |
||
| 294 | |||
| 295 | public function getAttachments() |
||
| 299 | |||
| 300 | public function getVersion() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
| 307 | * another Statement). |
||
| 308 | * |
||
| 309 | * @return bool True if the Statement voids another Statement, false otherwise |
||
| 310 | */ |
||
| 311 | public function isVoidStatement() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Returns a {@link StatementReference} for the Statement. |
||
| 318 | * |
||
| 319 | * @return StatementReference The reference |
||
| 320 | */ |
||
| 321 | public function getStatementReference() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Returns a Statement that voids the current Statement. |
||
| 330 | * |
||
| 331 | * @param Actor $actor The Actor voiding this Statement |
||
| 332 | * |
||
| 333 | * @return Statement The voiding Statement |
||
| 334 | */ |
||
| 335 | public function getVoidStatement(Actor $actor) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Checks if another statement is equal. |
||
| 347 | * |
||
| 348 | * Two statements are equal if and only if all of their properties are equal. |
||
| 349 | * |
||
| 350 | * @param Statement $statement The statement to compare with |
||
| 351 | * |
||
| 352 | * @return bool True if the statements are equal, false otherwise |
||
| 353 | */ |
||
| 354 | public function equals(Statement $statement) |
||
| 430 | } |
||
| 431 |
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: