Conditions | 1 |
Paths | 8 |
Total Lines | 76 |
Code Lines | 46 |
Lines | 8 |
Ratio | 10.53 % |
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'); |
||
10 | public function create($logged, $faculty, $department, $requester, $first_name, $last_name, $email, $username, $expiry) { |
||
11 | |||
12 | $firstchar = substr($this->input->post('first_name'), 0, 1); |
||
13 | $builtUsername = strtolower($firstchar) . strtolower($this->input->post('last_name')); |
||
14 | |||
15 | //check user name is new |
||
16 | $username = $this->config->item('ldapshortdomain') . $builtUsername; |
||
17 | // specify the LDAP server to connect to |
||
18 | $conn = ldap_connect($this->config->item('ldapserver')) or die("Oh no can't create LDAP connection"); |
||
19 | // bind to the LDAP server specified above |
||
20 | View Code Duplication | if (!ldap_bind($conn, $this->config->item('ldapbindun'),"@".$this->config->item('ldapdomain'), $this->config->item('ldapbindpass'))) |
|
|
|||
21 | echo "Invalid credentials."; |
||
22 | // Search for user in directory |
||
23 | $cred = explode('\\', $username); |
||
24 | list($domain, $user) = $cred; |
||
25 | |||
26 | $result = ldap_search($conn, $this->config->item('ldapuserou'), "samaccountname=$user"); |
||
27 | // get entry data as array |
||
28 | $info = ldap_count_entries($conn, $result); |
||
29 | |||
30 | if ($info != 0) { |
||
31 | $builtUsername = $builtUsername . "2"; |
||
32 | } |
||
33 | |||
34 | function randomPassword() { |
||
35 | $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; |
||
36 | $pass = array(); //remember to declare $pass as an array |
||
37 | $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache |
||
38 | for ($i = 0; $i < 6; $i++) { |
||
39 | $n = rand(0, $alphaLength); |
||
40 | $pass[] = $alphabet[$n]; |
||
41 | } |
||
42 | return implode($pass); //turn the array into a string |
||
43 | } |
||
44 | $generatedpassword = randomPassword(); |
||
45 | |||
46 | /** Returns UNIX timestamp and Active Directory timestamp for given date and time */ |
||
47 | //Format dd-mm-yyyy |
||
48 | $dateFromForm = $this->input->post('expiry'); |
||
49 | |||
50 | //Format hh:mm:ss |
||
51 | $timeFromForm = "00:00:00"; |
||
52 | $dateWithTime = $dateFromForm . " " . $timeFromForm; |
||
53 | |||
54 | View Code Duplication | function convertDateToUnix($input) { |
|
55 | $format = 'd-m-Y H:i:s'; |
||
56 | $date = DateTime::createFromFormat($format, $input); |
||
57 | $UNIXTimestamp = $date->getTimestamp(); |
||
58 | return $UNIXTimestamp; |
||
59 | } |
||
60 | |||
61 | function convertUnixtoWin($input) { |
||
62 | return ($input + 11644473600) * 10000000; |
||
63 | } |
||
64 | |||
65 | //$UNIX = convertDateToUnix($dateWithTime); |
||
66 | //$WIN = convertUnixtoWin($UNIX); |
||
67 | |||
68 | $data = array( |
||
69 | 'logged' => $_SESSION['ldap']['full_name'], |
||
70 | 'logged_at' => date("Y-m-d H:i:s", time()), |
||
71 | 'requester' => $this->input->post('requester'), |
||
72 | 'faculty' => $this->input->post('faculty'), |
||
73 | 'department' => $this->input->post('department'), |
||
74 | 'first_name' => $this->input->post('first_name'), |
||
75 | 'last_name' => $this->input->post('last_name'), |
||
76 | 'email' => $this->input->post('email'), |
||
77 | 'username' => $this->input->post('username'), |
||
78 | 'expiry' => $this->input->post('expiry'), |
||
79 | 'password' => $generatedpassword, |
||
80 | //'wintime' => $WIN, |
||
81 | //'unixtime' => $UNIX, |
||
82 | 'status' => 0); |
||
83 | |||
84 | return $this->db->insert('temporary_account', $data); |
||
85 | } |
||
86 | |||
196 | } |
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.