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 | public function __construct(Project $project, $pageName) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get basic information about this page from the repository. |
||
| 48 | * @return \string[] |
||
| 49 | */ |
||
| 50 | protected function getPageInfo() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get the page's title. |
||
| 61 | * @param bool $useUnnormalized Use the unnormalized page title to avoid an |
||
| 62 | * API call. This should be used only if you fetched the page title via |
||
| 63 | * other means (SQL query), and is not from user input alone. |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getTitle($useUnnormalized = false) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get the page's title without the namespace. |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function getTitleWithoutNamespace() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get this page's database ID. |
||
| 89 | * @return int |
||
| 90 | */ |
||
| 91 | public function getId() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get this page's length in bytes. |
||
| 99 | * @return int |
||
| 100 | */ |
||
| 101 | public function getLength() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get HTML for the stylized display of the title. |
||
| 109 | * The text will be the same as Page::getTitle(). |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getDisplayTitle() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the full URL of this page. |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getUrl() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the numerical ID of the namespace of this page. |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | public function getNamespace() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the name of the namespace of this page. |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getNamespaceName() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the number of page watchers. |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | public function getWatchers() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Whether or not this page exists. |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function exists() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the Project to which this page belongs. |
||
| 175 | * @return Project |
||
| 176 | */ |
||
| 177 | public function getProject() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the language code for this page. |
||
| 184 | * If not set, the language code for the project is returned. |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getLang() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the Wikidata ID of this page. |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getWikidataId() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the number of revisions the page has. |
||
| 213 | * @param User $user Optionally limit to those of this user. |
||
| 214 | * @return int |
||
| 215 | */ |
||
| 216 | public function getNumRevisions(User $user = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get all edits made to this page. |
||
| 241 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function getRevisions(User $user = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the full page wikitext. |
||
| 257 | * @return string|null Null if nothing was found. |
||
| 258 | */ |
||
| 259 | public function getWikitext() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the statement for a single revision, so that you can iterate row by row. |
||
| 273 | * @see PagesRepository::getRevisionsStmt() |
||
| 274 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 275 | * @param int $limit Max number of revisions to process. |
||
| 276 | * @param int $numRevisions Number of revisions, if known. This is used solely to determine the |
||
| 277 | * OFFSET if we are given a $limit. If $limit is set and $numRevisions is not set, a |
||
| 278 | * separate query is ran to get the nuber of revisions. |
||
| 279 | * @return Doctrine\DBAL\Driver\PDOStatement |
||
| 280 | */ |
||
| 281 | public function getRevisionsStmt(User $user = null, $limit = null, $numRevisions = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get various basic info used in the API, including the |
||
| 293 | * number of revisions, unique authors, initial author |
||
| 294 | * and edit count of the initial author. |
||
| 295 | * This is combined into one query for better performance. |
||
| 296 | * Caching is intentionally disabled, because using the gadget, |
||
| 297 | * this will get hit for a different page constantly, where |
||
| 298 | * the likelihood of cache benefiting us is slim. |
||
| 299 | * @return string[] |
||
| 300 | */ |
||
| 301 | public function getBasicEditingInfo() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get assessments of this page |
||
| 308 | * @see https://www.mediawiki.org/wiki/Extension:PageAssessments |
||
| 309 | * @return string[]|false `false` if unsupported, or array in the format of: |
||
| 310 | * [ |
||
| 311 | * 'assessment' => 'C', // overall assessment |
||
| 312 | * 'wikiprojects' => [ |
||
| 313 | * 'Biography' => [ |
||
| 314 | * 'assessment' => 'C', |
||
| 315 | * 'badge' => 'url', |
||
| 316 | * ], |
||
| 317 | * ... |
||
| 318 | * ], |
||
| 319 | * 'wikiproject_prefix' => 'Wikipedia:WikiProject_', |
||
| 320 | * ] |
||
| 321 | */ |
||
| 322 | public function getAssessments() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get CheckWiki errors for this page |
||
| 400 | * @return string[] See getErrors() for format |
||
| 401 | */ |
||
| 402 | public function getCheckWikiErrors() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get Wikidata errors for this page |
||
| 409 | * @return string[] See getErrors() for format |
||
| 410 | */ |
||
| 411 | public function getWikidataErrors() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get Wikidata and CheckWiki errors, if present |
||
| 452 | * @return string[] List of errors in the format: |
||
| 453 | * [[ |
||
| 454 | * 'prio' => int, |
||
| 455 | * 'name' => string, |
||
| 456 | * 'notice' => string (HTML), |
||
| 457 | * 'explanation' => string (HTML) |
||
| 458 | * ], ... ] |
||
| 459 | */ |
||
| 460 | public function getErrors() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get all wikidata items for the page, not just languages of sister projects |
||
| 472 | * @return int Number of records. |
||
| 473 | */ |
||
| 474 | public function getWikidataItems() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Count wikidata items for the page, not just languages of sister projects |
||
| 484 | * @return int Number of records. |
||
| 485 | */ |
||
| 486 | public function countWikidataItems() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get number of in and outgoing links and redirects to this page. |
||
| 498 | * @return string[] Counts with the keys 'links_ext_count', 'links_out_count', |
||
| 499 | * 'links_in_count' and 'redirects_count' |
||
| 500 | */ |
||
| 501 | public function countLinksAndRedirects() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the sum of pageviews for the given page and timeframe. |
||
| 508 | * @param string|DateTime $start In the format YYYYMMDD |
||
| 509 | * @param string|DateTime $end In the format YYYYMMDD |
||
| 510 | * @return string[] |
||
| 511 | */ |
||
| 512 | public function getPageviews($start, $end) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get the sum of pageviews over the last N days |
||
| 528 | * @param int $days Default 30 |
||
| 529 | * @return int Number of pageviews |
||
| 530 | */ |
||
| 531 | public function getLastPageviews($days = 30) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Is the page the project's Main Page? |
||
| 540 | * @return bool |
||
| 541 | */ |
||
| 542 | public function isMainPage() |
||
| 546 | } |
||
| 547 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: