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 defined('BASEPATH') OR exit('No direct script access allowed'); |
||
3 | class Disposals extends My_Force_Login { |
||
4 | |||
5 | public function __construct() { |
||
6 | parent::__construct(); |
||
7 | $this->load->helper('download'); |
||
8 | $this->load->library('grocery_CRUD'); |
||
9 | $this->load->model('computing-support/Disposals_model', 'disposals_model'); |
||
10 | |||
11 | } |
||
12 | |||
13 | public function index() { |
||
14 | |||
15 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
16 | |||
17 | $this->load->helper('form'); |
||
18 | $this->load->library('form_validation'); |
||
19 | |||
20 | // validation rules |
||
21 | $this->form_validation->set_rules('make', 'Device Make', 'trim|required|min_length[3]'); |
||
22 | $this->form_validation->set_rules('model', 'Device Model', 'trim|required|min_length[3]'); |
||
23 | $this->form_validation->set_rules('sn', 'Device Serial Number', 'trim|required|min_length[3]'); |
||
24 | |||
25 | if ($this->form_validation->run() === false) { |
||
26 | |||
27 | $this->load->view('templates/header'); |
||
28 | $this->load->view('computing-support/disposals/view'); |
||
29 | $this->load->view('templates/footer'); |
||
30 | |||
31 | } else { |
||
32 | |||
33 | $logged = $this->input->post('logged'); |
||
34 | $make = $this->input->post('make'); |
||
35 | $model = $this->input->post('model'); |
||
36 | $sn = $this->input->post('sn'); |
||
37 | |||
38 | if ($this->disposals_model->dispose($logged, $make, $model, $sn)) { |
||
39 | |||
40 | $function = 'disposals_submitted'; |
||
41 | $this->user_model->function_log($function); |
||
42 | |||
43 | // user created |
||
44 | $this->load->view('templates/header'); |
||
45 | $this->load->view('computing-support/new-account/created'); |
||
46 | $this->load->view('templates/footer'); |
||
47 | |||
48 | } else { |
||
49 | |||
50 | $function = 'disposals_error'; |
||
51 | $this->user_model->function_log($function); |
||
52 | |||
53 | $data = new stdClass(); |
||
54 | $data->error = 'There was a problem submitting this disposal record. Please try again.'; |
||
55 | |||
56 | // failed to create user |
||
57 | $this->load->view('templates/header'); |
||
58 | $this->load->view('computing-support/disposals/view', $data); |
||
59 | $this->load->view('templates/footer'); |
||
60 | |||
61 | } |
||
62 | } |
||
63 | } else { |
||
64 | redirect('permissions'); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | View Code Duplication | public function history() { |
|
|
|||
69 | |||
70 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
71 | |||
72 | $crud = new grocery_CRUD(); |
||
73 | $crud->set_table('disposals'); |
||
74 | $crud->set_subject('disposals', 'Disposals'); |
||
75 | $crud->display_as('sn','Serial Number'); |
||
76 | $crud->unset_columns('id'); |
||
77 | $crud->unset_add(); |
||
78 | $crud->unset_read(); |
||
79 | $crud->unset_delete(); |
||
80 | $output = $crud->render(); |
||
81 | |||
82 | $this->load->view('templates/header.php'); |
||
83 | $this->load->view('computing-support/disposals/history', $output); |
||
84 | $this->load->view('templates/footer.php'); |
||
85 | $this->load->view('templates/table_assets.php', $output); |
||
86 | |||
87 | } else { |
||
88 | redirect('permissions'); |
||
89 | } |
||
90 | |||
91 | } |
||
92 | |||
93 | View Code Duplication | public function current() { |
|
94 | |||
95 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
96 | |||
97 | $crud = new grocery_CRUD(); |
||
98 | $crud->set_table('disposals_current'); |
||
99 | $crud->set_subject('disposals', 'Disposals'); |
||
100 | $crud->set_primary_key('id'); |
||
101 | $crud->display_as('sn','Serial Number'); |
||
102 | $crud->unset_columns('id'); |
||
103 | $crud->unset_add(); |
||
104 | $crud->unset_read(); |
||
105 | $crud->unset_delete(); |
||
106 | $output = $crud->render(); |
||
107 | |||
108 | $this->load->view('templates/header.php'); |
||
109 | $this->load->view('computing-support/disposals/current', $output); |
||
110 | $this->load->view('templates/footer.php'); |
||
111 | $this->load->view('templates/table_assets.php', $output); |
||
112 | |||
113 | } else { |
||
114 | redirect('permissions'); |
||
115 | } |
||
116 | |||
117 | } |
||
118 | |||
119 | View Code Duplication | public function complete() { |
|
120 | |||
121 | if (in_array('CN=Dashboard_Admin,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
122 | |||
123 | $function = 'complete_disposals'; |
||
124 | $this->user_model->function_log($function); |
||
125 | |||
126 | $this->disposals_model->complete_account(); |
||
127 | |||
128 | $this->load->view('templates/header'); |
||
129 | $this->load->view('computing-support/disposals/complete'); |
||
130 | $this->load->view('templates/footer'); |
||
131 | |||
132 | } else { |
||
133 | redirect('permissions'); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | // If changed are made, duplicate in New_staff_export.php controller. |
||
138 | |||
139 | View Code Duplication | function export_current() { |
|
140 | |||
141 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
142 | |||
143 | $function = 'export current_disposals'; |
||
144 | $this->user_model->function_log($function); |
||
145 | |||
146 | $this->load->dbutil(); |
||
147 | //MySQL View - only export incomplete |
||
148 | $query = $this->db->query("SELECT * FROM disposals WHERE completed IS NULL"); |
||
149 | $delimiter = ","; |
||
150 | $newline = "\n"; |
||
151 | $output = $this->dbutil->csv_from_result($query, $delimiter, $newline); |
||
152 | |||
153 | function clean_export($string) { |
||
157 | |||
158 | $output = clean_export($output); |
||
159 | force_download("current_disposals.csv", $output); |
||
160 | |||
161 | } else { |
||
162 | redirect('permissions'); |
||
165 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.