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 |
||
5 | class Temporary_account extends My_Force_Login { |
||
6 | |||
7 | public function __construct() { |
||
11 | |||
12 | public function index() { |
||
13 | |||
14 | $this->load->helper('form'); |
||
15 | $this->load->library('form_validation'); |
||
16 | |||
17 | $data = array(); |
||
18 | $data['faculty'] = $this->temporary_account_model->get_faculty(); |
||
19 | $data['department'] = $this->temporary_account_model->get_department(); |
||
20 | $data['tempid'] = "tempuser" . $this->temporary_account_model->get_next_temp(); |
||
21 | |||
22 | //validation |
||
23 | $this->form_validation->set_rules('faculty', 'Faculty', 'trim|required'); |
||
24 | $this->form_validation->set_rules('requester', 'Staff requester', 'trim|required|min_length[3]'); |
||
25 | $this->form_validation->set_rules('first_name', 'Users first name', 'trim|required|min_length[3]'); |
||
26 | $this->form_validation->set_rules('last_name', 'Users last name', 'trim|required|min_length[3]'); |
||
27 | |||
28 | if ($this->form_validation->run() === FALSE) { |
||
29 | |||
30 | $this->load->view('templates/header'); |
||
31 | $this->load->view('computing-support/temporary-account/view', $data); |
||
32 | $this->load->view('templates/footer'); |
||
33 | } else { |
||
34 | |||
35 | $logged = $this->input->post('logged'); |
||
36 | $faculty = $this->input->post('faculty'); |
||
37 | $department = $this->input->post('department'); |
||
38 | $requester = $this->input->post('requester'); |
||
39 | $first_name = $this->input->post('first_name'); |
||
40 | $last_name = $this->input->post('last_name'); |
||
41 | $email = $this->input->post('email'); |
||
42 | $username = $this->input->post('username'); |
||
43 | $expiry = $this->input->post('expiry'); |
||
44 | |||
45 | if ($this->temporary_account_model->create($logged, $faculty, $department, $requester, $first_name, $last_name, $email, $username, $expiry)) { |
||
46 | |||
47 | $this->email->from('[email protected]', 'Temporary Logon Account Request'); |
||
48 | $this->email->to('[email protected]'); |
||
49 | $this->email->subject('Temporary Logon Account Request'); |
||
50 | $this->email->message('A temporary network account has been requested by ' . $_SESSION['ldap']['full_name'] |
||
51 | . ' for ' . $this->input->post('requester') |
||
52 | . '' |
||
53 | . 'the user will be: ' . $this->input->post('first_name') . ' ' . $this->input->post('last_name') |
||
54 | . '' |
||
55 | . 'Approve https://intranet.cant-col.ac.uk/dashboard/computing-support/temporary-account/approve'); |
||
56 | $this->email->send(); |
||
57 | |||
58 | $this->load->view('templates/header'); |
||
59 | $this->load->view('computing-support/temporary-account/complete'); |
||
60 | $this->load->view('templates/footer'); |
||
61 | } else { |
||
62 | |||
63 | $data = array(); |
||
|
|||
64 | $data = new stdClass(); |
||
65 | $data->error = 'There was a problem requesting this account. Please try again.'; |
||
66 | |||
67 | // failed to create user |
||
68 | $this->load->view('templates/header'); |
||
69 | $this->load->view('computing-support/temporary-account/view', $data); |
||
70 | $this->load->view('templates/footer'); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | View Code Duplication | public function history() { |
|
76 | |||
77 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
78 | |||
79 | $data = array(); |
||
80 | $data['temporary_account'] = $this->temporary_account_model->get_all(); |
||
81 | |||
82 | $this->load->view('templates/header'); |
||
83 | $this->load->view('computing-support/temporary-account/history', $data); |
||
84 | $this->load->view('templates/footer'); |
||
85 | } else { |
||
86 | redirect('permissions'); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | public function check() { |
||
91 | |||
92 | $data = array(); |
||
93 | $data['temporary_account'] = $this->temporary_account_model->check_status(); |
||
94 | |||
95 | $this->load->view('templates/header'); |
||
96 | $this->load->view('computing-support/temporary-account/check', $data); |
||
97 | $this->load->view('templates/footer'); |
||
98 | } |
||
99 | |||
100 | View Code Duplication | public function pending() { |
|
101 | |||
102 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
103 | |||
104 | $data = array(); |
||
105 | $data['temporary_account'] = $this->temporary_account_model->get_pending(); |
||
106 | |||
107 | $this->load->view('templates/header'); |
||
108 | $this->load->view('computing-support/temporary-account/pending', $data); |
||
109 | $this->load->view('templates/footer'); |
||
110 | } else { |
||
111 | redirect('permissions'); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | public function cancel() { |
||
116 | |||
117 | $id = $_GET['id']; |
||
118 | $check_user = $this->temporary_account_model->match_id_user($id); |
||
119 | if ($check_user[0]['logged'] == $_SESSION['ldap']['full_name'] || $check_user[0]['requested'] == $_SESSION['ldap']['full_name']) { |
||
120 | |||
121 | if (isset($_GET['id'])) { |
||
122 | |||
123 | $id = $_GET['id']; |
||
124 | $this->temporary_account_model->cancel($id); |
||
125 | |||
126 | $function = 'temp_account_CANCEL_' . $id; |
||
127 | $this->user_model->function_log($function); |
||
128 | |||
129 | redirect($_SERVER['HTTP_REFERER']); |
||
130 | } |
||
131 | redirect($_SERVER['HTTP_REFERER']); |
||
132 | } else { |
||
133 | redirect($_SERVER['HTTP_REFERER']); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | View Code Duplication | public function reject() { |
|
138 | |||
139 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
140 | |||
141 | if (isset($_GET['id'])) { |
||
142 | |||
143 | $id = $_GET['id']; |
||
144 | $this->temporary_account_model->reject($id); |
||
145 | |||
146 | $function = 'temp_account_REJECT_' . $id; |
||
147 | $this->user_model->function_log($function); |
||
148 | |||
149 | redirect($_SERVER['HTTP_REFERER']); |
||
150 | } |
||
151 | |||
152 | redirect($_SERVER['HTTP_REFERER']); |
||
153 | } else { |
||
154 | redirect($_SERVER['HTTP_REFERER']); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | public function approve() { |
||
159 | |||
160 | if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) { |
||
161 | |||
162 | if (isset($_GET['id'])) { |
||
163 | |||
164 | $id = $_GET['id']; |
||
165 | //$this->temporary_account_model->approve($id); |
||
166 | |||
167 | $function = 'temp_account_APPROVED_' . $id; |
||
168 | $this->user_model->function_log($function); |
||
169 | |||
170 | $data = array(); |
||
171 | $data['AD'] = $this->temporary_account_model->get_account($id); |
||
172 | |||
173 | //AD account start |
||
174 | //AD Server |
||
175 | $AD_server = $this->config->item('ldapserver'); |
||
176 | $AD_Auth_User = $this->config->item('ldapshortdomain').$this->config->item('ldapadminun'); //Administrative user |
||
177 | $AD_Auth_PWD = $this->config->item('ldapadminpass'); //The password |
||
178 | //Create Active Directory timestamp |
||
179 | date_default_timezone_set($this->config->item('timezone')); |
||
180 | |||
181 | //Format dd-mm-yyyy |
||
182 | //Expiry is beginning of day (thats why +1 day |
||
183 | $dateFromForm = date('d-m-Y', strtotime($data['AD'][0]['expiry']. ' +1 day')); |
||
184 | |||
185 | //Format hh:mm:ss |
||
186 | $timeFromForm = "00:00:00"; |
||
187 | |||
188 | $dateWithTime = $dateFromForm . " " . $timeFromForm; |
||
189 | |||
190 | View Code Duplication | function convertDateToUnix($input) { |
|
196 | |||
197 | function convertUnixtoWin($input) { |
||
200 | |||
201 | //$UNIX = convertDateToUnix($dateWithTime); |
||
202 | //$WIN = convertUnixtoWin($UNIX); |
||
203 | |||
204 | //Create Unicode password |
||
205 | $passwordWithQuotes = '"' . $data['AD'][0]['password'] . '"'; |
||
206 | $ldaprecord = array(); |
||
253 | |||
254 | public function error() { |
||
265 | } |
||
266 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.