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 Object |
||
| 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 Object} |
||
| 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 $timestamp; |
||
| 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, Object $object, Result $result = null, Context $context = null, \DateTime $timestamp = null, array $attachments = null) |
||
| 67 | |||
| 68 | public function withActor(Actor $actor) |
||
| 75 | |||
| 76 | public function withVerb(Verb $verb) |
||
| 83 | |||
| 84 | public function withObject(Object $object) |
||
| 91 | |||
| 92 | public function withResult(Result $result) |
||
| 99 | |||
| 100 | public function withTimestamp(\DateTime $timestamp = null) |
||
| 107 | |||
| 108 | public function withContext(Context $context) |
||
| 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() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the Statement's {@link Actor}. |
||
| 141 | * |
||
| 142 | * @return Actor The Actor |
||
| 143 | */ |
||
| 144 | public function getActor() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the Statement's {@link Object}. |
||
| 151 | * |
||
| 152 | * @return \Xabbuh\XApi\Model\Object 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 | public function getTimestamp() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the {@link Statement} {@link Context}. |
||
| 180 | * |
||
| 181 | * @return Context The Context |
||
| 182 | */ |
||
| 183 | public function getContext() |
||
| 187 | |||
| 188 | public function getAttachments() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Tests whether or not this Statement is a void Statement (i.e. it voids |
||
| 195 | * another Statement). |
||
| 196 | * |
||
| 197 | * @return bool True if the Statement voids another Statement, false otherwise |
||
| 198 | */ |
||
| 199 | public function isVoidStatement() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function equals(Object $statement) |
||
| 269 | } |
||
| 270 |