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 |
||
| 16 | class ApiCore |
||
| 17 | { |
||
| 18 | /** @var int Increased whenever the API is changed */ |
||
| 19 | const API_VERSION = 10; |
||
| 20 | |||
| 21 | |||
| 22 | /** @var Api */ |
||
| 23 | private $api; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param Api $api |
||
| 27 | */ |
||
| 28 | public function __construct(Api $api) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Returns details about the core methods |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function __getRemoteInfo() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getVersion() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return int unix timestamp |
||
| 198 | */ |
||
| 199 | public function getTime() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return a raw wiki page |
||
| 206 | * |
||
| 207 | * @param string $id wiki page id |
||
| 208 | * @param int|string $rev revision timestamp of the page or empty string |
||
| 209 | * @return string page text. |
||
| 210 | * @throws AccessDeniedException if no permission for page |
||
| 211 | */ |
||
| 212 | public function rawPage($id, $rev = '') |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return a media file |
||
| 228 | * |
||
| 229 | * @author Gina Haeussge <[email protected]> |
||
| 230 | * |
||
| 231 | * @param string $id file id |
||
| 232 | * @return mixed media file |
||
| 233 | * @throws AccessDeniedException no permission for media |
||
| 234 | * @throws RemoteException not exist |
||
| 235 | */ |
||
| 236 | public function getAttachment($id) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return info about a media file |
||
| 254 | * |
||
| 255 | * @author Gina Haeussge <[email protected]> |
||
| 256 | * |
||
| 257 | * @param string $id page id |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getAttachmentInfo($id) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Return a wiki page rendered to html |
||
| 288 | * |
||
| 289 | * @param string $id page id |
||
| 290 | * @param string|int $rev revision timestamp or empty string |
||
| 291 | * @return null|string html |
||
| 292 | * @throws AccessDeniedException no access to page |
||
| 293 | */ |
||
| 294 | public function htmlPage($id, $rev = '') |
||
| 302 | |||
| 303 | /** |
||
| 304 | * List all pages - we use the indexer list here |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | public function listPages() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * List all pages in the given namespace (and below) |
||
| 332 | * |
||
| 333 | * @param string $ns |
||
| 334 | * @param array $opts |
||
| 335 | * $opts['depth'] recursion level, 0 for all |
||
| 336 | * $opts['hash'] do md5 sum of content? |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | public function readNamespace($ns, $opts) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * List all pages in the given namespace (and below) |
||
| 355 | * |
||
| 356 | * @param string $query |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function search($query) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns the wiki title. |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function getTitle() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * List all media files. |
||
| 403 | * |
||
| 404 | * Available options are 'recursive' for also including the subnamespaces |
||
| 405 | * in the listing, and 'pattern' for filtering the returned files against |
||
| 406 | * a regular expression matching their name. |
||
| 407 | * |
||
| 408 | * @author Gina Haeussge <[email protected]> |
||
| 409 | * |
||
| 410 | * @param string $ns |
||
| 411 | * @param array $options |
||
| 412 | * $options['depth'] recursion level, 0 for all |
||
| 413 | * $options['showmsg'] shows message if invalid media id is used |
||
| 414 | * $options['pattern'] check given pattern |
||
| 415 | * $options['hash'] add hashes to result list |
||
| 416 | * @return array |
||
| 417 | * @throws AccessDeniedException no access to the media files |
||
| 418 | */ |
||
| 419 | public function listAttachments($ns, $options = array()) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Return a list of backlinks |
||
| 450 | * |
||
| 451 | * @param string $id page id |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function listBackLinks($id) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Return some basic data about a page |
||
| 461 | * |
||
| 462 | * @param string $id page id |
||
| 463 | * @param string|int $rev revision timestamp or empty string |
||
| 464 | * @return array |
||
| 465 | * @throws AccessDeniedException no access for page |
||
| 466 | * @throws RemoteException page not exist |
||
| 467 | */ |
||
| 468 | public function pageInfo($id, $rev = '') |
||
| 469 | { |
||
| 470 | $id = $this->resolvePageId($id); |
||
| 471 | if (auth_quickaclcheck($id) < AUTH_READ) { |
||
| 472 | throw new AccessDeniedException('You are not allowed to read this page', 111); |
||
| 473 | } |
||
| 474 | $file = wikiFN($id, $rev); |
||
| 475 | $time = @filemtime($file); |
||
| 476 | if (!$time) { |
||
| 477 | throw new RemoteException('The requested page does not exist', 121); |
||
| 478 | } |
||
| 479 | |||
| 480 | // set revision to current version if empty, use revision otherwise |
||
| 481 | // as the timestamps of old files are not necessarily correct |
||
| 482 | if ($rev === '') { |
||
| 483 | $rev = $time; |
||
| 484 | } |
||
| 485 | |||
| 486 | $pagelog = new PageChangeLog($id, 1024); |
||
| 487 | $info = $pagelog->getRevisionInfo($rev); |
||
| 488 | |||
| 489 | $data = array( |
||
| 490 | 'name' => $id, |
||
| 491 | 'lastModified' => $this->api->toDate($rev), |
||
| 492 | 'author' => is_array($info) ? (($info['user']) ? $info['user'] : $info['ip']) : null, |
||
| 493 | 'version' => $rev |
||
| 494 | ); |
||
| 495 | |||
| 496 | return ($data); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Save a wiki page |
||
| 501 | * |
||
| 502 | * @author Michael Klier <[email protected]> |
||
| 503 | * |
||
| 504 | * @param string $id page id |
||
| 505 | * @param string $text wiki text |
||
| 506 | * @param array $params parameters: summary, minor edit |
||
| 507 | * @return bool |
||
| 508 | * @throws AccessDeniedException no write access for page |
||
| 509 | * @throws RemoteException no id, empty new page or locked |
||
| 510 | */ |
||
| 511 | public function putPage($id, $text, $params) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Appends text to a wiki page. |
||
| 567 | * |
||
| 568 | * @param string $id page id |
||
| 569 | * @param string $text wiki text |
||
| 570 | * @param array $params such as summary,minor |
||
| 571 | * @return bool|string |
||
| 572 | * @throws RemoteException |
||
| 573 | */ |
||
| 574 | public function appendPage($id, $text, $params) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Remove one or more users from the list of registered users |
||
| 585 | * |
||
| 586 | * @param string[] $usernames List of usernames to remove |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | * |
||
| 590 | * @throws AccessDeniedException |
||
| 591 | */ |
||
| 592 | public function deleteUsers($usernames) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Uploads a file to the wiki. |
||
| 604 | * |
||
| 605 | * Michael Klier <[email protected]> |
||
| 606 | * |
||
| 607 | * @param string $id page id |
||
| 608 | * @param string $file |
||
| 609 | * @param array $params such as overwrite |
||
| 610 | * @return false|string |
||
| 611 | * @throws RemoteException |
||
| 612 | */ |
||
| 613 | public function putAttachment($id, $file, $params) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Deletes a file from the wiki. |
||
| 640 | * |
||
| 641 | * @author Gina Haeussge <[email protected]> |
||
| 642 | * |
||
| 643 | * @param string $id page id |
||
| 644 | * @return int |
||
| 645 | * @throws AccessDeniedException no permissions |
||
| 646 | * @throws RemoteException file in use or not deleted |
||
| 647 | */ |
||
| 648 | public function deleteAttachment($id) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Returns the permissions of a given wiki page for the current user or another user |
||
| 666 | * |
||
| 667 | * @param string $id page id |
||
| 668 | * @param string|null $user username |
||
| 669 | * @param array|null $groups array of groups |
||
| 670 | * @return int permission level |
||
| 671 | */ |
||
| 672 | public function aclCheck($id, $user = null, $groups = null) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Lists all links contained in a wiki page |
||
| 695 | * |
||
| 696 | * @author Michael Klier <[email protected]> |
||
| 697 | * |
||
| 698 | * @param string $id page id |
||
| 699 | * @return array |
||
| 700 | * @throws AccessDeniedException no read access for page |
||
| 701 | */ |
||
| 702 | public function listLinks($id) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Returns a list of recent changes since give timestamp |
||
| 748 | * |
||
| 749 | * @author Michael Hamann <[email protected]> |
||
| 750 | * @author Michael Klier <[email protected]> |
||
| 751 | * |
||
| 752 | * @param int $timestamp unix timestamp |
||
| 753 | * @return array |
||
| 754 | * @throws RemoteException no valid timestamp |
||
| 755 | */ |
||
| 756 | public function getRecentChanges($timestamp) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Returns a list of recent media changes since give timestamp |
||
| 787 | * |
||
| 788 | * @author Michael Hamann <[email protected]> |
||
| 789 | * @author Michael Klier <[email protected]> |
||
| 790 | * |
||
| 791 | * @param int $timestamp unix timestamp |
||
| 792 | * @return array |
||
| 793 | * @throws RemoteException no valid timestamp |
||
| 794 | */ |
||
| 795 | public function getRecentMediaChanges($timestamp) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Returns a list of available revisions of a given wiki page |
||
| 825 | * Number of returned pages is set by $conf['recent'] |
||
| 826 | * However not accessible pages are skipped, so less than $conf['recent'] could be returned |
||
| 827 | * |
||
| 828 | * @author Michael Klier <[email protected]> |
||
| 829 | * |
||
| 830 | * @param string $id page id |
||
| 831 | * @param int $first skip the first n changelog lines |
||
| 832 | * 0 = from current(if exists) |
||
| 833 | * 1 = from 1st old rev |
||
| 834 | * 2 = from 2nd old rev, etc |
||
| 835 | * @return array |
||
| 836 | * @throws AccessDeniedException no read access for page |
||
| 837 | * @throws RemoteException empty id |
||
| 838 | */ |
||
| 839 | public function pageVersions($id, $first) |
||
| 893 | |||
| 894 | /** |
||
| 895 | * The version of Wiki RPC API supported |
||
| 896 | */ |
||
| 897 | public function wikiRpcVersion() |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Locks or unlocks a given batch of pages |
||
| 904 | * |
||
| 905 | * Give an associative array with two keys: lock and unlock. Both should contain a |
||
| 906 | * list of pages to lock or unlock |
||
| 907 | * |
||
| 908 | * Returns an associative array with the keys locked, lockfail, unlocked and |
||
| 909 | * unlockfail, each containing lists of pages. |
||
| 910 | * |
||
| 911 | * @param array[] $set list pages with array('lock' => array, 'unlock' => array) |
||
| 912 | * @return array |
||
| 913 | */ |
||
| 914 | public function setLocks($set) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Return API version |
||
| 950 | * |
||
| 951 | * @return int |
||
| 952 | */ |
||
| 953 | public function getAPIVersion() |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Login |
||
| 960 | * |
||
| 961 | * @param string $user |
||
| 962 | * @param string $pass |
||
| 963 | * @return int |
||
| 964 | */ |
||
| 965 | public function login($user, $pass) |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Log off |
||
| 993 | * |
||
| 994 | * @return int |
||
| 995 | */ |
||
| 996 | public function logoff() |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Resolve page id |
||
| 1010 | * |
||
| 1011 | * @param string $id page id |
||
| 1012 | * @return string |
||
| 1013 | */ |
||
| 1014 | private function resolvePageId($id) |
||
| 1023 | } |
||
| 1024 |
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.