Complex classes like SharingFrame 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 SharingFrame, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class SharingFrame implements \JsonSerializable { |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | private $source; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | private $type; |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | private $circleUniqueId; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $circleName; |
||
| 44 | |||
| 45 | /** @var int */ |
||
| 46 | private $circleType; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $author; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $cloudId; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | private $payload; |
||
| 56 | |||
| 57 | /** @var array */ |
||
| 58 | private $headers; |
||
| 59 | |||
| 60 | /** @var int */ |
||
| 61 | private $creation; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | private $uniqueId; |
||
| 65 | |||
| 66 | public function __construct($source, $type) { |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getSource() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function getType() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $circleUniqueId |
||
| 88 | */ |
||
| 89 | public function setCircleId($circleUniqueId) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getCircleId() { |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $circleName |
||
| 103 | */ |
||
| 104 | public function setCircleName($circleName) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getCircleName() { |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @param int $circleType |
||
| 118 | */ |
||
| 119 | public function setCircleType($circleType) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getCircleType() { |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $author |
||
| 133 | */ |
||
| 134 | public function setAuthor($author) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getAuthor() { |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $cloudId |
||
| 148 | */ |
||
| 149 | public function setCloudId($cloudId) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getCloudId() { |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $uniqueId |
||
| 163 | * |
||
| 164 | * @return SharingFrame |
||
| 165 | */ |
||
| 166 | public function setUniqueId($uniqueId) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getUniqueId() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return SharingFrame |
||
| 181 | */ |
||
| 182 | public function generateUniqueId() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param array $payload |
||
| 191 | */ |
||
| 192 | public function setPayload($payload) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param bool $asJson |
||
| 198 | * |
||
| 199 | * @return array|string |
||
| 200 | */ |
||
| 201 | public function getPayload($asJson = false) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $headers |
||
| 212 | */ |
||
| 213 | public function setHeaders($headers) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param bool $asJson |
||
| 219 | * |
||
| 220 | * @return array|string |
||
| 221 | */ |
||
| 222 | public function getHeaders($asJson = false) { |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $k |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getHeader($k) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $k |
||
| 251 | * @param string $v |
||
| 252 | */ |
||
| 253 | public function setHeader($k, $v) { |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param int $creation |
||
| 260 | */ |
||
| 261 | public function setCreation($creation) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function getCreation() { |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public function isLocal() { |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @throws SharingFrameSourceCannotBeAppCirclesException |
||
| 287 | */ |
||
| 288 | public function cannotBeFromCircles() { |
||
| 293 | |||
| 294 | |||
| 295 | public function jsonSerialize() { |
||
| 310 | |||
| 311 | public static function fromJSON($json) { |
||
| 343 | |||
| 344 | } |
||
| 345 | |||
| 346 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.