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 |
||
| 13 | class News extends UrlModel implements NamedModel |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The category of the article |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | protected $category; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The subject of the news article |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $subject; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The content of the news article |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $content; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The creation date of the news article |
||
| 35 | * @var TimeDate |
||
| 36 | */ |
||
| 37 | protected $created; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The date the news article was last updated |
||
| 41 | * @var TimeDate |
||
| 42 | */ |
||
| 43 | protected $updated; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The ID of the author of the news article |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $author; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The ID of the last person to edit the news article |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | protected $editor; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The status of the news article |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $status; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The name of the database table used for queries |
||
| 65 | */ |
||
| 66 | const TABLE = "news"; |
||
| 67 | |||
| 68 | const CREATE_PERMISSION = Permission::CREATE_NEWS; |
||
| 69 | const EDIT_PERMISSION = Permission::EDIT_NEWS; |
||
| 70 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_NEWS; |
||
| 71 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_NEWS; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | 2 | protected function assignResult($news) |
|
| 77 | { |
||
| 78 | 2 | $this->category = $news['category']; |
|
| 79 | 2 | $this->subject = $news['subject']; |
|
| 80 | 2 | $this->content = $news['content']; |
|
| 81 | 2 | $this->created = TimeDate::fromMysql($news['created']); |
|
| 82 | 2 | $this->updated = TimeDate::fromMysql($news['updated']); |
|
| 83 | 2 | $this->author = $news['author']; |
|
| 84 | 2 | $this->editor = $news['editor']; |
|
| 85 | 2 | $this->status = $news['status']; |
|
| 86 | 2 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Get the author of the news article |
||
| 90 | * @return Player The author of the post |
||
| 91 | */ |
||
| 92 | 1 | public function getAuthor() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get the user ID of the author who wrote this article |
||
| 99 | * @return int The author ID |
||
| 100 | */ |
||
| 101 | public function getAuthorID() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the category of the news article |
||
| 108 | * @return NewsCategory The category of the post |
||
| 109 | */ |
||
| 110 | public function getCategory() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the database ID from the category the article belongs into |
||
| 117 | * @return int The category ID |
||
| 118 | */ |
||
| 119 | public function getCategoryID() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the content of the article |
||
| 126 | * @return string The raw content of the article |
||
| 127 | */ |
||
| 128 | public function getContent() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the time when the article was submitted |
||
| 135 | * |
||
| 136 | * @return TimeDate The article's creation time |
||
| 137 | */ |
||
| 138 | public function getCreated() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the time when the article was last updated |
||
| 145 | * |
||
| 146 | * @return TimeDate The article's last update time |
||
| 147 | */ |
||
| 148 | public function getLastEdit() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the last editor of the post |
||
| 155 | * @return Player A Player object of the last editor |
||
| 156 | */ |
||
| 157 | public function getLastEditor() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the ID of the person who last edited the article |
||
| 164 | * @return int The ID of the last editor |
||
| 165 | */ |
||
| 166 | public function getLastEditorID() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the subject of the news article |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 1 | public function getSubject() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | 1 | public function getName() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | 1 | public static function getParamName() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 1 | public static function getRouteName($action = 'show') |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Update the content of a post |
||
| 206 | * |
||
| 207 | * @param string $content The new content of the post |
||
| 208 | * |
||
| 209 | * @return self |
||
| 210 | */ |
||
| 211 | public function updateContent($content) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Update the last edit timestamp |
||
| 218 | * @return self |
||
| 219 | */ |
||
| 220 | public function updateEditTimestamp() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Update the editor of the post |
||
| 227 | * |
||
| 228 | * @param int $editorID The ID of the editor |
||
| 229 | * @return self |
||
| 230 | */ |
||
| 231 | public function updateLastEditor($editorID) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Update the category of the post |
||
| 238 | * |
||
| 239 | * @param int $categoryID The ID of the category |
||
| 240 | * @return self |
||
| 241 | */ |
||
| 242 | public function updateCategory($categoryID) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Update the status of a post |
||
| 249 | * |
||
| 250 | * @param string $status The new status of a post |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | public function updateStatus($status = 'published') |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Update the subject of a post |
||
| 260 | * |
||
| 261 | * @param string $subject The new subject of a post |
||
| 262 | * @return self |
||
| 263 | */ |
||
| 264 | public function updateSubject($subject) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | 1 | public static function getActiveStatuses() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Add a new news article |
||
| 279 | * |
||
| 280 | * @param string $subject The subject of the article |
||
| 281 | * @param string $content The content of the article |
||
| 282 | * @param int $authorID The ID of the author |
||
| 283 | * @param int $categoryId The ID of the category this article will be published under |
||
| 284 | * @param string $status The status of the article: 'published', 'disabled', or 'deleted' |
||
| 285 | * |
||
| 286 | * @internal param int $categoryID The ID of the category |
||
| 287 | * @return News An object representing the article that was just created or false if the article was not created |
||
| 288 | */ |
||
| 289 | 2 | public static function addNews($subject, $content, $authorID, $categoryId = 1, $status = 'published') |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Get all the news entries in the database that aren't disabled or deleted |
||
| 303 | * |
||
| 304 | * @param int $start The offset used when fetching matches, i.e. the starting point |
||
| 305 | * @param int $limit The amount of matches to be retrieved |
||
| 306 | * @param bool $getDrafts Whether or not to fetch drafts |
||
| 307 | * |
||
| 308 | * @return News[] An array of news objects |
||
| 309 | */ |
||
| 310 | 1 | public static function getNews($start = 0, $limit = 5, $getDrafts = false) |
|
| 311 | { |
||
| 312 | 1 | $ignoredStatuses[] = "deleted"; |
|
|
|
|||
| 313 | |||
| 314 | 1 | if (!$getDrafts) { |
|
| 315 | 1 | $ignoredStatuses[] = "draft"; |
|
| 316 | } |
||
| 317 | |||
| 318 | 1 | return self::arrayIdToModel( |
|
| 319 | 1 | self::fetchIdsFrom( |
|
| 320 | 1 | "status", $ignoredStatuses, "s", true, |
|
| 321 | 1 | "ORDER BY created DESC LIMIT $limit OFFSET $start" |
|
| 322 | ) |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get a query builder for news |
||
| 328 | * @return QueryBuilder |
||
| 329 | */ |
||
| 330 | View Code Duplication | public static function getQueryBuilder() |
|
| 342 | } |
||
| 343 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.