Passed
Push — master ( 09ebe9...f973a6 )
by Ryan
07:29
created

DBWhere::__toString()   F

Complexity

Conditions 23
Paths 187

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 23.0134

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 23
eloc 38
c 2
b 1
f 1
nc 187
nop 0
dl 0
loc 59
ccs 33
cts 34
cp 0.9706
crap 23.0134
rs 3.4416

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
namespace Godsgood33\Php_Db;
3
4
/**
5
 * Class to create a where clause
6
 * 
7
 * @author Ryan Prather <[email protected]>
8
 */
9
class DBWhere
10
{
11
12
    /**
13
     * Global to represent an IN statement (e.g.
14
     * WHERE field IN (1,2))
15
     *
16
     * @var string
17
     */
18
    const IN = 'IN';
19
20
    /**
21
     * Global to represent a NOT IN statement (e.g.
22
     * WHERE field NOT IN (1,2))
23
     *
24
     * @var string
25
     */
26
    const NOT_IN = 'NOT IN';
27
28
    /**
29
     * Global to represent a BETWEEN statement (e.g.
30
     * WHERE field BETWEEN 1 and 2)
31
     *
32
     * @var string
33
     */
34
    const BETWEEN = 'BETWEEN';
35
36
    /**
37
     * Global to represent a LIKE statement (e.g.
38
     * WHERE field LIKE '%value%')
39
     *
40
     * @var string
41
     */
42
    const LIKE = 'LIKE';
43
44
    /**
45
     * Global to represent a NOT LIKE statement (e.g.
46
     * WHERE field NOT LIKE '%value%')
47
     *
48
     * @var string
49
     */
50
    const NOT_LIKE = 'NOT LIKE';
51
52
    /**
53
     * Global to represent an IS statement (e.g.
54
     * WHERE field IS NULL)
55
     *
56
     * @var string
57
     */
58
    const IS = 'IS';
59
60
    /**
61
     * Global to represent an IS NOT statement (e.g.
62
     * WHERE field IS NOT NULL)
63
     *
64
     * @var string
65
     */
66
    const IS_NOT = 'IS NOT';
67
68
    /**
69
     * Array to store the necessary class variables
70
     * 
71
     * @var array
72
     */
73
    protected $data = [];
74
75
    /**
76
     * Constructor
77
     * 
78
     * @param string $field
79
     * @param mixed $value
80
     * @param string $operator
81
     */
82 37
    public function __construct($field = null, $value = null, $operator = '=')
83
    {
84 37
        $this->data = [
85 37
            'index' => 0,
86 37
            'field' => $field,
87 37
            'value' => $value,
88
            'low' => null,
89
            'high' => null,
90
            'escape' => true,
91 37
            'operator' => $operator,
92 37
            'sqlOperator' => 'AND',
93
            'backticks' => true,
94
            'openParen' => false,
95
            'closeParen' => false,
96
            'caseInsensitive' => false
97
        ];
98 37
    }
99
100
    /**
101
     * Method to return the variables
102
     * 
103
     * @param string $var
104
     * 
105
     * @return mixed
106
     * 
107
     * @throws InvalidArgumentException
108
     */
109 19
    public function __get($var)
110
    {
111 19
        if(!in_array($var, [
112 19
            'index', 'field', 'value', 'low', 'high', 'operator', 'backticks',
113
            'sqlOperator', 'escape', 'openParen', 'closeParen', 'caseInsensitive'
114
        ])) {
115 1
            $trace = \debug_backtrace();
116 1
            throw new \InvalidArgumentException("Property not allowed via __get():  $var in {$trace[0]['file']} on line {$trace[0]['line']}", E_USER_WARNING);
117
        }
118
119 18
        return $this->data[$var];
120
    }
121
122
    /**
123
     * Method to set a variable
124
     * 
125
     * @param string $name
126
     * @param mixed $value
127
     * 
128
     * @throws InvalidArgumentException
129
     */
130 23
    public function __set($name, $value)
131
    {
132 23
        if(!in_array($name, [
133 23
            'index', 'field', 'value', 'low', 'high', 'operator', 'backticks',
134
            'sqlOperator', 'escape', 'openParen', 'closeParen', 'caseInsensitive'
135
        ])) {
136 1
            $trace = \debug_backtrace();
137 1
            throw new \InvalidArgumentException("Property not allowed via __set():  $name in {$trace[0]['file']} on line {$trace[0]['line']}", E_USER_WARNING);
138
        }
139
140 22
        $this->data[$name] = $value;
141 22
    }
142
143
    /**
144
     * Method to parse where clauses
145
     */
146 33
    public function __toString()
147
    {
148 33
        $ret = '';
149
150 33
        if (is_null($this->data['field']) && isset($this->data['closeParen']) && $this->data['closeParen']) {
151 1
            $ret .= ")";
152 1
            return $ret;
153
        }
154
155 32
        switch ($this->data['operator']) {
156 32
            case self::BETWEEN:
157 2
                if (! isset($this->data['field']) || ! isset($this->data['low']) || ! isset($this->data['high'])) {
158 1
                    return '';
159
                }
160 1
                break;
161
            default:
162 30
                if (! $this->data['field']) {
163 1
                    return '';
164
                }
165
        }
166
167 30
        if ($this->data['openParen']) {
168 1
            $ret .= " (";
169
        }
170
171 30
        if (! $this->data['backticks']) {
172 1
            $field = $this->data['field'];
173
        } else {
174 29
            $field = "`{$this->data['field']}`";
175
        }
176
177 30
        if ($this->data['operator'] == self::IN || $this->data['operator'] == self::NOT_IN) {
178 4
            if (is_string($this->data['value'])) {
179 2
                $ret .= " {$field} {$this->data['operator']} " . (strpos($this->data['value'], '(') !== false ? $this->data['value'] : "({$this->data['value']})");
180 2
            } elseif (is_array($this->data['value'])) {
181 2
                $ret .= " {$field} {$this->data['operator']} (" . implode(",", $this->data['value']) . ")";
182
            } else {
183
                return '';
184
            }
185 26
        } elseif ($this->data['operator'] == self::BETWEEN) {
186 1
            $low = (is_string($this->data['low']) ? "'{$this->data['low']}'" : $this->data['low']);
187 1
            $high = (is_string($this->data['high']) ? "'{$this->data['high']}'" : $this->data['high']);
188
189 1
            $ret .= " {$field} BETWEEN {$low} AND {$high}";
190
        } else {
191 25
            $value = (is_null($this->data['value']) ? "NULL" : $this->data['value']);
192
193 25
            if ($this->data['caseInsensitive']) {
194 1
                $ret .= " LOWER({$field}) {$this->data['operator']} LOWER({$value})";
195
            } else {
196 24
                $ret .= " {$field} {$this->data['operator']} {$value}";
197
            }
198
        }
199
200 30
        if (isset($this->data['closeParen']) && $this->data['closeParen']) {
201 1
            $ret .= ")";
202
        }
203
204 30
        return $ret;
205
    }
206
}