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 | /** |
||
| 27 | * Page constructor. |
||
| 28 | * @param Project $project |
||
| 29 | * @param string $pageName |
||
| 30 | */ |
||
| 31 | public function __construct(Project $project, $pageName) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get basic information about this page from the repository. |
||
| 39 | * @return \string[] |
||
| 40 | */ |
||
| 41 | protected function getPageInfo() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the page's title. |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function getTitle() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the page's title without the namespace. |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function getTitleWithoutNamespace() |
||
| 65 | { |
||
| 66 | $info = $this->getPageInfo(); |
||
| 67 | $title = isset($info['title']) ? $info['title'] : $this->unnormalizedPageName; |
||
| 68 | $nsName = $this->getNamespaceName(); |
||
| 69 | return str_replace($nsName . ':', '', $title); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get this page's database ID. |
||
| 74 | * @return int |
||
| 75 | */ |
||
| 76 | public function getId() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get this page's length in bytes. |
||
| 84 | * @return int |
||
| 85 | */ |
||
| 86 | public function getLength() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get HTML for the stylized display of the title. |
||
| 94 | * The text will be the same as Page::getTitle(). |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getDisplayTitle() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the full URL of this page. |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getUrl() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the numerical ID of the namespace of this page. |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public function getNamespace() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the name of the namespace of this page. |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getNamespaceName() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the number of page watchers. |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getWatchers() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Whether or not this page exists. |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function exists() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the Project to which this page belongs. |
||
| 160 | * @return Project |
||
| 161 | */ |
||
| 162 | public function getProject() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the language code for this page. |
||
| 169 | * If not set, the language code for the project is returned. |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getLang() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the Wikidata ID of this page. |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getWikidataId() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the number of revisions the page has. |
||
| 198 | * @param User $user Optionally limit to those of this user. |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | public function getNumRevisions(User $user = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get all edits made to this page. |
||
| 215 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function getRevisions(User $user = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the full page wikitext. |
||
| 231 | * @return string|null Null if nothing was found. |
||
| 232 | */ |
||
| 233 | public function getWikitext() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the statement for a single revision, |
||
| 247 | * so that you can iterate row by row. |
||
| 248 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 249 | * @return Doctrine\DBAL\Driver\PDOStatement |
||
| 250 | */ |
||
| 251 | public function getRevisionsStmt(User $user = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get various basic info used in the API, including the |
||
| 258 | * number of revisions, unique authors, initial author |
||
| 259 | * and edit count of the initial author. |
||
| 260 | * This is combined into one query for better performance. |
||
| 261 | * Caching is intentionally disabled, because using the gadget, |
||
| 262 | * this will get hit for a different page constantly, where |
||
| 263 | * the likelihood of cache benefiting us is slim. |
||
| 264 | * @return string[] |
||
| 265 | */ |
||
| 266 | public function getBasicEditingInfo() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get assessments of this page |
||
| 273 | * @return string[]|false `false` if unsupported, or array in the format of: |
||
| 274 | * [ |
||
| 275 | * 'assessment' => 'C', // overall assessment |
||
| 276 | * 'wikiprojects' => [ |
||
| 277 | * 'Biography' => [ |
||
| 278 | * 'assessment' => 'C', |
||
| 279 | * 'badge' => 'url', |
||
| 280 | * ], |
||
| 281 | * ... |
||
| 282 | * ], |
||
| 283 | * 'wikiproject_prefix' => 'Wikipedia:WikiProject_', |
||
| 284 | * ] |
||
| 285 | */ |
||
| 286 | public function getAssessments() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get CheckWiki errors for this page |
||
| 363 | * @return string[] See getErrors() for format |
||
| 364 | */ |
||
| 365 | public function getCheckWikiErrors() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get Wikidata errors for this page |
||
| 372 | * @return string[] See getErrors() for format |
||
| 373 | */ |
||
| 374 | public function getWikidataErrors() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get Wikidata and CheckWiki errors, if present |
||
| 415 | * @return string[] List of errors in the format: |
||
| 416 | * [[ |
||
| 417 | * 'prio' => int, |
||
| 418 | * 'name' => string, |
||
| 419 | * 'notice' => string (HTML), |
||
| 420 | * 'explanation' => string (HTML) |
||
| 421 | * ], ... ] |
||
| 422 | */ |
||
| 423 | public function getErrors() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get all wikidata items for the page, not just languages of sister projects |
||
| 435 | * @return int Number of records. |
||
| 436 | */ |
||
| 437 | public function getWikidataItems() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Count wikidata items for the page, not just languages of sister projects |
||
| 444 | * @return int Number of records. |
||
| 445 | */ |
||
| 446 | public function countWikidataItems() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get number of in and outgoing links and redirects to this page. |
||
| 453 | * @return string[] Counts with the keys 'links_ext_count', 'links_out_count', |
||
| 454 | * 'links_in_count' and 'redirects_count' |
||
| 455 | */ |
||
| 456 | public function countLinksAndRedirects() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get the sum of pageviews for the given page and timeframe. |
||
| 463 | * @param string|DateTime $start In the format YYYYMMDD |
||
| 464 | * @param string|DateTime $end In the format YYYYMMDD |
||
| 465 | * @return string[] |
||
| 466 | */ |
||
| 467 | public function getPageviews($start, $end) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get the sum of pageviews over the last N days |
||
| 483 | * @param int [$days] Default 30 |
||
| 484 | * @return int Number of pageviews |
||
| 485 | */ |
||
| 486 | public function getLastPageviews($days = 30) |
||
| 492 | } |
||
| 493 |
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: