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 Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Page extends Model |
||
| 12 | { |
||
| 13 | |||
| 14 | /** @var Project The project that this page belongs to. */ |
||
| 15 | protected $project; |
||
| 16 | |||
| 17 | /** @var string The page name as provided at instantiation. */ |
||
| 18 | protected $unnormalizedPageName; |
||
| 19 | |||
| 20 | /** @var string[] Metadata about this page. */ |
||
| 21 | protected $pageInfo; |
||
| 22 | |||
| 23 | /** @var string[] Revision history of this page. */ |
||
| 24 | protected $revisions; |
||
| 25 | |||
| 26 | /** @var int Number of revisions for this page. */ |
||
| 27 | protected $numRevisions; |
||
| 28 | |||
| 29 | /** @var string[] List of Wikidata sitelinks for this page. */ |
||
| 30 | protected $wikidataItems; |
||
| 31 | |||
| 32 | /** @var int Number of Wikidata sitelinks for this page. */ |
||
| 33 | protected $numWikidataItems; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Page constructor. |
||
| 37 | * @param Project $project |
||
| 38 | * @param string $pageName |
||
| 39 | */ |
||
| 40 | 31 | public function __construct(Project $project, $pageName) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Unique identifier for this Page, to be used in cache keys. |
||
| 48 | * Use of md5 ensures the cache key does not contain reserved characters. |
||
| 49 | * @see Repository::getCacheKey() |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | public function getCacheKey() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get basic information about this page from the repository. |
||
| 59 | * @return \string[] |
||
| 60 | */ |
||
| 61 | 9 | protected function getPageInfo() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get the page's title. |
||
| 72 | * @param bool $useUnnormalized Use the unnormalized page title to avoid an |
||
| 73 | * API call. This should be used only if you fetched the page title via |
||
| 74 | * other means (SQL query), and is not from user input alone. |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 3 | public function getTitle($useUnnormalized = false) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get the page's title without the namespace. |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 1 | public function getTitleWithoutNamespace() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get this page's database ID. |
||
| 100 | * @return int |
||
| 101 | */ |
||
| 102 | 2 | public function getId() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Get this page's length in bytes. |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | 1 | public function getLength() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get HTML for the stylized display of the title. |
||
| 120 | * The text will be the same as Page::getTitle(). |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 1 | public function getDisplayTitle() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Get the full URL of this page. |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 1 | public function getUrl() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Get the numerical ID of the namespace of this page. |
||
| 144 | * @return int |
||
| 145 | */ |
||
| 146 | 2 | public function getNamespace() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Get the name of the namespace of this page. |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 1 | public function getNamespaceName() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get the number of page watchers. |
||
| 166 | * @return int |
||
| 167 | */ |
||
| 168 | 1 | public function getWatchers() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Whether or not this page exists. |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 1 | public function exists() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get the Project to which this page belongs. |
||
| 186 | * @return Project |
||
| 187 | */ |
||
| 188 | 13 | public function getProject() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get the language code for this page. |
||
| 195 | * If not set, the language code for the project is returned. |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 2 | public function getLang() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get the Wikidata ID of this page. |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 4 | public function getWikidataId() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get the number of revisions the page has. |
||
| 224 | * @param User $user Optionally limit to those of this user. |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | 4 | public function getNumRevisions(User $user = null) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get all edits made to this page. |
||
| 252 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function getRevisions(User $user = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get the full page wikitext. |
||
| 268 | * @return string|null Null if nothing was found. |
||
| 269 | */ |
||
| 270 | 1 | public function getWikitext() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Get the statement for a single revision, so that you can iterate row by row. |
||
| 284 | * @see PagesRepository::getRevisionsStmt() |
||
| 285 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 286 | * @param int $limit Max number of revisions to process. |
||
| 287 | * @param int $numRevisions Number of revisions, if known. This is used solely to determine the |
||
| 288 | * OFFSET if we are given a $limit. If $limit is set and $numRevisions is not set, a |
||
| 289 | * separate query is ran to get the nuber of revisions. |
||
| 290 | * @return Doctrine\DBAL\Driver\PDOStatement |
||
| 291 | */ |
||
| 292 | public function getRevisionsStmt(User $user = null, $limit = null, $numRevisions = null) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get various basic info used in the API, including the |
||
| 304 | * number of revisions, unique authors, initial author |
||
| 305 | * and edit count of the initial author. |
||
| 306 | * This is combined into one query for better performance. |
||
| 307 | * Caching is intentionally disabled, because using the gadget, |
||
| 308 | * this will get hit for a different page constantly, where |
||
| 309 | * the likelihood of cache benefiting us is slim. |
||
| 310 | * @return string[] |
||
| 311 | */ |
||
| 312 | public function getBasicEditingInfo() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get assessments of this page |
||
| 319 | * @see https://www.mediawiki.org/wiki/Extension:PageAssessments |
||
| 320 | * @return string[]|false `false` if unsupported, or array in the format of: |
||
| 321 | * [ |
||
| 322 | * 'assessment' => 'C', // overall assessment |
||
| 323 | * 'wikiprojects' => [ |
||
| 324 | * 'Biography' => [ |
||
| 325 | * 'assessment' => 'C', |
||
| 326 | * 'badge' => 'url', |
||
| 327 | * ], |
||
| 328 | * ... |
||
| 329 | * ], |
||
| 330 | * 'wikiproject_prefix' => 'Wikipedia:WikiProject_', |
||
| 331 | * ] |
||
| 332 | */ |
||
| 333 | 1 | public function getAssessments() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Get CheckWiki errors for this page |
||
| 411 | * @return string[] See getErrors() for format |
||
| 412 | */ |
||
| 413 | 1 | public function getCheckWikiErrors() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get Wikidata errors for this page |
||
| 420 | * @return string[] See getErrors() for format |
||
| 421 | */ |
||
| 422 | 2 | public function getWikidataErrors() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get Wikidata and CheckWiki errors, if present |
||
| 463 | * @return string[] List of errors in the format: |
||
| 464 | * [[ |
||
| 465 | * 'prio' => int, |
||
| 466 | * 'name' => string, |
||
| 467 | * 'notice' => string (HTML), |
||
| 468 | * 'explanation' => string (HTML) |
||
| 469 | * ], ... ] |
||
| 470 | */ |
||
| 471 | 1 | public function getErrors() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Get all wikidata items for the page, not just languages of sister projects |
||
| 483 | * @return int Number of records. |
||
| 484 | */ |
||
| 485 | 1 | public function getWikidataItems() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Count wikidata items for the page, not just languages of sister projects |
||
| 495 | * @return int Number of records. |
||
| 496 | */ |
||
| 497 | 2 | public function countWikidataItems() |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Get number of in and outgoing links and redirects to this page. |
||
| 509 | * @return string[] Counts with the keys 'links_ext_count', 'links_out_count', |
||
| 510 | * 'links_in_count' and 'redirects_count' |
||
| 511 | */ |
||
| 512 | 2 | public function countLinksAndRedirects() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Get the sum of pageviews for the given page and timeframe. |
||
| 519 | * @param string|DateTime $start In the format YYYYMMDD |
||
| 520 | * @param string|DateTime $end In the format YYYYMMDD |
||
| 521 | * @return string[] |
||
| 522 | */ |
||
| 523 | 1 | public function getPageviews($start, $end) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Get the sum of pageviews over the last N days |
||
| 539 | * @param int $days Default 30 |
||
| 540 | * @return int Number of pageviews |
||
| 541 | */ |
||
| 542 | 1 | public function getLastPageviews($days = 30) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Is the page the project's Main Page? |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | 1 | public function isMainPage() |
|
| 557 | } |
||
| 558 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.