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 | * The unique domain name of this project, without protocol or path components. |
||
| 32 | * This should be used as the canonical project identifier. |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public function getDomain() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The name of the database for this project. |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function getDatabaseName() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The project URL is the fully-qualified domain name, with protocol and trailing slash. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getUrl() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The base URL path of this project (that page titles are appended to). |
||
| 63 | * |
||
| 64 | * @link https://www.mediawiki.org/wiki/Manual:$wgArticlePath |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function getArticlePath() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * The URL path to index.php |
||
| 78 | * |
||
| 79 | * @link https://www.mediawiki.org/wiki/Manual:$wgScriptPath |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function getScriptPath() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Get this project's title, the human-language full title of the wiki (e.g. "English |
||
| 93 | * Wikipedia" or |
||
| 94 | */ |
||
| 95 | public function getTitle() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get an array of this project's namespaces and their IDs. |
||
| 103 | * |
||
| 104 | * @return string[] Keys are IDs, values are names. |
||
| 105 | */ |
||
| 106 | public function getNamespaces() |
||
| 111 | } |
||
| 112 |
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.