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 Project 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 Project, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Project extends Model |
||
| 15 | { |
||
| 16 | |||
| 17 | /** @var string The project name as supplied by the user. */ |
||
| 18 | protected $nameUnnormalized; |
||
| 19 | |||
| 20 | /** @var string[] Basic metadata about the project */ |
||
| 21 | protected $metadata; |
||
| 22 | |||
| 23 | /** @var string[] Project's 'dbName', 'url' and 'lang'. */ |
||
| 24 | protected $basicInfo; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Whether the user being queried for in this session |
||
| 28 | * has opted in to restricted statistics |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | protected $userOptedIn; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new Project. |
||
| 35 | * @param string $nameOrUrl The project's database name or URL. |
||
| 36 | */ |
||
| 37 | public function __construct($nameOrUrl) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get 'dbName', 'url' and 'lang' of the project, the relevant basic info |
||
| 44 | * we can get from the meta database. This is all you need to make |
||
| 45 | * database queries. More comprehensive metadata can be fetched with |
||
| 46 | * getMetadata() at the expense of an API call, which may be cached. |
||
| 47 | * @return string[] |
||
| 48 | */ |
||
| 49 | protected function getBasicInfo() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get full metadata about the project. See ProjectRepository::getMetadata |
||
| 59 | * for more information. |
||
| 60 | * @return string[] |
||
| 61 | */ |
||
| 62 | protected function getMetadata() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Does this project exist? |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function exists() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The unique domain name of this project, without protocol or path components. |
||
| 82 | * This should be used as the canonical project identifier. |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getDomain() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The name of the database for this project. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getDatabaseName() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The language for this project. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getLang() |
||
| 108 | { |
||
| 109 | return isset($this->getBasicInfo()['lang']) ? $this->getBasicInfo()['lang'] : ''; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 114 | * |
||
| 115 | * @param bool $withTrailingSlash Whether to append a slash. |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getUrl($withTrailingSlash = true) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get a MediawikiApi object for this Project. |
||
| 125 | * |
||
| 126 | * @return MediawikiApi |
||
| 127 | */ |
||
| 128 | public function getApi() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The base URL path of this project (that page titles are appended to). |
||
| 135 | * For some wikis the title (apparently) may not be at the end. |
||
| 136 | * Replace $1 with the article name. |
||
| 137 | * |
||
| 138 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function getArticlePath() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * The URL path of the directory that contains index.php, with no trailing slash. |
||
| 152 | * Defaults to '/w' which is the same as the normal WMF set-up. |
||
| 153 | * |
||
| 154 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function getScriptPath() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * The URL path to index.php |
||
| 168 | * Defaults to '/w/index.php' which is the same as the normal WMF set-up. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getScript() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * The full URL to api.php |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getApiUrl() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get this project's title, the human-language full title of the wiki (e.g. "English |
||
| 192 | * Wikipedia" or |
||
| 193 | */ |
||
| 194 | public function getTitle() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get an array of this project's namespaces and their IDs. |
||
| 202 | * |
||
| 203 | * @return string[] Keys are IDs, values are names. |
||
| 204 | */ |
||
| 205 | public function getNamespaces() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the title of the Main Page. |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function getMainPage() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get a list of users who are in one of the given user groups. |
||
| 225 | * @param string[] User groups to search for. |
||
| 226 | * @return string[] User groups keyed by user name. |
||
| 227 | */ |
||
| 228 | public function getUsersInGroups($groups) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the name of the page on this project that the user must create in order to opt in for |
||
| 245 | * restricted statistics display. |
||
| 246 | * @param User $user |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function userOptInPage(User $user) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Has a user opted in to having their restricted statistics displayed to anyone? |
||
| 257 | * @param User $user |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function userHasOptedIn(User $user) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Does this project support page assessments? |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function hasPageAssessments() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the image URL of the badge for the given page assessment |
||
| 310 | * @param string $class Valid classification for project, such as 'Start', 'GA', etc. |
||
| 311 | * @return string URL to image |
||
| 312 | */ |
||
| 313 | public function getAssessmentBadgeURL($class) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Normalize and quote a table name for use in SQL. |
||
| 328 | * @param string $tableName |
||
| 329 | * @param string|null $tableExtension Optional table extension, which will only get used if we're on Labs. |
||
| 330 | * @return string Fully-qualified and quoted table name. |
||
| 331 | */ |
||
| 332 | public function getTableName($tableName, $tableExtension = null) |
||
| 336 | } |
||
| 337 |
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: