| Conditions | 4 |
| Paths | 8 |
| Total Lines | 82 |
| Code Lines | 53 |
| Lines | 2 |
| Ratio | 2.44 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); |
||
| 9 | public function create($first_name, $last_name, $ern, $position, $faculty, $department, $room, $ext, $con_start, $con_end, $password, $site) { |
||
| 10 | |||
| 11 | $firstchar = substr($this->input->post('first_name'), 0, 1); |
||
| 12 | $builtUsername = strtolower($firstchar) . strtolower($this->input->post('last_name')); |
||
| 13 | //check user name is new |
||
| 14 | |||
| 15 | $username = $this->config->item('ldapshortdomain') . $builtUsername; |
||
| 16 | // specify the LDAP server to connect to |
||
| 17 | $conn = ldap_connect($this->config->item('ldapserver')) or die("Oh no can't create LDAP connection"); |
||
| 18 | // bind to the LDAP server specified above |
||
| 19 | View Code Duplication | if (!ldap_bind($conn, $this->config->item('ldapbindun'),"@".$this->config->item('ldapdomain'), $this->config->item('ldapbindpass'))) |
|
|
|
|||
| 20 | echo "Invalid credentials."; |
||
| 21 | // Search for user in directory |
||
| 22 | $cred = explode('\\', $username); |
||
| 23 | list($domain, $user) = $cred; |
||
| 24 | |||
| 25 | $result = ldap_search($conn, $this->config->item('ldapuserou'), "samaccountname=$user"); |
||
| 26 | // get entry data as array |
||
| 27 | $info = ldap_count_entries($conn, $result); |
||
| 28 | |||
| 29 | if ($info != 0) { |
||
| 30 | $builtUsername = $builtUsername . "2"; |
||
| 31 | } |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | $this->email->from('[email protected]', 'New Staff Account'); |
||
| 36 | $this->email->to($_SESSION['ldap']['email']); |
||
| 37 | $this->email->subject('New Staff Account'); |
||
| 38 | $this->email->message('A new staff account has been requested by yourself |
||
| 39 | |||
| 40 | Account Details: |
||
| 41 | Name: ' . $this->input->post('first_name') . ' ' . $this->input->post('last_name') . ' |
||
| 42 | Position: ' . $this->input->post('position') . ' |
||
| 43 | ERN: ' . $this->input->post('ern') . ' |
||
| 44 | Faculty: ' . $this->input->post('faculty') . ' |
||
| 45 | Department: ' . $this->input->post('department') . ' |
||
| 46 | Room: ' . $this->input->post('room') . ' |
||
| 47 | Phone: ' . $this->input->post('ext') . ' |
||
| 48 | Site: ' . $this->input->post('site') . ' |
||
| 49 | |||
| 50 | Username: ' . $builtUsername . ' |
||
| 51 | Password: ' . $this->input->post('password') . ' |
||
| 52 | |||
| 53 | |||
| 54 | If this account was made by mistake please contact Computing Support. |
||
| 55 | If you notice a mistake please edit it below. |
||
| 56 | https://intranet.cant-col.ac.uk/dashboard/computing-support/new-account/history'); |
||
| 57 | $this->email->send(); |
||
| 58 | |||
| 59 | |||
| 60 | $this->db->where('cat_id', $this->input->post('faculty')); |
||
| 61 | $faculty_query = $this->db->get('faculty')->result_array(); |
||
| 62 | |||
| 63 | $this->db->where('cat_key', $this->input->post('department')); |
||
| 64 | $department_query = $this->db->get('facultysub')->result_array(); |
||
| 65 | |||
| 66 | $this->db->where('cat_key', $this->input->post('department')); |
||
| 67 | $costarea_query = $this->db->get('facultysub')->result_array(); |
||
| 68 | |||
| 69 | $data = array( |
||
| 70 | 'logged' => $_SESSION['username'], |
||
| 71 | 'created' => date("Y-m-d H:i:s", time()), |
||
| 72 | 'first_name' => $this->input->post('first_name'), |
||
| 73 | 'last_name' => $this->input->post('last_name'), |
||
| 74 | 'username' => $builtUsername, |
||
| 75 | 'ern' => $this->input->post('ern'), |
||
| 76 | 'position' => $this->input->post('position'), |
||
| 77 | 'faculty' => $faculty_query[0]['faculty'], |
||
| 78 | 'department' => $department_query[0]['subfaculty'], |
||
| 79 | 'room' => $this->input->post('room'), |
||
| 80 | 'ext' => $this->input->post('ext'), |
||
| 81 | 'con_start' => $this->input->post('con_start'), |
||
| 82 | 'con_end' => $this->input->post('con_end'), |
||
| 83 | 'password' => $this->input->post('password'), |
||
| 84 | 'site' => $this->input->post('site'), |
||
| 85 | 'costarea' => $costarea_query[0]['costarea'], |
||
| 86 | 'complete' => NULL |
||
| 87 | ); |
||
| 88 | |||
| 89 | return $this->db->insert('new_account', $data); |
||
| 90 | } |
||
| 91 | |||
| 143 |
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.