find()   F
last analyzed

Complexity

Conditions 18
Paths 2592

Size

Total Lines 110
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 69
nc 2592
nop 0
dl 0
loc 110
rs 2
c 0
b 0
f 0
ccs 0
cts 90
cp 0
crap 342

How to fix   Long Method    Complexity   

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:

1
<?php
2
class Intraface_modules_procurement_ProcurementGateway
3
{
4
    protected $dbquery;
5
    protected $error;
6
    protected $kernel;
7
8
    function __construct($kernel)
9
    {
10
        $this->kernel = $kernel;
11
        $this->error = new Intraface_Error;
12
    }
13
14
    function findById($id)
15
    {
16
        return new Procurement($this->kernel, $id);
17
    }
18
19
    function getDBQuery()
20
    {
21
        if (!is_object($this->dbquery)) {
22
            $this->dbquery = new Intraface_DBQuery($this->kernel, "procurement", "active = 1 AND intranet_id = ".$this->kernel->intranet->get("id"));
23
            $this->dbquery->useErrorObject($this->error);
24
        }
25
26
        return $this->dbquery;
27
    }
28
29
    function findByContactId($id)
30
    {
31
        $this->getDBQuery()->setFilter('contact_id', $id);
32
        $this->getDBQuery()->setCondition('newsletter_subscriber.contact_id = '.$this->getDBQuery()->getFilter('contact_id'));
33
34
        return $this->dbquery->getRecordset("*,
35
            DATE_FORMAT(invoice_date, '%d-%m-%Y') AS dk_invoice_date,
36
            DATE_FORMAT(delivery_date, '%d-%m-%Y') AS dk_delivery_date,
37
            DATE_FORMAT(payment_date, '%d-%m-%Y') AS dk_payment_date,
38
            DATE_FORMAT(paid_date, '%d-%m-%Y') AS dk_paid_date");
39
    }
40
41
    function setNewContactId($old_id, $new_id)
42
    {
43
        $db = MDB2::singleton();
44
        $db->query('UPDATE procurement SET contact_id = ' . $new_id . ' WHERE contact_id = ' . $old_id);
45
    }
46
47
    function find()
48
    {
49
        $list = array();
50
51
        if ($this->dbquery->checkFilter("contact_id")) {
52
            $this->dbquery->setCondition("contact_id = ".intval($this->dbquery->getFilter("contact_id")));
53
        }
54
55
        if ($this->dbquery->checkFilter("text")) {
56
            $this->dbquery->setCondition("(description LIKE \"%".$this->dbquery->getFilter("text")."%\" OR number = \"".$this->dbquery->getFilter("text")."\" OR vendor LIKE \"%".$this->dbquery->getFilter("text")."%\")");
57
        }
58
59
        if ($this->dbquery->checkFilter("from_date")) {
60
            $date = new Intraface_Date($this->dbquery->getFilter("from_date"));
61
            if ($date->convert2db()) {
62
                $this->dbquery->setCondition("invoice_date >= \"".$date->get()."\"");
63
            } else {
64
                $this->error->set("Fra dato er ikke gyldig");
65
            }
66
        }
67
68
        // Posts with invoice date before end date
69
        if ($this->dbquery->checkFilter("to_date")) {
70
            $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
71
            if ($date->convert2db()) {
72
                $this->dbquery->setCondition("invoice_date <= \"".$date->get()."\"");
73
            } else {
74
                $this->error->set("Til dato er ikke gyldig");
75
            }
76
        }
77
78
        if ($this->dbquery->checkFilter("status")) {
79
            if ($this->dbquery->getFilter("status") == "-1") {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
80
            } elseif ($this->dbquery->getFilter("status") == "-2") {
81
                $this->dbquery->setCondition("status_key < 1 OR (status_key = 1 AND paid_date = \"0000-00-00\")");
82
            } else {
83
                if ($this->dbquery->checkFilter("to_date")) {
84
                    switch ($this->dbquery->getFilter("status")) {
85
                        case "0":
86
                            $to_date_field = "date_created";
87
                            break;
88
89
                        case "1":
90
                            $to_date_field = "date_recieved";
91
                            break;
92
93
                        case "2":
94
                            $to_date_field = "data_canceled";
95
                            break;
96
                    }
97
98
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
99
                    if ($date->convert2db()) {
100
                        $this->dbquery->setCondition($to_date_field." <= \"".$date->get()."\"");
0 ignored issues
show
Bug introduced by
The variable $to_date_field does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
101
                    }
102
                } else {
103
                    // takes everyone with a status
104
                    $this->dbquery->setCondition("status_key = ".intval($this->dbquery->getFilter("status")));
105
                }
106
            }
107
        }
108
109
        if ($this->dbquery->getFilter('not_stated') == 1) {
110
            $this->dbquery->setCondition("voucher_id = 0");
111
        }
112
113
        $i = 0;
114
115
        $this->dbquery->setSorting("date_created DESC");
116
        $db = $this->dbquery->getRecordset("*,
117
            DATE_FORMAT(invoice_date, '%d-%m-%Y') AS dk_invoice_date,
118
            DATE_FORMAT(delivery_date, '%d-%m-%Y') AS dk_delivery_date,
119
            DATE_FORMAT(payment_date, '%d-%m-%Y') AS dk_payment_date,
120
            DATE_FORMAT(paid_date, '%d-%m-%Y') AS dk_paid_date");
121
122
        $status_types = $this->getStatusTypes();
123
        while ($db->nextRecord()) {
124
            $list[$i]["id"] = $db->f("id");
125
            $list[$i]["description"] = $db->f("description");
126
            $list[$i]["number"] = $db->f("number");
127
            $list[$i]["vendor"] = $db->f("vendor");
128
            $list[$i]["status_key"] = $db->f("status_key");
129
130
            $list[$i]["status"] = $status_types[$db->f("status_key")];
131
            $list[$i]["delivery_date"] = $db->f("delivery_date");
132
            $list[$i]["dk_delivery_date"] = $db->f("dk_delivery_date");
133
            $list[$i]["dk_invoice_date"] = $db->f("dk_invoice_date");
134
            $list[$i]["payment_date"] = $db->f("payment_date");
135
            $list[$i]["dk_payment_date"] = $db->f("dk_payment_date");
136
            $list[$i]["paid_date"] = $db->f("paid_date");
137
            $list[$i]["dk_paid_date"] = $db->f("dk_paid_date");
138
            $list[$i]["contact_id"] = $db->f("contact_id");
139
140
            if ($list[$i]["contact_id"] > 0) {
141
                $contact = new Contact($this->kernel, $list[$i]["contact_id"]);
142
                $list[$i]["contact"] = $contact->get('name');
143
            } else {
144
                $list[$i]["contact"] = 'Unknown';
145
            }
146
147
            $list[$i]["contact_id"] = $db->f("contact_id");
148
149
            $list[$i]["total_price"] = round($db->f("price_items") + $db->f("price_shipment_etc") + $db->f("vat"), 2);
150
            ;
151
152
            $i++;
153
        }
154
155
        return $list;
156
    }
157
158
    function any()
159
    {
160
        $db = new DB_Sql;
161
        $db->query("SELECT id FROM procurement WHERE intranet_id = " . $this->kernel->intranet->get('id'));
162
        return $db->numRows();
163
    }
164
165
    function anyByContact($contact_id)
166
    {
167
        $db = new DB_Sql;
168
        $db->query("SELECT id FROM procurement WHERE intranet_id = " . $this->kernel->intranet->get('id')." AND contact_id = ".$contact_id." AND active = 1");
169
        return $db->numRows();
170
    }
171
172 View Code Duplication
    function getMaxNumber()
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...
173
    {
174
        $db = new DB_sql;
175
176
        $db->query("SELECT MAX(number) as max_number FROM procurement WHERE intranet_id = ".$this->kernel->intranet->get("id"));
177
        $db->nextRecord();
178
179
        return $db->f("max_number");
180
    }
181
182
    /**
183
     * returns possible status types
184
     * @todo: duplicate in Procurement class
185
     *
186
     * @return array status types
187
     */
188
    public function getStatusTypes()
189
    {
190
        return array(
191
            0 => 'ordered',
192
            1 => 'recieved',
193
            2 => 'canceled'
194
        );
195
    }
196
197
    /**
198
     * returns the possible regions where procurement is bought
199
     * @todo: duplicate in Procurement class
200
     *
201
     * @return array possible regions
202
     */
203
    public function getRegionTypes()
204
    {
205
        return array(
206
            0 => 'denmark',
207
            1 => 'eu',
208
            2 => 'eu_vat_registered',
209
            3 => 'outside_eu'
210
        );
211
    }
212
213 View Code Duplication
    public function findCountByContactId($contact_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...
214
    {
215
        $sql = "SELECT id
216
                FROM procurement
217
                WHERE intranet_id = " . $this->kernel->intranet->get("id") . "
218
                    AND contact_id = ".(int)$contact_id."
219
                    AND active = 1";
220
221
        $db = new DB_Sql;
222
        $db->query($sql);
223
        return $db->numRows();
224
    }
225
}
226