Complex classes like SubStatement 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 SubStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | final class SubStatement extends StatementObject |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Verb $verb The {@link Verb} |
||
| 23 | */ |
||
| 24 | private $verb; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Actor The {@link Actor} |
||
| 28 | */ |
||
| 29 | private $actor; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Object The {@link StatementObject} |
||
| 33 | */ |
||
| 34 | private $object; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Result The {@link Activity} {@link Result} |
||
| 38 | */ |
||
| 39 | private $result; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \DateTime The timestamp of when the events described in this statement occurred |
||
| 43 | */ |
||
| 44 | private $created; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Context The {@link Statement} {@link Context} |
||
| 48 | */ |
||
| 49 | private $context; |
||
| 50 | |||
| 51 | private $attachments; |
||
| 52 | |||
| 53 | public function __construct(Actor $actor, Verb $verb, StatementObject $object, Result $result = null, Context $context = null, \DateTime $created = null, array $attachments = null) |
||
| 54 | { |
||
| 55 | if ($object instanceof SubStatement) { |
||
| 56 | throw new \InvalidArgumentException('Nesting sub statements is forbidden by the xAPI spec.'); |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->actor = $actor; |
||
| 60 | $this->verb = $verb; |
||
| 61 | $this->object = $object; |
||
| 62 | $this->result = $result; |
||
| 63 | $this->created = $created; |
||
| 64 | $this->context = $context; |
||
| 65 | $this->attachments = null !== $attachments ? array_values($attachments) : null; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function withActor(Actor $actor) |
||
| 69 | { |
||
| 70 | $subStatement = clone $this; |
||
| 71 | $subStatement->actor = $actor; |
||
| 72 | |||
| 73 | return $subStatement; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function withVerb(Verb $verb) |
||
| 77 | { |
||
| 78 | $subStatement = clone $this; |
||
| 79 | $subStatement->verb = $verb; |
||
| 80 | |||
| 81 | return $subStatement; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function withObject(StatementObject $object) |
||
| 85 | { |
||
| 86 | $subStatement = clone $this; |
||
| 87 | $subStatement->object = $object; |
||
| 88 | |||
| 89 | return $subStatement; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function withResult(Result $result) |
||
| 93 | { |
||
| 94 | $subStatement = clone $this; |
||
| 95 | $subStatement->result = $result; |
||
| 96 | |||
| 97 | return $subStatement; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function withCreated(\DateTime $created = null) |
||
| 101 | { |
||
| 102 | $statement = clone $this; |
||
| 103 | $statement->created = $created; |
||
| 104 | |||
| 105 | return $statement; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function withContext(Context $context) |
||
| 109 | { |
||
| 110 | $subStatement = clone $this; |
||
| 111 | $subStatement->context = $context; |
||
| 112 | |||
| 113 | return $subStatement; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param Attachment[]|null $attachments |
||
| 118 | * |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | public function withAttachments(array $attachments = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the Statement's {@link Verb}. |
||
| 131 | * |
||
| 132 | * @return Verb The Verb |
||
| 133 | */ |
||
| 134 | public function getVerb() |
||
| 135 | { |
||
| 136 | return $this->verb; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the Statement's {@link Actor}. |
||
| 141 | * |
||
| 142 | * @return Actor The Actor |
||
| 143 | */ |
||
| 144 | public function getActor() |
||
| 145 | { |
||
| 146 | return $this->actor; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the Statement's {@link StatementObject}. |
||
| 151 | * |
||
| 152 | * @return \Xabbuh\XApi\Model\StatementObject The Object |
||
| 153 | */ |
||
| 154 | public function getObject() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the {@link Activity} {@link Result}. |
||
| 161 | * |
||
| 162 | * @return Result The Result |
||
| 163 | */ |
||
| 164 | public function getResult() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the timestamp of when the events described in this statement |
||
| 171 | * occurred. |
||
| 172 | * |
||
| 173 | * @return \DateTime The timestamp |
||
| 174 | */ |
||
| 175 | public function getCreated() |
||
| 176 | { |
||
| 177 | return $this->created; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns the {@link Statement} {@link Context}. |
||
| 182 | * |
||
| 183 | * @return Context The Context |
||
| 184 | */ |
||
| 185 | public function getContext() |
||
| 189 | |||
| 190 | public function getAttachments() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
| 197 | * another Statement). |
||
| 198 | * |
||
| 199 | * @return bool True if the Statement voids another Statement, false otherwise |
||
| 200 | */ |
||
| 201 | public function isVoidStatement() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function equals(StatementObject $statement) |
||
| 271 | } |
||
| 272 |