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 string */ |
||
63 | public $hash = ''; |
||
64 | |||
65 | /** @var array */ |
||
66 | public $parts = []; |
||
67 | |||
68 | /** @var string */ |
||
69 | public $link = ''; |
||
70 | |||
71 | /** @var array */ |
||
72 | public $more = []; |
||
73 | |||
74 | /** @var array */ |
||
75 | public $excerpts = []; |
||
76 | |||
77 | /** @var string */ |
||
78 | public $score; |
||
79 | |||
80 | /** @var array */ |
||
81 | public $info = []; |
||
82 | |||
83 | /** @var int */ |
||
84 | public $contentEncoded; |
||
85 | |||
86 | |||
87 | public function __construct($providerId, $id) { |
||
91 | |||
92 | |||
93 | /** |
||
94 | * @param string|integer $id |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setId($id) { |
||
103 | |||
104 | /** |
||
105 | * @return string|integer |
||
106 | */ |
||
107 | public function getId() { |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param string $providerId |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function setProviderId($providerId) { |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getProviderId() { |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param Index $index |
||
133 | */ |
||
134 | public function setIndex(Index $index) { |
||
137 | |||
138 | /** |
||
139 | * @return Index |
||
140 | */ |
||
141 | public function getIndex() { |
||
144 | |||
145 | |||
146 | /** |
||
147 | * @param int $modifiedTime |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function setModifiedTime($modifiedTime) { |
||
156 | |||
157 | /** |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getModifiedTime() { |
||
163 | |||
164 | /** |
||
165 | * @param int $time |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function isOlderThan($time) { |
||
172 | |||
173 | |||
174 | /** |
||
175 | * @param DocumentAccess $access |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function setAccess(DocumentAccess $access) { |
||
184 | |||
185 | /** |
||
186 | * @return DocumentAccess |
||
187 | */ |
||
188 | public function getAccess() { |
||
191 | |||
192 | |||
193 | /** |
||
194 | * @param array $tags |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setTags($tags) { |
||
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getTags() { |
||
210 | |||
211 | /** |
||
212 | * @param $tag |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function addTag($tag) { |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getSource() { |
||
228 | |||
229 | /** |
||
230 | * @param string $source |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setSource($source) { |
||
239 | |||
240 | |||
241 | /** |
||
242 | * @param string $title |
||
243 | * |
||
244 | * @return $this |
||
245 | */ |
||
246 | public function setTitle($title) { |
||
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getTitle() { |
||
258 | |||
259 | |||
260 | /** |
||
261 | * @param string $content |
||
262 | * @param int $encoded |
||
263 | * |
||
264 | * @return $this |
||
265 | */ |
||
266 | public function setContent($content, $encoded = 0) { |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getContent() { |
||
279 | |||
280 | |||
281 | /** |
||
282 | * @return $this |
||
283 | */ |
||
284 | public function initHash() { |
||
293 | |||
294 | /** |
||
295 | * @param $hash |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function setHash($hash) { |
||
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getHash() { |
||
311 | |||
312 | |||
313 | /** |
||
314 | * @param string $part |
||
315 | * @param string $content |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function addPart($part, $content) { |
||
324 | |||
325 | /** |
||
326 | * @param array $parts |
||
327 | * |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function setParts($parts) { |
||
335 | |||
336 | /** |
||
337 | * @return array |
||
338 | */ |
||
339 | public function getParts() { |
||
342 | |||
343 | |||
344 | /** |
||
345 | * @return int |
||
346 | */ |
||
347 | public function isContentEncoded() { |
||
350 | |||
351 | |||
352 | /** |
||
353 | * @param string $link |
||
354 | * |
||
355 | * @return $this |
||
356 | */ |
||
357 | public function setLink($link) { |
||
362 | |||
363 | /** |
||
364 | * @return string |
||
365 | */ |
||
366 | public function getLink() { |
||
369 | |||
370 | |||
371 | /** |
||
372 | * @param array $more |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function setMore($more) { |
||
381 | |||
382 | /** |
||
383 | * @return array |
||
384 | */ |
||
385 | public function getMore() { |
||
388 | |||
389 | |||
390 | /** |
||
391 | * @param array $excerpts |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setExcerpts($excerpts) { |
||
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getExcerpts() { |
||
409 | |||
410 | /** |
||
411 | * @param string $excerpt |
||
412 | */ |
||
413 | public function addExcerpt($excerpt) { |
||
418 | |||
419 | /** |
||
420 | * @param $excerpt |
||
421 | * |
||
422 | * @return mixed |
||
423 | */ |
||
424 | public function cleanExcerpt($excerpt) { |
||
434 | |||
435 | /** |
||
436 | * @param string $score |
||
437 | * |
||
438 | * @return $this |
||
439 | */ |
||
440 | public function setScore($score) { |
||
445 | |||
446 | /** |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getScore() { |
||
452 | |||
453 | |||
454 | /** |
||
455 | * @param string $info |
||
456 | * @param mixed $value |
||
457 | * |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function setInfo($info, $value) { |
||
465 | |||
466 | /** |
||
467 | * @param string $info |
||
468 | * @param mixed $default |
||
469 | * |
||
470 | * @return mixed |
||
471 | */ |
||
472 | public function getInfo($info, $default = '') { |
||
479 | |||
480 | |||
481 | /** |
||
482 | * @return array |
||
483 | */ |
||
484 | public function getInfoAll() { |
||
497 | |||
498 | |||
499 | public function __destruct() { |
||
516 | |||
517 | /** |
||
518 | * @return array<string,string|integer|DocumentAccess|array> |
||
519 | */ |
||
520 | public function jsonSerialize() { |
||
537 | |||
538 | } |