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 | public $id; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | public $providerId; |
||
| 39 | |||
| 40 | /** @var DocumentAccess */ |
||
| 41 | public $access; |
||
| 42 | |||
| 43 | /** @var Index */ |
||
| 44 | public $index; |
||
| 45 | |||
| 46 | /** @var int */ |
||
| 47 | public $modifiedTime = 0; |
||
| 48 | |||
| 49 | /** @var array */ |
||
| 50 | public $tags = []; |
||
| 51 | |||
| 52 | // TODO: do we really need it in the base class ? |
||
| 53 | /** @var string */ |
||
| 54 | public $source = ''; |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | public $title = ''; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | public $content = null; |
||
| 61 | |||
| 62 | /** @var array */ |
||
| 63 | public $parts = []; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | public $link = ''; |
||
| 67 | |||
| 68 | /** @var array */ |
||
| 69 | public $more = []; |
||
| 70 | |||
| 71 | /** @var array */ |
||
| 72 | public $excerpts = []; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | public $score; |
||
| 76 | |||
| 77 | /** @var array */ |
||
| 78 | public $info = []; |
||
| 79 | |||
| 80 | /** @var int */ |
||
| 81 | public $contentEncoded; |
||
| 82 | |||
| 83 | |||
| 84 | public function __construct($providerId, $id) { |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string|integer $id |
||
| 92 | * |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | public function setId($id) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return string|integer |
||
| 103 | */ |
||
| 104 | public function getId() { |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $providerId |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | public function setProviderId($providerId) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getProviderId() { |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @param Index $index |
||
| 130 | */ |
||
| 131 | public function setIndex(Index $index) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return Index |
||
| 137 | */ |
||
| 138 | public function getIndex() { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param int $modifiedTime |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function setModifiedTime($modifiedTime) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | public function getModifiedTime() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param int $time |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function isOlderThan($time) { |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @param DocumentAccess $access |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function setAccess(DocumentAccess $access) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return DocumentAccess |
||
| 184 | */ |
||
| 185 | public function getAccess() { |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * @param array $tags |
||
| 192 | * |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function setTags($tags) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | public function getTags() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param $tag |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function addTag($tag) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getSource() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $source |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function setSource($source) { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $title |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function setTitle($title) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getTitle() { |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $content |
||
| 259 | * @param int $encoded |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function setContent($content, $encoded = 0) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getContent() { |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $part |
||
| 280 | * @param string $content |
||
| 281 | * |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function addPart($part, $content) { |
||
| 285 | $this->parts[$part] = $content; |
||
| 286 | |||
| 287 | return $this; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $parts |
||
| 292 | * |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | public function setParts($parts) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function getParts() { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | public function isContentEncoded() { |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $link |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function setLink($link) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getLink() { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param array $more |
||
| 338 | * |
||
| 339 | * @return $this |
||
| 340 | */ |
||
| 341 | public function setMore($more) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getMore() { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $excerpts |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setExcerpts($excerpts) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function getExcerpts() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $excerpt |
||
| 377 | */ |
||
| 378 | public function addExcerpt($excerpt) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $excerpt |
||
| 386 | * |
||
| 387 | * @return mixed |
||
| 388 | */ |
||
| 389 | public function cleanExcerpt($excerpt) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $score |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function setScore($score) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getScore() { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $info |
||
| 421 | * @param mixed $value |
||
| 422 | * |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | public function setInfo($info, $value) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $info |
||
| 433 | * @param mixed $default |
||
| 434 | * |
||
| 435 | * @return mixed |
||
| 436 | */ |
||
| 437 | public function getInfo($info, $default = '') { |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | public function getInfoAll() { |
||
| 462 | |||
| 463 | |||
| 464 | public function __destruct() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return array<string,string|integer|DocumentAccess|array> |
||
| 483 | */ |
||
| 484 | public function jsonSerialize() { |
||
| 499 | |||
| 500 | } |