Complex classes like PagesRepository 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 PagesRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class PagesRepository extends Repository |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get metadata about a single page from the API. |
||
| 20 | * @param Project $project The project to which the page belongs. |
||
| 21 | * @param string $pageTitle Page title. |
||
| 22 | * @return string[] Array with some of the following keys: pageid, title, missing, displaytitle, |
||
| 23 | * url. |
||
| 24 | */ |
||
| 25 | public function getPageInfo(Project $project, $pageTitle) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get metadata about a set of pages from the API. |
||
| 33 | * @param Project $project The project to which the pages belong. |
||
| 34 | * @param string[] $pageTitles Array of page titles. |
||
| 35 | * @return string[] Array keyed by the page names, each element with some of the |
||
| 36 | * following keys: pageid, title, missing, displaytitle, url. |
||
| 37 | */ |
||
| 38 | public function getPagesInfo(Project $project, $pageTitles) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the full page text of a set of pages. |
||
| 66 | * @param Project $project The project to which the pages belong. |
||
| 67 | * @param string[] $pageTitles Array of page titles. |
||
| 68 | * @return string[] Array keyed by the page names, with the page text as the values. |
||
| 69 | */ |
||
| 70 | public function getPagesWikitext(Project $project, $pageTitles) |
||
| 71 | { |
||
| 72 | $query = new SimpleRequest('query', [ |
||
| 73 | 'prop' => 'revisions', |
||
| 74 | 'rvprop' => 'content', |
||
| 75 | 'titles' => join('|', $pageTitles), |
||
| 76 | 'formatversion' => 2, |
||
| 77 | ]); |
||
| 78 | $result = []; |
||
| 79 | |||
| 80 | $api = $this->getMediawikiApi($project); |
||
| 81 | $res = $api->getRequest($query); |
||
| 82 | |||
| 83 | if (!isset($res['query']['pages'])) { |
||
| 84 | return []; |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach ($res['query']['pages'] as $page) { |
||
| 88 | if (isset($page['revisions'][0]['content'])) { |
||
| 89 | $result[$page['title']] = $page['revisions'][0]['content']; |
||
| 90 | } else { |
||
| 91 | $result[$page['title']] = ''; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return $result; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get revisions of a single page. |
||
| 100 | * @param Page $page The page. |
||
| 101 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 102 | * @return string[] Each member with keys: id, timestamp, length- |
||
| 103 | */ |
||
| 104 | public function getRevisions(Page $page, User $user = null) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the statement for a single revision, so that you can iterate row by row. |
||
| 132 | * @param Page $page The page. |
||
| 133 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 134 | * @param int $limit Max number of revisions to process. |
||
| 135 | * @param int $numRevisions Number of revisions, if known. This is used solely to determine the |
||
| 136 | * OFFSET if we are given a $limit (see below). If $limit is set and $numRevisions is not set, |
||
| 137 | * a separate query is ran to get the nuber of revisions. |
||
| 138 | * @return Doctrine\DBAL\Driver\PDOStatement |
||
| 139 | */ |
||
| 140 | public function getRevisionsStmt(Page $page, User $user = null, $limit = null, $numRevisions = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get a count of the number of revisions of a single page |
||
| 181 | * @param Page $page The page. |
||
| 182 | * @param User|null $user Specify to only count revisions by the given user. |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public function getNumRevisions(Page $page, User $user = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get various basic info used in the API, including the |
||
| 203 | * number of revisions, unique authors, initial author |
||
| 204 | * and edit count of the initial author. |
||
| 205 | * This is combined into one query for better performance. |
||
| 206 | * Caching is only applied if it took considerable time to process, |
||
| 207 | * because using the gadget, this will get hit for a different page |
||
| 208 | * constantly, where the likelihood of cache benefiting us is slim. |
||
| 209 | * @param Page $page The page. |
||
| 210 | * @return string[] |
||
| 211 | */ |
||
| 212 | public function getBasicEditingInfo(Page $page) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get assessment data for the given pages |
||
| 280 | * @param Project $project The project to which the pages belong. |
||
| 281 | * @param int[] $pageIds Page IDs |
||
| 282 | * @return string[] Assessment data as retrieved from the database. |
||
| 283 | */ |
||
| 284 | public function getAssessments(Project $project, $pageIds) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get any CheckWiki errors of a single page |
||
| 304 | * @param Page $page |
||
| 305 | * @return array Results from query |
||
| 306 | */ |
||
| 307 | public function getCheckWikiErrors(Page $page) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get basic wikidata on the page: label and description. |
||
| 339 | * @param Page $page |
||
| 340 | * @return string[] In the format: |
||
| 341 | * [[ |
||
| 342 | * 'term' => string such as 'label', |
||
| 343 | * 'term_text' => string (value for 'label'), |
||
| 344 | * ], ... ] |
||
| 345 | */ |
||
| 346 | public function getWikidataInfo(Page $page) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get or count all wikidata items for the given page, |
||
| 388 | * not just languages of sister projects |
||
| 389 | * @param Page $page |
||
| 390 | * @param bool $count Set to true to get only a COUNT |
||
| 391 | * @return string[]|int Records as returend by the DB, |
||
| 392 | * or raw COUNT of the records. |
||
| 393 | */ |
||
| 394 | public function getWikidataItems(Page $page, $count = false) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get number of in and outgoing links and redirects to the given page. |
||
| 417 | * @param Page $page |
||
| 418 | * @return string[] Counts with the keys 'links_ext_count', 'links_out_count', |
||
| 419 | * 'links_in_count' and 'redirects_count' |
||
| 420 | */ |
||
| 421 | public function countLinksAndRedirects(Page $page) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Count wikidata items for the given page, not just languages of sister projects |
||
| 460 | * @param Page $page |
||
| 461 | * @return int Number of records. |
||
| 462 | */ |
||
| 463 | public function countWikidataItems(Page $page) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get page views for the given page and timeframe. |
||
| 470 | * @FIXME use Symfony Guzzle package. |
||
| 471 | * @param Page $page |
||
| 472 | * @param string|DateTime $start In the format YYYYMMDD |
||
| 473 | * @param string|DateTime $end In the format YYYYMMDD |
||
| 474 | * @return string[] |
||
| 475 | */ |
||
| 476 | public function getPageviews(Page $page, $start, $end) |
||
| 496 | } |
||
| 497 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.