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 |
||
| 4 | class News extends Base { |
||
| 5 | protected $table = 'news'; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * We allow changing the database for shared accounts across pools |
||
| 9 | * Load the config on construct so we can assign the DB name |
||
| 10 | * @param config array MPOS configuration |
||
| 11 | * @return none |
||
|
|
|||
| 12 | **/ |
||
| 13 | public function __construct($config) { |
||
| 14 | $this->setConfig($config); |
||
| 15 | $this->table = $this->config['db']['shared']['news'] . '.' . $this->table; |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get activation status of post |
||
| 20 | * @param id int News ID |
||
| 21 | * @return bool true or false |
||
| 22 | **/ |
||
| 23 | public function getActive($id) { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Switch activation status |
||
| 30 | * @param id int News ID |
||
| 31 | * @return bool true or false |
||
| 32 | **/ |
||
| 33 | public function toggleActive($id) { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get all active news |
||
| 41 | **/ |
||
| 42 | View Code Duplication | public function getAllActive() { |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get all news |
||
| 52 | **/ |
||
| 53 | View Code Duplication | public function getAll() { |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Get a specific news entry |
||
| 63 | **/ |
||
| 64 | public function getEntry($id) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Update a news entry |
||
| 74 | **/ |
||
| 75 | public function updateNews($id, $header, $content, $active=0) { |
||
| 82 | |||
| 83 | View Code Duplication | public function deleteNews($id) { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Add a new mews entry to the table |
||
| 94 | * @param type string Type of the notification |
||
| 95 | * @return bool |
||
| 96 | **/ |
||
| 97 | public function addNews($account_id, $aData, $active=false) { |
||
| 108 | } |
||
| 109 | |||
| 115 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.