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 | /** @var bool Whether or not the News item is a draft */ |
||
| 58 | protected $is_draft; |
||
| 59 | |||
| 60 | const DELETED_COLUMN = 'is_deleted'; |
||
| 61 | const TABLE = "news"; |
||
| 62 | |||
| 63 | const CREATE_PERMISSION = Permission::CREATE_NEWS; |
||
| 64 | const EDIT_PERMISSION = Permission::EDIT_NEWS; |
||
| 65 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_NEWS; |
||
| 66 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_NEWS; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | protected function assignResult($news) |
||
| 72 | { |
||
| 73 | $this->category = $news['category']; |
||
| 74 | $this->subject = $news['subject']; |
||
| 75 | $this->content = $news['content']; |
||
| 76 | 2 | $this->created = TimeDate::fromMysql($news['created']); |
|
| 77 | $this->updated = TimeDate::fromMysql($news['updated']); |
||
| 78 | 2 | $this->author = $news['author']; |
|
| 79 | 2 | $this->editor = $news['editor']; |
|
| 80 | 2 | $this->is_draft = $news['is_draft']; |
|
| 81 | 2 | $this->is_deleted = $news['is_deleted']; |
|
| 82 | 2 | } |
|
| 83 | 2 | ||
| 84 | 2 | /** |
|
| 85 | 2 | * Get the author of the news article |
|
| 86 | 2 | * @return Player The author of the post |
|
| 87 | */ |
||
| 88 | public function getAuthor() |
||
| 92 | 1 | ||
| 93 | /** |
||
| 94 | 1 | * Get the user ID of the author who wrote this article |
|
| 95 | * @return int The author ID |
||
| 96 | */ |
||
| 97 | public function getAuthorID() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the category of the news article |
||
| 104 | * @return NewsCategory The category of the post |
||
| 105 | */ |
||
| 106 | public function getCategory() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the database ID from the category the article belongs into |
||
| 113 | * @return int The category ID |
||
| 114 | */ |
||
| 115 | public function getCategoryID() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the content of the article |
||
| 122 | * @return string The raw content of the article |
||
| 123 | */ |
||
| 124 | public function getContent() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the time when the article was submitted |
||
| 131 | * |
||
| 132 | * @return TimeDate The article's creation time |
||
| 133 | */ |
||
| 134 | public function getCreated() |
||
| 138 | 1 | ||
| 139 | /** |
||
| 140 | 1 | * Get the time when the article was last updated |
|
| 141 | * |
||
| 142 | * @return TimeDate The article's last update time |
||
| 143 | */ |
||
| 144 | public function getLastEdit() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the last editor of the post |
||
| 151 | * @return Player A Player object of the last editor |
||
| 152 | */ |
||
| 153 | public function getLastEditor() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the ID of the person who last edited the article |
||
| 160 | * @return int The ID of the last editor |
||
| 161 | */ |
||
| 162 | public function getLastEditorID() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the subject of the news article |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getSubject() |
||
| 175 | 1 | ||
| 176 | /** |
||
| 177 | 1 | * {@inheritdoc} |
|
| 178 | */ |
||
| 179 | public function getName() |
||
| 183 | 1 | ||
| 184 | /** |
||
| 185 | 1 | * Get whether or not this news article is |
|
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function isDraft() |
||
| 193 | 1 | ||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public static function getParamName() |
||
| 201 | 1 | ||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public static function getRouteName($action = 'show') |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Update the "draft" status of a post |
||
| 212 | * |
||
| 213 | * @param bool $draft |
||
| 214 | * |
||
| 215 | * @return static |
||
| 216 | */ |
||
| 217 | public function setDraft($draft) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Update the content of a post |
||
| 224 | * |
||
| 225 | * @param string $content The new content of the post |
||
| 226 | * |
||
| 227 | * @return self |
||
| 228 | */ |
||
| 229 | public function updateContent($content) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Update the last edit timestamp |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | public function updateEditTimestamp() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Update the editor of the post |
||
| 245 | * |
||
| 246 | * @param int $editorID The ID of the editor |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | public function updateLastEditor($editorID) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Update the category of the post |
||
| 256 | * |
||
| 257 | * @param int $categoryID The ID of the category |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | public function updateCategory($categoryID) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Update the status of a post |
||
| 267 | * |
||
| 268 | * @param string $status The new status of a post |
||
| 269 | * @return self |
||
| 270 | */ |
||
| 271 | public function updateStatus($status = 'published') |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Update the subject of a post |
||
| 280 | * |
||
| 281 | * @param string $subject The new subject of a post |
||
| 282 | * @return self |
||
| 283 | */ |
||
| 284 | public function updateSubject($subject) |
||
| 288 | 2 | ||
| 289 | /** |
||
| 290 | 2 | * Add a new news article |
|
| 291 | 2 | * |
|
| 292 | 2 | * @param string $subject The subject of the article |
|
| 293 | 2 | * @param string $content The content of the article |
|
| 294 | 2 | * @param int $authorID The ID of the author |
|
| 295 | 2 | * @param int $categoryId The ID of the category this article will be published under |
|
| 296 | 2 | * @param bool $is_draft Whether or not the added news item should be stored as a draft |
|
| 297 | 2 | * |
|
| 298 | * @return News An object representing the article that was just created or false if the article was not created |
||
| 299 | */ |
||
| 300 | public static function addNews($subject, $content, $authorID, $categoryId = 1, $is_draft = false) |
||
| 311 | 1 | ||
| 312 | /** |
||
| 313 | 1 | * Get all the news entries in the database that aren't disabled or deleted |
|
| 314 | 1 | * |
|
| 315 | * @param int $start The offset used when fetching matches, i.e. the starting point |
||
| 316 | * @param int $limit The amount of matches to be retrieved |
||
| 317 | 1 | * @param bool $getDrafts Whether or not to fetch drafts |
|
| 318 | 1 | * |
|
| 319 | 1 | * @throws Exception When a database is not configured in BZiON |
|
| 320 | 1 | * |
|
| 321 | * @return News[] An array of news objects |
||
| 322 | */ |
||
| 323 | View Code Duplication | public static function getNews($start = 0, $limit = 5, $getDrafts = false) |
|
| 324 | { |
||
| 325 | $qb = self::getQueryBuilder() |
||
| 326 | ->limit($limit) |
||
| 327 | ->offset($start) |
||
| 328 | ->orderBy('created', 'DESC') |
||
| 329 | ->active() |
||
| 330 | ; |
||
| 331 | |||
| 332 | if ($getDrafts) { |
||
| 333 | $qb->orWhere('is_draft', '=', true); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $qb->getModels(true); |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get a query builder for news |
||
| 341 | * |
||
| 342 | * @throws Exception |
||
| 343 | * |
||
| 344 | * @return QueryBuilderFlex |
||
| 345 | */ |
||
| 346 | public static function getQueryBuilder() |
||
| 352 | } |
||
| 353 |
If you suppress an error, we recommend checking for the error condition explicitly: