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 |
||
| 8 | class Project extends Model |
||
| 9 | { |
||
| 10 | |||
| 11 | /** @var string The project name as supplied by the user. */ |
||
| 12 | protected $nameUnnormalized; |
||
| 13 | |||
| 14 | /** @var string[] Basic metadata about the project */ |
||
| 15 | protected $metadata; |
||
| 16 | |||
| 17 | public function __construct($nameOrUrl) |
||
| 21 | |||
| 22 | protected function getMetadata() |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Does this project exist? |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | public function exists() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The unique domain name of this project, without protocol or path components. |
||
| 41 | * This should be used as the canonical project identifier. |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function getDomain() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The name of the database for this project. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getDatabaseName() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getUrl() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The base URL path of this project (that page titles are appended to). |
||
| 73 | * |
||
| 74 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function getArticlePath() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * The URL path to index.php |
||
| 88 | * |
||
| 89 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function getScriptPath() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Get this project's title, the human-language full title of the wiki (e.g. "English |
||
| 103 | * Wikipedia" or |
||
| 104 | */ |
||
| 105 | public function getTitle() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get an array of this project's namespaces and their IDs. |
||
| 113 | * |
||
| 114 | * @return string[] Keys are IDs, values are names. |
||
| 115 | */ |
||
| 116 | public function getNamespaces() |
||
| 121 | } |
||
| 122 |
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.