New_account_model::create()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 82
Code Lines 53

Duplication

Lines 2
Ratio 2.44 %

Importance

Changes 0
Metric Value
cc 4
eloc 53
nc 8
nop 12
dl 2
loc 82
rs 8.5076
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

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:

Many Parameters

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');
2
3
class New_account_model extends CI_Model {
4
5
    public function __construct() {
6
        $this->load->model('computing-support/New_account_model', 'new_account_model');
7
    }
8
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')))
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...
20
            echo "Invalid credentials.";
21
        // Search for user in directory
22
        $cred = explode('\\', $username);
23
        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...
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
92 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...
93
        $this->db->from('faculty');
94
        $this->db->order_by('faculty');
95
        $result = $this->db->get();
96
        $return = array();
97
        if ($result->num_rows() > 0) {
98
            foreach ($result->result_array() as $row) {
99
                $return[$row['cat_id']] = $row['faculty'];
100
            }
101
        }
102
103
        return $return;
104
    }
105
106 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...
107
        if (!isset($_GET['cat'])) {
108
            $_GET['cat'] = '1';
109
        }
110
        $result = $this->db->get_where('facultysub', array('cat_id' => $_GET['cat']));
111
        $return = array();
112
        if ($result->num_rows() > 0) {
113
            foreach ($result->result_array() as $row) {
114
                $return[$row['cat_key']] = $row['subfaculty'];
115
            }
116
        }
117
118
        return $return;
119
    }
120
121
    public function complete_account() {
122
123
        $data = array(
124
            'complete' => date("Y-m-d H:i:s", time())
125
        );
126
        $this->db->where('complete', null, true);
127
        return $this->db->update('new_account', $data);
128
    }
129
130
    public function disable($full_name, $last) {
131
        
132
        $data = array(
133
            'logged' => $_SESSION['username'],
134
            'requested' => date("Y-m-d H:i:s", time()),
135
            'full_name' => $full_name,
136
            'last' => $last,
137
        );
138
139
        return $this->db->insert('disable_account', $data);
140
    }
141
142
}
143