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 |
||
| 10 | class Project extends Model |
||
| 11 | { |
||
| 12 | |||
| 13 | /** @var string The project name as supplied by the user. */ |
||
| 14 | protected $nameUnnormalized; |
||
| 15 | |||
| 16 | /** @var string[] Basic metadata about the project */ |
||
| 17 | protected $metadata; |
||
| 18 | |||
| 19 | public function __construct($nameOrUrl) |
||
| 23 | |||
| 24 | protected function getMetadata() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Does this project exist? |
||
| 34 | * @return bool |
||
| 35 | */ |
||
| 36 | public function exists() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The unique domain name of this project, without protocol or path components. |
||
| 43 | * This should be used as the canonical project identifier. |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function getDomain() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The name of the database for this project. |
||
| 55 | * |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getDatabaseName() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function getUrl() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get a MediawikiApi object for this Project. |
||
| 75 | * |
||
| 76 | * @return MediawikiApi |
||
| 77 | */ |
||
| 78 | public function getApi() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The base URL path of this project (that page titles are appended to). |
||
| 85 | * |
||
| 86 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function getArticlePath() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * The URL path to index.php |
||
| 100 | * |
||
| 101 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | View Code Duplication | public function getScriptPath() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Get this project's title, the human-language full title of the wiki (e.g. "English |
||
| 115 | * Wikipedia" or |
||
| 116 | */ |
||
| 117 | public function getTitle() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get an array of this project's namespaces and their IDs. |
||
| 125 | * |
||
| 126 | * @return string[] Keys are IDs, values are names. |
||
| 127 | */ |
||
| 128 | public function getNamespaces() |
||
| 133 | } |
||
| 134 |
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.