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 Page extends Model |
||
| 12 | { |
||
| 13 | |||
| 14 | /** @var Project The project that this page belongs to. */ |
||
| 15 | protected $project; |
||
| 16 | |||
| 17 | /** @var string The page name as provided at instantiation. */ |
||
| 18 | protected $unnormalizedPageName; |
||
| 19 | |||
| 20 | /** @var string[] Metadata about this page. */ |
||
| 21 | protected $pageInfo; |
||
| 22 | |||
| 23 | /** @var string[] Revision history of this page */ |
||
| 24 | protected $revisions; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Page constructor. |
||
| 28 | * @param Project $project |
||
| 29 | * @param string $pageName |
||
| 30 | */ |
||
| 31 | public function __construct(Project $project, $pageName) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get basic information about this page from the repository. |
||
| 39 | * @return \string[] |
||
| 40 | */ |
||
| 41 | protected function getPageInfo() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the page's title. |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function getTitle() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get this page's database ID. |
||
| 62 | * @return int |
||
| 63 | */ |
||
| 64 | public function getId() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get this page's length in bytes. |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | public function getLength() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get HTML for the stylized display of the title. |
||
| 82 | * The text will be the same as Page::getTitle(). |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | public function getDisplayTitle() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the full URL of this page. |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getUrl() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the numerical ID of the namespace of this page. |
||
| 106 | * @return int |
||
| 107 | */ |
||
| 108 | public function getNamespace() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the number of page watchers. |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | public function getWatchers() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Whether or not this page exists. |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function exists() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the Project to which this page belongs. |
||
| 136 | * @return Project |
||
| 137 | */ |
||
| 138 | public function getProject() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the Wikidata ID of this page. |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getWikidataId() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get the number of revisions the page has. |
||
| 159 | * @param User $user Optionally limit to those of this user. |
||
| 160 | * @return int |
||
| 161 | */ |
||
| 162 | public function getNumRevisions(User $user = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get all edits made to this page. |
||
| 176 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function getRevisions(User $user = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get assessments of this page |
||
| 204 | * @return string[]|false `false` if unsupported, or array in the format of: |
||
| 205 | * [ |
||
| 206 | * 'assessment' => 'C', // overall assessment |
||
| 207 | * 'wikiprojects' => [ |
||
| 208 | * 'Biography' => [ |
||
| 209 | * 'assessment' => 'C', |
||
| 210 | * 'badge' => 'url', |
||
| 211 | * ], |
||
| 212 | * ... |
||
| 213 | * ], |
||
| 214 | * 'wikiproject_prefix' => 'Wikipedia:WikiProject_', |
||
| 215 | * ] |
||
| 216 | */ |
||
| 217 | public function getAssessments() |
||
| 285 | } |
||
| 286 |
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.