Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ApiHelper 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 ApiHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ApiHelper extends HelperBase |
||
| 16 | { |
||
| 17 | /** @var MediawikiApi */ |
||
| 18 | private $api; |
||
| 19 | |||
| 20 | /** @var LabsHelper */ |
||
| 21 | private $labsHelper; |
||
| 22 | |||
| 23 | /** @var CacheItemPoolInterface */ |
||
| 24 | protected $cache; |
||
| 25 | |||
| 26 | /** @var ContainerInterface */ |
||
| 27 | protected $container; |
||
| 28 | |||
| 29 | public function __construct(ContainerInterface $container, LabsHelper $labsHelper) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Set up the MediawikiApi object for the given project. |
||
| 38 | * |
||
| 39 | * @param string $project |
||
| 40 | */ |
||
| 41 | private function setUp($project) |
||
| 42 | { |
||
| 43 | if (!$this->api instanceof MediawikiApi) { |
||
| 44 | $project = ProjectRepository::getProject($project, $this->container); |
||
|
1 ignored issue
–
show
|
|||
| 45 | $this->api = $project->getApi(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get general siteinfo and namespaces for a project and cache it. |
||
| 51 | * @param string [$project] Base project domain with or without protocal, or database name |
||
| 52 | * such as 'en.wikipedia.org', 'https://en.wikipedia.org' or 'enwiki' |
||
| 53 | * Can be left blank for single wikis. |
||
| 54 | * @return string[] with keys 'general' and 'namespaces'. General info will include 'dbName', |
||
| 55 | * 'wikiName', 'url', 'lang', 'articlePath', 'scriptPath', |
||
| 56 | * 'script', 'timezone', and 'timeOffset' |
||
| 57 | */ |
||
| 58 | public function getSiteInfo($projectName = '') |
||
| 59 | { |
||
| 60 | if ($this->container->getParameter('app.single_wiki')) { |
||
| 61 | $projectName = $this->container->getParameter('wiki_url'); |
||
| 62 | } |
||
| 63 | $project = ProjectRepository::getProject($projectName, $this->container); |
||
|
1 ignored issue
–
show
|
|||
| 64 | |||
| 65 | if (!$project->exists()) { |
||
| 66 | throw new Exception("Unable to find project '$projectName'"); |
||
| 67 | } |
||
| 68 | |||
| 69 | $cacheKey = "siteinfo." . $project->getDatabaseName(); |
||
| 70 | if ($this->cacheHas($cacheKey)) { |
||
| 71 | return $this->cacheGet($cacheKey); |
||
| 72 | } |
||
| 73 | |||
| 74 | $params = [ 'meta'=>'siteinfo', 'siprop'=>'general|namespaces' ]; |
||
| 75 | $query = new SimpleRequest('query', $params); |
||
| 76 | |||
| 77 | $result = [ |
||
| 78 | 'general' => [], |
||
| 79 | 'namespaces' => [] |
||
| 80 | ]; |
||
| 81 | |||
| 82 | try { |
||
| 83 | $res = $project->getApi()->getRequest($query); |
||
| 84 | |||
| 85 | if (isset($res['query']['general'])) { |
||
| 86 | $info = $res['query']['general']; |
||
| 87 | $result['general'] = [ |
||
| 88 | 'wikiName' => $info['sitename'], |
||
| 89 | 'dbName' => $info['wikiid'], |
||
| 90 | 'url' => $info['server'], |
||
| 91 | 'lang' => $info['lang'], |
||
| 92 | 'articlePath' => $info['articlepath'], |
||
| 93 | 'scriptPath' => $info['scriptpath'], |
||
| 94 | 'script' => $info['script'], |
||
| 95 | 'timezone' => $info['timezone'], |
||
| 96 | 'timeOffset' => $info['timeoffset'], |
||
| 97 | ]; |
||
| 98 | |||
| 99 | if ($this->container->getParameter('app.is_labs') && substr($result['general']['dbName'], -2) != '_p') { |
||
| 100 | $result['general']['dbName'] .= '_p'; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | View Code Duplication | if (isset($res['query']['namespaces'])) { |
|
| 105 | foreach ($res['query']['namespaces'] as $namespace) { |
||
| 106 | if ($namespace['id'] < 0) { |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (isset($namespace['name'])) { |
||
| 111 | $name = $namespace['name']; |
||
| 112 | } elseif (isset($namespace['*'])) { |
||
| 113 | $name = $namespace['*']; |
||
| 114 | } else { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | // FIXME: Figure out a way to i18n-ize this |
||
| 119 | if ($name === '') { |
||
| 120 | $name = 'Article'; |
||
| 121 | } |
||
| 122 | |||
| 123 | $result['namespaces'][$namespace['id']] = $name; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->cacheSave($cacheKey, $result, 'P7D'); |
||
| 128 | } catch (Exception $e) { |
||
| 129 | // The api returned an error! Ignore |
||
| 130 | } |
||
| 131 | |||
| 132 | return $result; |
||
| 133 | } |
||
| 134 | |||
| 135 | View Code Duplication | public function groups($project, $username) |
|
| 153 | |||
| 154 | View Code Duplication | public function globalGroups($project, $username) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get a list of namespaces on the given project. |
||
| 175 | * |
||
| 176 | * @param string $project such as en.wikipedia.org |
||
| 177 | * @return string[] Array of namespace IDs (keys) to names (values). |
||
| 178 | */ |
||
| 179 | public function namespaces($project) |
||
| 183 | |||
| 184 | public function getAdmins($project) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get basic info about a page via the API |
||
| 226 | * @param string $project Full domain of project (en.wikipedia.org) |
||
| 227 | * @param string $page Page title |
||
| 228 | * @param boolean $followRedir Whether or not to resolve redirects |
||
| 229 | * @return array Associative array of data |
||
| 230 | */ |
||
| 231 | public function getBasicPageInfo($project, $page, $followRedir) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get HTML display titles of a set of pages (or the normal title if there's no display title). |
||
| 268 | * This will send t/50 API requests where t is the number of titles supplied. |
||
| 269 | * @param string $project The project. |
||
| 270 | * @param string[] $pageTitles The titles to fetch. |
||
| 271 | * @return string[] Keys are the original supplied title, and values are the display titles. |
||
| 272 | */ |
||
| 273 | public function displayTitles($project, $pageTitles) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get assessments of the given pages, if a supported project |
||
| 314 | * @param string $project Project such as en.wikipedia.org |
||
| 315 | * @param string|array $pageTitles Single page title or array of titles |
||
| 316 | * @return array|null Page assessments info or null if none found |
||
| 317 | */ |
||
| 318 | public function getPageAssessments($project, $pageTitles) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the image URL of the badge for the given page assessment |
||
| 412 | * @param string $project Project such as en.wikipedia.org |
||
| 413 | * @param string $class Valid classification for project, such as 'Start', 'GA', etc. |
||
| 414 | * @return string URL to image |
||
| 415 | */ |
||
| 416 | public function getAssessmentBadgeURL($project, $class) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Fetch assessments data from config/assessments.yml and cache in static variable |
||
| 431 | * @return array Mappings of project/quality/class with badges, colors and category links |
||
| 432 | */ |
||
| 433 | private function getAssessmentsConfig() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Does the given project support page assessments? |
||
| 444 | * @param string $project Project to query, e.g. en.wikipedia.org |
||
| 445 | * @return boolean True or false |
||
| 446 | */ |
||
| 447 | public function projectHasPageAssessments($project) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Make mass API requests to MediaWiki API |
||
| 454 | * The API normally limits to 500 pages, but gives you a 'continue' value |
||
| 455 | * to finish iterating through the resource. |
||
| 456 | * Adapted from https://github.com/MusikAnimal/pageviews |
||
| 457 | * @param array $params Associative array of params to pass to API |
||
| 458 | * @param string $project Project to query, e.g. en.wikipedia.org |
||
| 459 | * @param string|func $dataKey The key for the main chunk of data, in the query hash |
||
| 460 | * (e.g. 'categorymembers' for API:Categorymembers). |
||
| 461 | * If this is a function it is given the response data, |
||
| 462 | * and expected to return the data we want to concatentate. |
||
| 463 | * @param string [$continueKey] the key to look in the continue hash, if present |
||
| 464 | * (e.g. 'cmcontinue' for API:Categorymembers) |
||
| 465 | * @param integer [$limit] Max number of pages to fetch |
||
| 466 | * @return array Associative array with data |
||
| 467 | */ |
||
| 468 | public function massApi($params, $project, $dataKey, $continueKey = 'continue', $limit = 5000) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Internal function used by massApi() to make recursive calls |
||
| 497 | * @param array &$data Everything we need to keep track of, as defined in massApi() |
||
| 498 | * @return null Nothing. $data['promise']->then is used to continue flow of |
||
| 499 | * execution after all recursive calls are complete |
||
| 500 | */ |
||
| 501 | private function massApiInternal(&$data) |
||
| 565 | } |
||
| 566 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.