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 |
||
| 11 | class Project extends Model |
||
| 12 | { |
||
| 13 | |||
| 14 | /** @var string The project name as supplied by the user. */ |
||
| 15 | protected $nameUnnormalized; |
||
| 16 | |||
| 17 | /** @var string[] Basic metadata about the project */ |
||
| 18 | protected $metadata; |
||
| 19 | |||
| 20 | public function __construct($nameOrUrl) |
||
| 24 | |||
| 25 | protected function getMetadata() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Does this project exist? |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | public function exists() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The unique domain name of this project, without protocol or path components. |
||
| 44 | * This should be used as the canonical project identifier. |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function getDomain() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The name of the database for this project. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function getDatabaseName() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The language for this project. |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function getLang() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 76 | * |
||
| 77 | * @param bool $withTrailingSlash Whether to append a slash. |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getUrl($withTrailingSlash = true) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get a MediawikiApi object for this Project. |
||
| 87 | * |
||
| 88 | * @return MediawikiApi |
||
| 89 | */ |
||
| 90 | public function getApi() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The base URL path of this project (that page titles are appended to). |
||
| 97 | * |
||
| 98 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function getArticlePath() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * The URL path of the directory that contains index.php, with no trailing slash. |
||
| 112 | * Defaults to '/w' which is the same as the normal WMF set-up. |
||
| 113 | * |
||
| 114 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function getScriptPath() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get this project's title, the human-language full title of the wiki (e.g. "English |
||
| 128 | * Wikipedia" or |
||
| 129 | */ |
||
| 130 | public function getTitle() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get an array of this project's namespaces and their IDs. |
||
| 138 | * |
||
| 139 | * @return string[] Keys are IDs, values are names. |
||
| 140 | */ |
||
| 141 | public function getNamespaces() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the name of the page on this project that the user must create in order to opt in for |
||
| 149 | * restricted statistics display. |
||
| 150 | * @param User $user |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function userOptInPage(User $user) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Has a user opted in to having their restricted statistics displayed to anyone? |
||
| 161 | * @param User $user |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function userHasOptedIn(User $user) |
||
| 194 | } |
||
| 195 |
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.