Complex classes like IndexDocument 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 IndexDocument, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class IndexDocument implements \JsonSerializable { |
||
| 30 | |||
| 31 | const NOT_ENCODED = 0; |
||
| 32 | const ENCODED_BASE64 = 1; |
||
| 33 | |||
| 34 | /** @var string|int */ |
||
| 35 | private $id; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | private $providerId; |
||
| 39 | |||
| 40 | /** @var DocumentAccess */ |
||
| 41 | private $access; |
||
| 42 | |||
| 43 | /** @var Index */ |
||
| 44 | private $index; |
||
| 45 | |||
| 46 | /** @var int */ |
||
| 47 | private $modifiedTime = 0; |
||
| 48 | |||
| 49 | /** @var array */ |
||
| 50 | private $tags = []; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $source; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $title; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $content = null; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $link = ''; |
||
| 63 | |||
| 64 | /** @var array */ |
||
| 65 | private $more = []; |
||
| 66 | |||
| 67 | /** @var array */ |
||
| 68 | private $excerpts = []; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | private $score; |
||
| 72 | |||
| 73 | /** @var array */ |
||
| 74 | private $info = []; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | private $contentEncoded; |
||
| 78 | |||
| 79 | public function __construct($providerId, $id) { |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string|integer $id |
||
| 87 | * |
||
| 88 | * @return $this |
||
| 89 | */ |
||
| 90 | public function setId($id) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return string|integer |
||
| 98 | */ |
||
| 99 | public function getId() { |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $providerId |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function setProviderId($providerId) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getProviderId() { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Index $index |
||
| 125 | */ |
||
| 126 | public function setIndex(Index $index) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return Index |
||
| 132 | */ |
||
| 133 | public function getIndex() { |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * @param int $modifiedTime |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function setModifiedTime($modifiedTime) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function getModifiedTime() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param int $time |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function isOlderThan($time) { |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * @param DocumentAccess $access |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function setAccess(DocumentAccess $access) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return DocumentAccess |
||
| 179 | */ |
||
| 180 | public function getAccess() { |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @param array $tags |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setTags($tags) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function getTags() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $tag |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function addTag($tag) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getSource() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $source |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function setSource($source) { |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $title |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setTitle($title) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getTitle() { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $content |
||
| 254 | * @param int $encoded |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function setContent($content, $encoded = 0) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getContent() { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @return int |
||
| 275 | */ |
||
| 276 | public function isContentEncoded() { |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $link |
||
| 283 | * |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function setLink($link) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getLink() { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @param array $more |
||
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function setMore($more) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function getMore() { |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * @param array $excerpts |
||
| 321 | * |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function setExcerpts($excerpts) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getExcerpts() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $excerpt |
||
| 341 | */ |
||
| 342 | public function addExcerpt($excerpt) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $excerpt |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | public function cleanExcerpt($excerpt) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $score |
||
| 364 | * |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | public function setScore($score) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getScore() { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $info |
||
| 383 | * @param mixed $value |
||
| 384 | * |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function setInfo($info, $value) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $info |
||
| 395 | * @param mixed $default |
||
| 396 | * |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | public function getInfo($info, $default = '') { |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function getInfoAll() { |
||
| 424 | |||
| 425 | |||
| 426 | public function __destruct() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return array<string,string|integer|DocumentAccess|array> |
||
| 443 | */ |
||
| 444 | public function jsonSerialize() { |
||
| 457 | |||
| 458 | } |