Temporary_account_model   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 194
Duplicated Lines 29.9 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 58
loc 194
rs 10
c 0
b 0
f 0
wmc 20
lcom 0
cbo 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A hp ➔ randomPassword() 0 10 2
A __construct() 0 4 1
B create() 8 76 1
A get_next_temp() 0 6 2
A get_faculty() 13 13 3
A get_department() 14 14 4
A get_pending() 0 5 1
A get_all() 0 4 1
A check_status() 0 5 1
A match_id_user() 0 5 1
A cancel() 0 9 1
A reject() 11 11 1
A approve() 12 12 1
A created_account() 0 9 1
A get_account() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

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');
2
3
class Temporary_account_model extends CI_Model {
4
5
    public function __construct() {
6
        parent::__construct();
7
        $this->load->helper('date');
8
    }
9
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')))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
21
            echo "Invalid credentials.";
22
        // Search for user in directory
23
        $cred = explode('\\', $username);
24
        list($domain, $user) = $cred;
0 ignored issues
show
Unused Code introduced by
The assignment to $domain is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
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";
0 ignored issues
show
Unused Code introduced by
$builtUsername is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
32
        }
33
34
        function randomPassword() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$dateWithTime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
53
54 View Code Duplication
        function convertDateToUnix($input) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

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.

Loading history...
Best Practice introduced by
The function convertDateToUnix() has been defined more than once; this definition is ignored, only the first definition in dashboard/application/co...t/Temporary_account.php (L190-195) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
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) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Best Practice introduced by
The function convertUnixtoWin() has been defined more than once; this definition is ignored, only the first definition in dashboard/application/co...t/Temporary_account.php (L197-199) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
62
            return ($input + 11644473600) * 10000000;
63
        }
64
65
        //$UNIX = convertDateToUnix($dateWithTime);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
        //$WIN = convertUnixtoWin($UNIX);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
            //'unixtime' => $UNIX,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
            'status' => 0);
83
84
        return $this->db->insert('temporary_account', $data);
85
    }
86
87
    public function get_next_temp() {
88
        $query = $this->db->query("SELECT id FROM temporary_account ORDER BY id DESC LIMIT 1;");
89
        foreach ($query->result() as $row) {
90
            return $row->id + 1;
91
        }
92
    }
93
94 View Code Duplication
    function get_faculty() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
95
        $this->db->from('faculty');
96
        $this->db->order_by('faculty');
97
        $result = $this->db->get();
98
        $return = array();
99
        if ($result->num_rows() > 0) {
100
            foreach ($result->result_array() as $row) {
101
                $return[$row['cat_id']] = $row['faculty'];
102
            }
103
        }
104
105
        return $return;
106
    }
107
108 View Code Duplication
    function get_department() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
109
        if (!isset($_GET['cat'])) {
110
            $_GET['cat'] = '1';
111
        }
112
        $result = $this->db->get_where('facultysub', array('cat_id' => $_GET['cat']));
113
        $return = array();
114
        if ($result->num_rows() > 0) {
115
            foreach ($result->result_array() as $row) {
116
                $return[$row['cat_key']] = $row['subfaculty'];
117
            }
118
        }
119
120
        return $return;
121
    }
122
    
123
    public function get_pending() {
124
        $this->db->where('status', '0');
125
        $query = $this->db->get('temporary_account');
126
        return $query->result_array();
127
    }
128
    
129
    public function get_all() {
130
        $query = $this->db->get('temporary_account');
131
        return $query->result_array();
132
    }
133
    
134
    public function check_status() {
135
        $this->db->where('logged', $_SESSION['ldap']['full_name'])->or_where('requester', $_SESSION['ldap']['full_name']);
136
        $query = $this->db->get('temporary_account');
137
        return $query->result_array();
138
    }
139
    
140
    public function match_id_user($id) {
141
        $this->db->where('id', $id);
142
        $query = $this->db->get('temporary_account');
143
        return $query->result_array();
144
    }
145
    
146
    public function cancel($id) {
147
            
148
        $this->db->where('id', $id);
149
	$this->db->from('temporary_account');
150
        $data = array(
151
            'status' => '3',
152
        );
153
        return $this->db->update('temporary_account', $data);
154
    }
155
    
156 View Code Duplication
    public function reject($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
157
            
158
        $this->db->where('id', $id);
159
	$this->db->from('temporary_account');
160
        $data = array(
161
            'status' => '2',
162
            'status_at' => date("Y-m-d H:i:s", time()),
163
            'status_by' => $_SESSION['ldap']['full_name'],
164
        );
165
        return $this->db->update('temporary_account', $data);
166
    }
167
    
168 View Code Duplication
    public function approve($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
169
            
170
        $this->db->where('id', $id);
171
	$this->db->from('temporary_account');
172
        $data = array(
173
            'status' => '1',
174
            'status_at' => date("Y-m-d H:i:s", time()),
175
            'status_by' => $_SESSION['ldap']['full_name'],
176
        );
177
        
178
        return $this->db->update('temporary_account', $data);
179
    }
180
    
181
    public function created_account($id) {
182
            
183
        $this->db->where('id', $id);
184
	$this->db->from('temporary_account');
185
        $data = array(
186
            'created' => '1',
187
        );
188
        return $this->db->update('temporary_account', $data);
189
    }
190
    
191
    public function get_account($id) {
192
        $this->db->where('id', $id);
193
        $query = $this->db->get('temporary_account');
194
        return $query->result_array();
195
    }
196
}