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) |
||
| 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) |
||
| 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) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the nth revision left or right handside for a specific page id and revision (timestamp) |
||
| 247 | * |
||
| 248 | * For large changelog files, only the chunk containing the |
||
| 249 | * reference revision $rev is read and sometimes a next chunck. |
||
| 250 | * |
||
| 251 | * Adjacent changelog lines are optimistically parsed and cached to speed up |
||
| 252 | * consecutive calls to getRevisionInfo. |
||
| 253 | * |
||
| 254 | * @param int $rev revision timestamp used as startdate (doesn't need to be revisionnumber) |
||
| 255 | * @param int $direction give position of returned revision with respect to $rev; positive=next, negative=prev |
||
| 256 | * @return bool|int |
||
| 257 | * timestamp of the requested revision |
||
| 258 | * otherwise false |
||
| 259 | */ |
||
| 260 | public function getRelativeRevision($rev, $direction) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns revisions around rev1 and rev2 |
||
| 324 | * When available it returns $max entries for each revision |
||
| 325 | * |
||
| 326 | * @param int $rev1 oldest revision timestamp |
||
| 327 | * @param int $rev2 newest revision timestamp (0 looks up last revision) |
||
| 328 | * @param int $max maximum number of revisions returned |
||
| 329 | * @return array with two arrays with revisions surrounding rev1 respectively rev2 |
||
| 330 | */ |
||
| 331 | public function getRevisionsAround($rev1, $rev2, $max = 50) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Returns lines from changelog. |
||
| 386 | * If file larger than $chuncksize, only chunck is read that could contain $rev. |
||
| 387 | * |
||
| 388 | * @param int $rev revision timestamp |
||
| 389 | * @return array|false |
||
| 390 | * if success returns array(fp, array(changeloglines), $head, $tail, $eof) |
||
| 391 | * where fp only defined for chuck reading, needs closing. |
||
| 392 | * otherwise false |
||
| 393 | */ |
||
| 394 | protected function readloglines($rev) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Read chunk and return array with lines of given chunck. |
||
| 461 | * Has no check if $head and $tail are really at a new line |
||
| 462 | * |
||
| 463 | * @param resource $fp resource filepointer |
||
| 464 | * @param int $head start point chunck |
||
| 465 | * @param int $tail end point chunck |
||
| 466 | * @return array lines read from chunck |
||
| 467 | */ |
||
| 468 | protected function readChunk($fp, $head, $tail) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set pointer to first new line after $finger and return its position |
||
| 489 | * |
||
| 490 | * @param resource $fp filepointer |
||
| 491 | * @param int $finger a pointer |
||
| 492 | * @return int pointer |
||
| 493 | */ |
||
| 494 | protected function getNewlinepointer($fp, $finger) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Check whether given revision is the current page |
||
| 507 | * |
||
| 508 | * @param int $rev timestamp of current page |
||
| 509 | * @return bool true if $rev is current revision, otherwise false |
||
| 510 | */ |
||
| 511 | public function isCurrentRevision($rev) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Return an existing revision for a specific date which is |
||
| 518 | * the current one or younger or equal then the date |
||
| 519 | * |
||
| 520 | * @param number $date_at timestamp |
||
| 521 | * @return string revision ('' for current) |
||
| 522 | */ |
||
| 523 | public function getLastRevisionAt($date_at) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns the next lines of the changelog of the chunck before head or after tail |
||
| 539 | * |
||
| 540 | * @param resource $fp filepointer |
||
| 541 | * @param int $head position head of last chunk |
||
| 542 | * @param int $tail position tail of last chunk |
||
| 543 | * @param int $direction positive forward, negative backward |
||
| 544 | * @return array with entries: |
||
| 545 | * - $lines: changelog lines of readed chunk |
||
| 546 | * - $head: head of chunk |
||
| 547 | * - $tail: tail of chunk |
||
| 548 | */ |
||
| 549 | protected function readAdjacentChunk($fp, $head, $tail, $direction) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Collect the $max revisions near to the timestamp $rev |
||
| 581 | * |
||
| 582 | * @param int $rev revision timestamp |
||
| 583 | * @param int $max maximum number of revisions to be returned |
||
| 584 | * @return bool|array |
||
| 585 | * return array with entries: |
||
| 586 | * - $requestedrevs: array of with $max revision timestamps |
||
| 587 | * - $revs: all parsed revision timestamps |
||
| 588 | * - $fp: filepointer only defined for chuck reading, needs closing. |
||
| 589 | * - $lines: non-parsed changelog lines before the parsed revisions |
||
| 590 | * - $head: position of first readed changelogline |
||
| 591 | * - $lasttail: position of end of last readed changelogline |
||
| 592 | * otherwise false |
||
| 593 | */ |
||
| 594 | protected function retrieveRevisionsAround($rev, $max) |
||
| 657 | } |
||
| 658 |