Complex classes like ChangeLog 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 ChangeLog, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class ChangeLog |
||
9 | { |
||
10 | |||
11 | /** @var string */ |
||
12 | protected $id; |
||
13 | /** @var int */ |
||
14 | protected $chunk_size; |
||
15 | /** @var array */ |
||
16 | protected $cache; |
||
17 | |||
18 | /** |
||
19 | * Constructor |
||
20 | * |
||
21 | * @param string $id page id |
||
22 | * @param int $chunk_size maximum block size read from file |
||
23 | */ |
||
24 | public function __construct($id, $chunk_size = 8192) |
||
25 | { |
||
26 | global $cache_revinfo; |
||
27 | |||
28 | $this->cache =& $cache_revinfo; |
||
29 | if (!isset($this->cache[$id])) { |
||
30 | $this->cache[$id] = array(); |
||
31 | } |
||
32 | |||
33 | $this->id = $id; |
||
34 | $this->setChunkSize($chunk_size); |
||
35 | |||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Set chunk size for file reading |
||
40 | * Chunk size zero let read whole file at once |
||
41 | * |
||
42 | * @param int $chunk_size maximum block size read from file |
||
43 | */ |
||
44 | public function setChunkSize($chunk_size) |
||
45 | { |
||
46 | if (!is_numeric($chunk_size)) $chunk_size = 0; |
||
47 | |||
48 | $this->chunk_size = (int)max($chunk_size, 0); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Returns path to changelog |
||
53 | * |
||
54 | * @return string path to file |
||
55 | */ |
||
56 | abstract protected function getChangelogFilename(); |
||
57 | |||
58 | /** |
||
59 | * Returns path to current page/media |
||
60 | * |
||
61 | * @return string path to file |
||
62 | */ |
||
63 | abstract protected function getFilename(); |
||
64 | |||
65 | /** |
||
66 | * Get the changelog information for a specific page id and revision (timestamp) |
||
67 | * |
||
68 | * Adjacent changelog lines are optimistically parsed and cached to speed up |
||
69 | * consecutive calls to getRevisionInfo. For large changelog files, only the chunk |
||
70 | * containing the requested changelog line is read. |
||
71 | * |
||
72 | * @param int $rev revision timestamp |
||
73 | * @return bool|array false or array with entries: |
||
74 | * - date: unix timestamp |
||
75 | * - ip: IPv4 address (127.0.0.1) |
||
76 | * - type: log line type |
||
77 | * - id: page id |
||
78 | * - user: user name |
||
79 | * - sum: edit summary (or action reason) |
||
80 | * - extra: extra data (varies by line type) |
||
81 | * |
||
82 | * @author Ben Coburn <[email protected]> |
||
83 | * @author Kate Arzamastseva <[email protected]> |
||
84 | */ |
||
85 | public function getRevisionInfo($rev) |
||
113 | |||
114 | /** |
||
115 | * Return a list of page revisions numbers |
||
116 | * |
||
117 | * Does not guarantee that the revision exists in the attic, |
||
118 | * only that a line with the date exists in the changelog. |
||
119 | * By default the current revision is skipped. |
||
120 | * |
||
121 | * The current revision is automatically skipped when the page exists. |
||
122 | * See $INFO['meta']['last_change'] for the current revision. |
||
123 | * A negative $first let read the current revision too. |
||
124 | * |
||
125 | * For efficiency, the log lines are parsed and cached for later |
||
126 | * calls to getRevisionInfo. Large changelog files are read |
||
127 | * backwards in chunks until the requested number of changelog |
||
128 | * lines are recieved. |
||
129 | * |
||
130 | * @param int $first skip the first n changelog lines |
||
131 | * @param int $num number of revisions to return |
||
132 | * @return array with the revision timestamps |
||
133 | * |
||
134 | * @author Ben Coburn <[email protected]> |
||
135 | * @author Kate Arzamastseva <[email protected]> |
||
136 | */ |
||
137 | public function getRevisions($first, $num) |
||
243 | |||
244 | /** |
||
245 | * Get the nth revision left or right handside for a specific page id and revision (timestamp) |
||
246 | * |
||
247 | * For large changelog files, only the chunk containing the |
||
248 | * reference revision $rev is read and sometimes a next chunck. |
||
249 | * |
||
250 | * Adjacent changelog lines are optimistically parsed and cached to speed up |
||
251 | * consecutive calls to getRevisionInfo. |
||
252 | * |
||
253 | * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) |
||
254 | * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev |
||
255 | * @return bool|int |
||
256 | * timestamp of the requested revision |
||
257 | * otherwise false |
||
258 | */ |
||
259 | public function getRelativeRevision($rev, $direction) |
||
320 | |||
321 | /** |
||
322 | * Returns revisions around rev1 and rev2 |
||
323 | * When available it returns $max entries for each revision |
||
324 | * |
||
325 | * @param int $rev1 oldest revision timestamp |
||
326 | * @param int $rev2 newest revision timestamp (0 looks up last revision) |
||
327 | * @param int $max maximum number of revisions returned |
||
328 | * @return array with two arrays with revisions surrounding rev1 respectively rev2 |
||
329 | */ |
||
330 | public function getRevisionsAround($rev1, $rev2, $max = 50) |
||
382 | |||
383 | |||
384 | /** |
||
385 | * Checks if the ID has old revisons |
||
386 | * @return boolean |
||
387 | */ |
||
388 | public function hasRevisions() { |
||
392 | |||
393 | /** |
||
394 | * Returns lines from changelog. |
||
395 | * If file larger than $chuncksize, only chunck is read that could contain $rev. |
||
396 | * |
||
397 | * @param int $rev revision timestamp |
||
398 | * @return array|false |
||
399 | * if success returns array(fp, array(changeloglines), $head, $tail, $eof) |
||
400 | * where fp only defined for chuck reading, needs closing. |
||
401 | * otherwise false |
||
402 | */ |
||
403 | protected function readloglines($rev) |
||
467 | |||
468 | /** |
||
469 | * Read chunk and return array with lines of given chunck. |
||
470 | * Has no check if $head and $tail are really at a new line |
||
471 | * |
||
472 | * @param resource $fp resource filepointer |
||
473 | * @param int $head start point chunck |
||
474 | * @param int $tail end point chunck |
||
475 | * @return array lines read from chunck |
||
476 | */ |
||
477 | protected function readChunk($fp, $head, $tail) |
||
495 | |||
496 | /** |
||
497 | * Set pointer to first new line after $finger and return its position |
||
498 | * |
||
499 | * @param resource $fp filepointer |
||
500 | * @param int $finger a pointer |
||
501 | * @return int pointer |
||
502 | */ |
||
503 | protected function getNewlinepointer($fp, $finger) |
||
513 | |||
514 | /** |
||
515 | * Check whether given revision is the current page |
||
516 | * |
||
517 | * @param int $rev timestamp of current page |
||
518 | * @return bool true if $rev is current revision, otherwise false |
||
519 | */ |
||
520 | public function isCurrentRevision($rev) |
||
524 | |||
525 | /** |
||
526 | * Return an existing revision for a specific date which is |
||
527 | * the current one or younger or equal then the date |
||
528 | * |
||
529 | * @param number $date_at timestamp |
||
530 | * @return string revision ('' for current) |
||
531 | */ |
||
532 | public function getLastRevisionAt($date_at) |
||
545 | |||
546 | /** |
||
547 | * Returns the next lines of the changelog of the chunck before head or after tail |
||
548 | * |
||
549 | * @param resource $fp filepointer |
||
550 | * @param int $head position head of last chunk |
||
551 | * @param int $tail position tail of last chunk |
||
552 | * @param int $direction positive forward, negative backward |
||
553 | * @return array with entries: |
||
554 | * - $lines: changelog lines of readed chunk |
||
555 | * - $head: head of chunk |
||
556 | * - $tail: tail of chunk |
||
557 | */ |
||
558 | protected function readAdjacentChunk($fp, $head, $tail, $direction) |
||
587 | |||
588 | /** |
||
589 | * Collect the $max revisions near to the timestamp $rev |
||
590 | * |
||
591 | * @param int $rev revision timestamp |
||
592 | * @param int $max maximum number of revisions to be returned |
||
593 | * @return bool|array |
||
594 | * return array with entries: |
||
595 | * - $requestedrevs: array of with $max revision timestamps |
||
596 | * - $revs: all parsed revision timestamps |
||
597 | * - $fp: filepointer only defined for chuck reading, needs closing. |
||
598 | * - $lines: non-parsed changelog lines before the parsed revisions |
||
599 | * - $head: position of first readed changelogline |
||
600 | * - $lasttail: position of end of last readed changelogline |
||
601 | * otherwise false |
||
602 | */ |
||
603 | protected function retrieveRevisionsAround($rev, $max) |
||
666 | } |
||
667 |