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 |
||
| 12 | class UsersController extends ControllerBase implements FileImport |
||
|
|
|||
| 13 | { |
||
| 14 | |||
| 15 | public $params = []; |
||
| 16 | |||
| 17 | public function __construct($controller) { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Show template for users online with data |
||
| 29 | */ |
||
| 30 | public function online() { |
||
| 31 | $this->controller->load->model('attendance_model'); |
||
| 32 | $onlineUsers = $this->controller->attendance_model->getOnline(); |
||
| 33 | |||
| 34 | $this->renderAdmin( |
||
| 35 | 'online', |
||
| 36 | ['data' => $onlineUsers] |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Show template for users online with data |
||
| 42 | */ |
||
| 43 | public function history() { |
||
| 44 | $this->controller->load->model('attendance_model'); |
||
| 45 | $data = $this->controller->attendance_model->getUserHistory(CI::$APP->input->post('userId')); |
||
| 46 | $this->controller->assetManager->setData(['data' => $data]); |
||
| 47 | $this->controller->assetManager->render('admin/users/history'); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Render template for users info with data |
||
| 52 | */ |
||
| 53 | public function info() { |
||
| 54 | $this->controller->load->model('users_model'); |
||
| 55 | $this->controller->users_model->setParams($this->params); |
||
| 56 | $data = $this->controller->users_model->getInfo(); |
||
| 57 | $this->renderAdmin( |
||
| 58 | 'info', |
||
| 59 | ['data' => $data] |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Render template for users attendance with data |
||
| 65 | */ |
||
| 66 | public function attendance() { |
||
| 67 | // getting view type |
||
| 68 | View Code Duplication | if (CI::$APP->input->get('view_type')) { |
|
| 69 | $vt = CI::$APP->input->get('view_type'); |
||
| 70 | $viewType = $vt == 'table' || $vt == 'chart' ? $vt : 'chart'; |
||
| 71 | } else { |
||
| 72 | $viewType = 'table'; |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->controller->load->model('attendance_model'); |
||
| 76 | |||
| 77 | $data = $this->controller->attendance_model->getCommonAttendance($this->params); |
||
| 78 | |||
| 79 | $this->renderAdmin( |
||
| 80 | 'attendance', |
||
| 81 | [ |
||
| 82 | 'data' => $data, |
||
| 83 | 'viewType' => $viewType, |
||
| 84 | ] |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Output chart data for users attendance |
||
| 90 | */ |
||
| 91 | public function getAttendanceData() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Render template for users registration |
||
| 136 | */ |
||
| 137 | public function registered() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Output chart data for users registration |
||
| 167 | */ |
||
| 168 | public function getRegisterData() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * |
||
| 198 | */ |
||
| 199 | public function robots_attendance() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Include file (or all recursively files in dir) |
||
| 220 | * The starting directory is the directory where the class is (witch using trait) |
||
| 221 | * @param string $filePath |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function import($filePath) { |
|
| 245 | |||
| 246 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.