Complex classes like ApiCore 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 ApiCore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ApiCore |
||
| 18 | { |
||
| 19 | /** @var int Increased whenever the API is changed */ |
||
| 20 | const API_VERSION = 10; |
||
| 21 | |||
| 22 | |||
| 23 | /** @var Api */ |
||
| 24 | private $api; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param Api $api |
||
| 28 | */ |
||
| 29 | public function __construct(Api $api) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Returns details about the core methods |
||
| 36 | * |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public function getRemoteInfo() |
||
| 40 | { |
||
| 41 | return array( |
||
| 42 | 'dokuwiki.getVersion' => array( |
||
| 43 | 'args' => array(), |
||
| 44 | 'return' => 'string', |
||
| 45 | 'doc' => 'Returns the running DokuWiki version.' |
||
| 46 | ), 'dokuwiki.login' => array( |
||
| 47 | 'args' => array('string', 'string'), |
||
| 48 | 'return' => 'int', |
||
| 49 | 'doc' => 'Tries to login with the given credentials and sets auth cookies.', |
||
| 50 | 'public' => '1' |
||
| 51 | ), 'dokuwiki.logoff' => array( |
||
| 52 | 'args' => array(), |
||
| 53 | 'return' => 'int', |
||
| 54 | 'doc' => 'Tries to logoff by expiring auth cookies and the associated PHP session.' |
||
| 55 | ), 'dokuwiki.getPagelist' => array( |
||
| 56 | 'args' => array('string', 'array'), |
||
| 57 | 'return' => 'array', |
||
| 58 | 'doc' => 'List all pages within the given namespace.', |
||
| 59 | 'name' => 'readNamespace' |
||
| 60 | ), 'dokuwiki.search' => array( |
||
| 61 | 'args' => array('string'), |
||
| 62 | 'return' => 'array', |
||
| 63 | 'doc' => 'Perform a fulltext search and return a list of matching pages' |
||
| 64 | ), 'dokuwiki.getTime' => array( |
||
| 65 | 'args' => array(), |
||
| 66 | 'return' => 'int', |
||
| 67 | 'doc' => 'Returns the current time at the remote wiki server as Unix timestamp.', |
||
| 68 | ), 'dokuwiki.setLocks' => array( |
||
| 69 | 'args' => array('array'), |
||
| 70 | 'return' => 'array', |
||
| 71 | 'doc' => 'Lock or unlock pages.' |
||
| 72 | ), 'dokuwiki.getTitle' => array( |
||
| 73 | 'args' => array(), |
||
| 74 | 'return' => 'string', |
||
| 75 | 'doc' => 'Returns the wiki title.', |
||
| 76 | 'public' => '1' |
||
| 77 | ), 'dokuwiki.appendPage' => array( |
||
| 78 | 'args' => array('string', 'string', 'array'), |
||
| 79 | 'return' => 'bool', |
||
| 80 | 'doc' => 'Append text to a wiki page.' |
||
| 81 | ), 'dokuwiki.deleteUsers' => array( |
||
| 82 | 'args' => array('array'), |
||
| 83 | 'return' => 'bool', |
||
| 84 | 'doc' => 'Remove one or more users from the list of registered users.' |
||
| 85 | ), 'wiki.getPage' => array( |
||
| 86 | 'args' => array('string'), |
||
| 87 | 'return' => 'string', |
||
| 88 | 'doc' => 'Get the raw Wiki text of page, latest version.', |
||
| 89 | 'name' => 'rawPage', |
||
| 90 | ), 'wiki.getPageVersion' => array( |
||
| 91 | 'args' => array('string', 'int'), |
||
| 92 | 'name' => 'rawPage', |
||
| 93 | 'return' => 'string', |
||
| 94 | 'doc' => 'Return a raw wiki page' |
||
| 95 | ), 'wiki.getPageHTML' => array( |
||
| 96 | 'args' => array('string'), |
||
| 97 | 'return' => 'string', |
||
| 98 | 'doc' => 'Return page in rendered HTML, latest version.', |
||
| 99 | 'name' => 'htmlPage' |
||
| 100 | ), 'wiki.getPageHTMLVersion' => array( |
||
| 101 | 'args' => array('string', 'int'), |
||
| 102 | 'return' => 'string', |
||
| 103 | 'doc' => 'Return page in rendered HTML.', |
||
| 104 | 'name' => 'htmlPage' |
||
| 105 | ), 'wiki.getAllPages' => array( |
||
| 106 | 'args' => array(), |
||
| 107 | 'return' => 'array', |
||
| 108 | 'doc' => 'Returns a list of all pages. The result is an array of utf8 pagenames.', |
||
| 109 | 'name' => 'listPages' |
||
| 110 | ), 'wiki.getAttachments' => array( |
||
| 111 | 'args' => array('string', 'array'), |
||
| 112 | 'return' => 'array', |
||
| 113 | 'doc' => 'Returns a list of all media files.', |
||
| 114 | 'name' => 'listAttachments' |
||
| 115 | ), 'wiki.getBackLinks' => array( |
||
| 116 | 'args' => array('string'), |
||
| 117 | 'return' => 'array', |
||
| 118 | 'doc' => 'Returns the pages that link to this page.', |
||
| 119 | 'name' => 'listBackLinks' |
||
| 120 | ), 'wiki.getPageInfo' => array( |
||
| 121 | 'args' => array('string'), |
||
| 122 | 'return' => 'array', |
||
| 123 | 'doc' => 'Returns a struct with info about the page, latest version.', |
||
| 124 | 'name' => 'pageInfo' |
||
| 125 | ), 'wiki.getPageInfoVersion' => array( |
||
| 126 | 'args' => array('string', 'int'), |
||
| 127 | 'return' => 'array', |
||
| 128 | 'doc' => 'Returns a struct with info about the page.', |
||
| 129 | 'name' => 'pageInfo' |
||
| 130 | ), 'wiki.getPageVersions' => array( |
||
| 131 | 'args' => array('string', 'int'), |
||
| 132 | 'return' => 'array', |
||
| 133 | 'doc' => 'Returns the available revisions of the page.', |
||
| 134 | 'name' => 'pageVersions' |
||
| 135 | ), 'wiki.putPage' => array( |
||
| 136 | 'args' => array('string', 'string', 'array'), |
||
| 137 | 'return' => 'bool', |
||
| 138 | 'doc' => 'Saves a wiki page.' |
||
| 139 | ), 'wiki.listLinks' => array( |
||
| 140 | 'args' => array('string'), |
||
| 141 | 'return' => 'array', |
||
| 142 | 'doc' => 'Lists all links contained in a wiki page.' |
||
| 143 | ), 'wiki.getRecentChanges' => array( |
||
| 144 | 'args' => array('int'), |
||
| 145 | 'return' => 'array', |
||
| 146 | 'doc' => 'Returns a struct about all recent changes since given timestamp.' |
||
| 147 | ), 'wiki.getRecentMediaChanges' => array( |
||
| 148 | 'args' => array('int'), |
||
| 149 | 'return' => 'array', |
||
| 150 | 'doc' => 'Returns a struct about all recent media changes since given timestamp.' |
||
| 151 | ), 'wiki.aclCheck' => array( |
||
| 152 | 'args' => array('string', 'string', 'array'), |
||
| 153 | 'return' => 'int', |
||
| 154 | 'doc' => 'Returns the permissions of a given wiki page. By default, for current user/groups' |
||
| 155 | ), 'wiki.putAttachment' => array( |
||
| 156 | 'args' => array('string', 'file', 'array'), |
||
| 157 | 'return' => 'array', |
||
| 158 | 'doc' => 'Upload a file to the wiki.' |
||
| 159 | ), 'wiki.deleteAttachment' => array( |
||
| 160 | 'args' => array('string'), |
||
| 161 | 'return' => 'int', |
||
| 162 | 'doc' => 'Delete a file from the wiki.' |
||
| 163 | ), 'wiki.getAttachment' => array( |
||
| 164 | 'args' => array('string'), |
||
| 165 | 'doc' => 'Return a media file', |
||
| 166 | 'return' => 'file', |
||
| 167 | 'name' => 'getAttachment', |
||
| 168 | ), 'wiki.getAttachmentInfo' => array( |
||
| 169 | 'args' => array('string'), |
||
| 170 | 'return' => 'array', |
||
| 171 | 'doc' => 'Returns a struct with info about the attachment.' |
||
| 172 | ), 'dokuwiki.getXMLRPCAPIVersion' => array( |
||
| 173 | 'args' => array(), |
||
| 174 | 'name' => 'getAPIVersion', |
||
| 175 | 'return' => 'int', |
||
| 176 | 'doc' => 'Returns the XMLRPC API version.', |
||
| 177 | 'public' => '1', |
||
| 178 | ), 'wiki.getRPCVersionSupported' => array( |
||
| 179 | 'args' => array(), |
||
| 180 | 'name' => 'wikiRpcVersion', |
||
| 181 | 'return' => 'int', |
||
| 182 | 'doc' => 'Returns 2 with the supported RPC API version.', |
||
| 183 | 'public' => '1' |
||
| 184 | ), |
||
| 185 | |||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getVersion() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return int unix timestamp |
||
| 199 | */ |
||
| 200 | public function getTime() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Return a raw wiki page |
||
| 207 | * |
||
| 208 | * @param string $id wiki page id |
||
| 209 | * @param int|string $rev revision timestamp of the page or empty string |
||
| 210 | * @return string page text. |
||
| 211 | * @throws AccessDeniedException if no permission for page |
||
| 212 | */ |
||
| 213 | public function rawPage($id, $rev = '') |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return a media file |
||
| 229 | * |
||
| 230 | * @author Gina Haeussge <[email protected]> |
||
| 231 | * |
||
| 232 | * @param string $id file id |
||
| 233 | * @return mixed media file |
||
| 234 | * @throws AccessDeniedException no permission for media |
||
| 235 | * @throws RemoteException not exist |
||
| 236 | */ |
||
| 237 | public function getAttachment($id) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Return info about a media file |
||
| 255 | * |
||
| 256 | * @author Gina Haeussge <[email protected]> |
||
| 257 | * |
||
| 258 | * @param string $id page id |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function getAttachmentInfo($id) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return a wiki page rendered to html |
||
| 289 | * |
||
| 290 | * @param string $id page id |
||
| 291 | * @param string|int $rev revision timestamp or empty string |
||
| 292 | * @return null|string html |
||
| 293 | * @throws AccessDeniedException no access to page |
||
| 294 | */ |
||
| 295 | public function htmlPage($id, $rev = '') |
||
| 303 | |||
| 304 | /** |
||
| 305 | * List all pages - we use the indexer list here |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function listPages() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * List all pages in the given namespace (and below) |
||
| 334 | * |
||
| 335 | * @param string $ns |
||
| 336 | * @param array $opts |
||
| 337 | * $opts['depth'] recursion level, 0 for all |
||
| 338 | * $opts['hash'] do md5 sum of content? |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function readNamespace($ns, $opts = array()) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * List all pages in the given namespace (and below) |
||
| 357 | * |
||
| 358 | * @param string $query |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | public function search($query) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Returns the wiki title. |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function getTitle() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * List all media files. |
||
| 405 | * |
||
| 406 | * Available options are 'recursive' for also including the subnamespaces |
||
| 407 | * in the listing, and 'pattern' for filtering the returned files against |
||
| 408 | * a regular expression matching their name. |
||
| 409 | * |
||
| 410 | * @author Gina Haeussge <[email protected]> |
||
| 411 | * |
||
| 412 | * @param string $ns |
||
| 413 | * @param array $options |
||
| 414 | * $options['depth'] recursion level, 0 for all |
||
| 415 | * $options['showmsg'] shows message if invalid media id is used |
||
| 416 | * $options['pattern'] check given pattern |
||
| 417 | * $options['hash'] add hashes to result list |
||
| 418 | * @return array |
||
| 419 | * @throws AccessDeniedException no access to the media files |
||
| 420 | */ |
||
| 421 | public function listAttachments($ns, $options = array()) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Return a list of backlinks |
||
| 452 | * |
||
| 453 | * @param string $id page id |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public function listBackLinks($id) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Return some basic data about a page |
||
| 463 | * |
||
| 464 | * @param string $id page id |
||
| 465 | * @param string|int $rev revision timestamp or empty string |
||
| 466 | * @return array |
||
| 467 | * @throws AccessDeniedException no access for page |
||
| 468 | * @throws RemoteException page not exist |
||
| 469 | */ |
||
| 470 | public function pageInfo($id, $rev = '') |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Save a wiki page |
||
| 503 | * |
||
| 504 | * @author Michael Klier <[email protected]> |
||
| 505 | * |
||
| 506 | * @param string $id page id |
||
| 507 | * @param string $text wiki text |
||
| 508 | * @param array $params parameters: summary, minor edit |
||
| 509 | * @return bool |
||
| 510 | * @throws AccessDeniedException no write access for page |
||
| 511 | * @throws RemoteException no id, empty new page or locked |
||
| 512 | */ |
||
| 513 | public function putPage($id, $text, $params = array()) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Appends text to a wiki page. |
||
| 569 | * |
||
| 570 | * @param string $id page id |
||
| 571 | * @param string $text wiki text |
||
| 572 | * @param array $params such as summary,minor |
||
| 573 | * @return bool|string |
||
| 574 | * @throws RemoteException |
||
| 575 | */ |
||
| 576 | public function appendPage($id, $text, $params = array()) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Remove one or more users from the list of registered users |
||
| 587 | * |
||
| 588 | * @param string[] $usernames List of usernames to remove |
||
| 589 | * |
||
| 590 | * @return bool |
||
| 591 | * |
||
| 592 | * @throws AccessDeniedException |
||
| 593 | */ |
||
| 594 | public function deleteUsers($usernames) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Uploads a file to the wiki. |
||
| 606 | * |
||
| 607 | * Michael Klier <[email protected]> |
||
| 608 | * |
||
| 609 | * @param string $id page id |
||
| 610 | * @param string $file |
||
| 611 | * @param array $params such as overwrite |
||
| 612 | * @return false|string |
||
| 613 | * @throws RemoteException |
||
| 614 | */ |
||
| 615 | public function putAttachment($id, $file, $params = array()) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Deletes a file from the wiki. |
||
| 642 | * |
||
| 643 | * @author Gina Haeussge <[email protected]> |
||
| 644 | * |
||
| 645 | * @param string $id page id |
||
| 646 | * @return int |
||
| 647 | * @throws AccessDeniedException no permissions |
||
| 648 | * @throws RemoteException file in use or not deleted |
||
| 649 | */ |
||
| 650 | public function deleteAttachment($id) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Returns the permissions of a given wiki page for the current user or another user |
||
| 668 | * |
||
| 669 | * @param string $id page id |
||
| 670 | * @param string|null $user username |
||
| 671 | * @param array|null $groups array of groups |
||
| 672 | * @return int permission level |
||
| 673 | */ |
||
| 674 | public function aclCheck($id, $user = null, $groups = null) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Lists all links contained in a wiki page |
||
| 697 | * |
||
| 698 | * @author Michael Klier <[email protected]> |
||
| 699 | * |
||
| 700 | * @param string $id page id |
||
| 701 | * @return array |
||
| 702 | * @throws AccessDeniedException no read access for page |
||
| 703 | */ |
||
| 704 | public function listLinks($id) |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Returns a list of recent changes since give timestamp |
||
| 750 | * |
||
| 751 | * @author Michael Hamann <[email protected]> |
||
| 752 | * @author Michael Klier <[email protected]> |
||
| 753 | * |
||
| 754 | * @param int $timestamp unix timestamp |
||
| 755 | * @return array |
||
| 756 | * @throws RemoteException no valid timestamp |
||
| 757 | */ |
||
| 758 | public function getRecentChanges($timestamp) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Returns a list of recent media changes since give timestamp |
||
| 789 | * |
||
| 790 | * @author Michael Hamann <[email protected]> |
||
| 791 | * @author Michael Klier <[email protected]> |
||
| 792 | * |
||
| 793 | * @param int $timestamp unix timestamp |
||
| 794 | * @return array |
||
| 795 | * @throws RemoteException no valid timestamp |
||
| 796 | */ |
||
| 797 | public function getRecentMediaChanges($timestamp) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Returns a list of available revisions of a given wiki page |
||
| 827 | * Number of returned pages is set by $conf['recent'] |
||
| 828 | * However not accessible pages are skipped, so less than $conf['recent'] could be returned |
||
| 829 | * |
||
| 830 | * @author Michael Klier <[email protected]> |
||
| 831 | * |
||
| 832 | * @param string $id page id |
||
| 833 | * @param int $first skip the first n changelog lines |
||
| 834 | * 0 = from current(if exists) |
||
| 835 | * 1 = from 1st old rev |
||
| 836 | * 2 = from 2nd old rev, etc |
||
| 837 | * @return array |
||
| 838 | * @throws AccessDeniedException no read access for page |
||
| 839 | * @throws RemoteException empty id |
||
| 840 | */ |
||
| 841 | public function pageVersions($id, $first = 0) |
||
| 895 | |||
| 896 | /** |
||
| 897 | * The version of Wiki RPC API supported |
||
| 898 | */ |
||
| 899 | public function wikiRpcVersion() |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Locks or unlocks a given batch of pages |
||
| 906 | * |
||
| 907 | * Give an associative array with two keys: lock and unlock. Both should contain a |
||
| 908 | * list of pages to lock or unlock |
||
| 909 | * |
||
| 910 | * Returns an associative array with the keys locked, lockfail, unlocked and |
||
| 911 | * unlockfail, each containing lists of pages. |
||
| 912 | * |
||
| 913 | * @param array[] $set list pages with array('lock' => array, 'unlock' => array) |
||
| 914 | * @return array |
||
| 915 | */ |
||
| 916 | public function setLocks($set) |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Return API version |
||
| 952 | * |
||
| 953 | * @return int |
||
| 954 | */ |
||
| 955 | public function getAPIVersion() |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Login |
||
| 962 | * |
||
| 963 | * @param string $user |
||
| 964 | * @param string $pass |
||
| 965 | * @return int |
||
| 966 | */ |
||
| 967 | public function login($user, $pass) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Log off |
||
| 997 | * |
||
| 998 | * @return int |
||
| 999 | */ |
||
| 1000 | public function logoff() |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Resolve page id |
||
| 1014 | * |
||
| 1015 | * @param string $id page id |
||
| 1016 | * @return string |
||
| 1017 | */ |
||
| 1018 | private function resolvePageId($id) |
||
| 1027 | } |
||
| 1028 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.