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 |
||
| 32 | class SharingFrame implements \JsonSerializable { |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | private $source; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | private $type; |
||
| 39 | |||
| 40 | /** @var Circle */ |
||
| 41 | private $circle; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $author; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | private $cloudId; |
||
| 48 | |||
| 49 | /** @var array */ |
||
| 50 | private $payload; |
||
| 51 | |||
| 52 | /** @var array */ |
||
| 53 | private $headers; |
||
| 54 | |||
| 55 | /** @var int */ |
||
| 56 | private $creation; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $uniqueId; |
||
| 60 | |||
| 61 | public function __construct($source, $type) { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getSource() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getType() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Circle $circle |
||
| 83 | */ |
||
| 84 | public function setCircle($circle) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return Circle |
||
| 90 | */ |
||
| 91 | public function getCircle() { |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $author |
||
| 98 | */ |
||
| 99 | public function setAuthor($author) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getAuthor() { |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $cloudId |
||
| 113 | */ |
||
| 114 | public function setCloudId($cloudId) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function getCloudId() { |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $uniqueId |
||
| 128 | * |
||
| 129 | * @return SharingFrame |
||
| 130 | */ |
||
| 131 | public function setUniqueId($uniqueId) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getUniqueId() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return SharingFrame |
||
| 146 | */ |
||
| 147 | public function generateUniqueId() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param array $payload |
||
| 156 | */ |
||
| 157 | public function setPayload($payload) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param bool $asJson |
||
| 163 | * |
||
| 164 | * @return array|string |
||
| 165 | */ |
||
| 166 | public function getPayload($asJson = false) { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $headers |
||
| 177 | */ |
||
| 178 | public function setHeaders($headers) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param bool $asJson |
||
| 184 | * |
||
| 185 | * @return array|string |
||
| 186 | */ |
||
| 187 | public function getHeaders($asJson = false) { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $k |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getHeader($k) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $k |
||
| 216 | * @param string $v |
||
| 217 | */ |
||
| 218 | public function setHeader($k, $v) { |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @param int $creation |
||
| 225 | */ |
||
| 226 | public function setCreation($creation) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function getCreation() { |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function isLocal() { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function is0Circle() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @throws SharingFrameSourceCannotBeAppCirclesException |
||
| 259 | */ |
||
| 260 | public function cannotBeFromCircles() { |
||
| 265 | |||
| 266 | |||
| 267 | public function jsonSerialize() { |
||
| 281 | |||
| 282 | public static function fromJSON($json) { |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $arr |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | private static function getHeadersFromArray($arr) { |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * @param array $arr |
||
| 323 | * |
||
| 324 | * @return Circle |
||
| 325 | */ |
||
| 326 | private static function getCircleFromArray($arr) { |
||
| 345 | } |
||
| 346 | |||
| 347 |