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:
| 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 | /** |
||
| 24 | * Create a new Project. |
||
| 25 | * @param string $nameOrUrl The project's database name or URL. |
||
| 26 | */ |
||
| 27 | public function __construct($nameOrUrl) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get basic metadata about the project. |
||
| 34 | * @return \string[] |
||
| 35 | */ |
||
| 36 | protected function getMetadata() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Does this project exist? |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public function exists() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The unique domain name of this project, without protocol or path components. |
||
| 55 | * This should be used as the canonical project identifier. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function getDomain() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The name of the database for this project. |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getDatabaseName() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The language for this project. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getLang() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 87 | * |
||
| 88 | * @param bool $withTrailingSlash Whether to append a slash. |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getUrl($withTrailingSlash = true) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get a MediawikiApi object for this Project. |
||
| 98 | * |
||
| 99 | * @return MediawikiApi |
||
| 100 | */ |
||
| 101 | public function getApi() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The base URL path of this project (that page titles are appended to). |
||
| 108 | * For some wikis the title (apparently) may not be at the end. |
||
| 109 | * Replace $1 with the article name. |
||
| 110 | * |
||
| 111 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function getArticlePath() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * The URL path of the directory that contains index.php, with no trailing slash. |
||
| 125 | * Defaults to '/w' which is the same as the normal WMF set-up. |
||
| 126 | * |
||
| 127 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function getScriptPath() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * The URL path to index.php |
||
| 141 | * Defaults to '/w/index.php' which is the same as the normal WMF set-up. |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function getScript() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * The full URL to api.php |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getApiUrl() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get this project's title, the human-language full title of the wiki (e.g. "English |
||
| 165 | * Wikipedia" or |
||
| 166 | */ |
||
| 167 | public function getTitle() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get an array of this project's namespaces and their IDs. |
||
| 175 | * |
||
| 176 | * @return string[] Keys are IDs, values are names. |
||
| 177 | */ |
||
| 178 | public function getNamespaces() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the name of the page on this project that the user must create in order to opt in for |
||
| 186 | * restricted statistics display. |
||
| 187 | * @param User $user |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function userOptInPage(User $user) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Has a user opted in to having their restricted statistics displayed to anyone? |
||
| 198 | * @param User $user |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function userHasOptedIn(User $user) |
||
| 238 | } |
||
| 239 |
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.