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 |
||
| 7 | class Base extends CI_Model |
||
| 8 | { |
||
|
|
|||
| 9 | |||
| 10 | /** |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | public $comments_product_id = []; |
||
| 14 | |||
| 15 | public function __construct() { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param int $item_id |
||
| 21 | * @param int $status |
||
| 22 | * @param string $module |
||
| 23 | * @param int $limit |
||
| 24 | * @param string $order_by |
||
| 25 | * @return bool|array |
||
| 26 | */ |
||
| 27 | public function get($item_id, $status = 0, $module, $limit = 999999, $order_by) { |
||
| 28 | if ($this->db->table_exists('comments')) { |
||
| 29 | |||
| 30 | $this->db->where('item_id', $item_id); |
||
| 31 | $this->db->where('status', $status); |
||
| 32 | $this->db->where('module', $module); |
||
| 33 | |||
| 34 | $order_by = $order_by ?: 'date.desc'; |
||
| 35 | $order_column = array_shift(explode('.', $order_by)); |
||
| 36 | $order_way = array_pop(explode('.', $order_by)); |
||
| 37 | $this->db->order_by($order_column, $order_way); |
||
| 38 | $query = $this->db->get('comments', $limit); |
||
| 39 | |||
| 40 | if ($query->num_rows() > 0) { |
||
| 41 | return $query->result_array(); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return FALSE; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function get_one($id) { |
||
| 52 | |||
| 53 | public function add($data) { |
||
| 58 | |||
| 59 | public function all($row_count, $offset) { |
||
| 74 | |||
| 75 | public function get_item_comments_status($item_id) { |
||
| 92 | |||
| 93 | public function update($id, $data = []) { |
||
| 99 | |||
| 100 | View Code Duplication | public function setYes($id) { |
|
| 108 | |||
| 109 | View Code Duplication | public function setNo($id) { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param int $id |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function delete($id) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param int $id |
||
| 150 | */ |
||
| 151 | public function changeVotesAfterDelete($id) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 162 | */ |
||
| 163 | public function setVoutesAfterDelete() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param int $status |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function count_by_status($status = 0) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function get_settings() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $data |
||
| 212 | */ |
||
| 213 | public function save_settings($data) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param array $ids |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | public function get_many($ids) { |
||
| 227 | |||
| 228 | } |
||
| 229 | |||
| 230 | /* End of base.php */ |