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 statement for a single revision, |
||
| 231 | * so that you can iterate row by row. |
||
| 232 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 233 | * @return Doctrine\DBAL\Driver\PDOStatement |
||
| 234 | */ |
||
| 235 | public function getRevisionsStmt(User $user = null) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get various basic info used in the API, including the |
||
| 242 | * number of revisions, unique authors, initial author |
||
| 243 | * and edit count of the initial author. |
||
| 244 | * This is combined into one query for better performance. |
||
| 245 | * Caching is intentionally disabled, because using the gadget, |
||
| 246 | * this will get hit for a different page constantly, where |
||
| 247 | * the likelihood of cache benefiting us is slim. |
||
| 248 | * @return string[] |
||
| 249 | */ |
||
| 250 | public function getBasicEditingInfo() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get assessments of this page |
||
| 257 | * @return string[]|false `false` if unsupported, or array in the format of: |
||
| 258 | * [ |
||
| 259 | * 'assessment' => 'C', // overall assessment |
||
| 260 | * 'wikiprojects' => [ |
||
| 261 | * 'Biography' => [ |
||
| 262 | * 'assessment' => 'C', |
||
| 263 | * 'badge' => 'url', |
||
| 264 | * ], |
||
| 265 | * ... |
||
| 266 | * ], |
||
| 267 | * 'wikiproject_prefix' => 'Wikipedia:WikiProject_', |
||
| 268 | * ] |
||
| 269 | */ |
||
| 270 | public function getAssessments() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get CheckWiki errors for this page |
||
| 347 | * @return string[] See getErrors() for format |
||
| 348 | */ |
||
| 349 | public function getCheckWikiErrors() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get Wikidata errors for this page |
||
| 356 | * @return string[] See getErrors() for format |
||
| 357 | */ |
||
| 358 | public function getWikidataErrors() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get Wikidata and CheckWiki errors, if present |
||
| 399 | * @return string[] List of errors in the format: |
||
| 400 | * [[ |
||
| 401 | * 'prio' => int, |
||
| 402 | * 'name' => string, |
||
| 403 | * 'notice' => string (HTML), |
||
| 404 | * 'explanation' => string (HTML) |
||
| 405 | * ], ... ] |
||
| 406 | */ |
||
| 407 | public function getErrors() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get all wikidata items for the page, not just languages of sister projects |
||
| 419 | * @return int Number of records. |
||
| 420 | */ |
||
| 421 | public function getWikidataItems() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Count wikidata items for the page, not just languages of sister projects |
||
| 428 | * @return int Number of records. |
||
| 429 | */ |
||
| 430 | public function countWikidataItems() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Get number of in and outgoing links and redirects to this page. |
||
| 437 | * @return string[] Counts with the keys 'links_ext_count', 'links_out_count', |
||
| 438 | * 'links_in_count' and 'redirects_count' |
||
| 439 | */ |
||
| 440 | public function countLinksAndRedirects() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the sum of pageviews for the given page and timeframe. |
||
| 447 | * @param string|DateTime $start In the format YYYYMMDD |
||
| 448 | * @param string|DateTime $end In the format YYYYMMDD |
||
| 449 | * @return string[] |
||
| 450 | */ |
||
| 451 | public function getPageviews($start, $end) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get the sum of pageviews over the last N days |
||
| 467 | * @param int [$days] Default 30 |
||
| 468 | * @return int Number of pageviews |
||
| 469 | */ |
||
| 470 | public function getLastPageviews($days = 30) |
||
| 476 | } |
||
| 477 |
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: