Conditions | 30 |
Paths | 35 |
Total Lines | 105 |
Lines | 0 |
Ratio | 0 % |
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:
1 | <?php |
||
76 | public function __construct(DBField $field, $conditionType, $value) { |
||
77 | $this->type = self::sqlConditionType($conditionType); |
||
78 | if (!Tools::isInstanceOf($value, "DBField")) { |
||
79 | $this->field = $field; |
||
80 | |||
81 | switch ($this->type) { |
||
82 | case ("="): |
||
83 | case ("<"): |
||
84 | case (">"): |
||
85 | case ("!="): |
||
86 | $this->value = DBField::castValue($this->field->type, $value); |
||
87 | |||
88 | $this->sqlCondition = "`" . $field->name . "` " |
||
89 | . $this->type . " " |
||
90 | . DBField::sqlValue($this->field->type, $value); |
||
91 | |||
92 | $this->preparedCondition = "`" . $field->name . "` " . $this->type . " ?"; |
||
93 | $this->preparedTypes = $this->field->type; |
||
94 | $this->preparedData = [DBField::sqlValue($this->field->type, $value)]; |
||
95 | break; |
||
96 | case ("LIKE"): |
||
97 | case ("NOT LIKE"): |
||
98 | $this->value = DBField::castValue($this->field->type, $value); |
||
99 | |||
100 | if ($this->field->type != "s") { |
||
101 | throw new DBQueryConditionException("Field type is not a string"); |
||
102 | } |
||
103 | $this->sqlCondition = "`" . $field->name . "` " |
||
104 | . $this->type . " " |
||
105 | . DBField::sqlValue($this->field->type, $value); |
||
106 | |||
107 | $this->preparedCondition = "`" . $field->name . "` " . $this->type . " ?"; |
||
108 | $this->preparedTypes = $this->field->type; |
||
109 | $this->preparedData = [DBField::sqlValue($this->field->type, $value)]; |
||
110 | break; |
||
111 | case ("IN"): |
||
112 | case ("NOT IN"): |
||
113 | if (is_array($value) && !empty($value)) { |
||
114 | $dataList = []; |
||
115 | foreach ($value as $dataItem) { |
||
116 | $dataList[] = DBField::sqlValue($this->field->type, $dataItem); |
||
117 | } |
||
118 | $dataList = array_unique($dataList); |
||
119 | $count = count($dataList); |
||
120 | if ($count > 0) { |
||
121 | $qmStr = "?"; |
||
122 | $tStr = $this->field->type; |
||
123 | for ($i = 1; $i < $count; $i ++) { |
||
124 | $qmStr .= ", ?"; |
||
125 | $tStr .= $this->field->type; |
||
126 | } |
||
127 | } else { |
||
128 | $this->sqlCondition = "1"; |
||
129 | |||
130 | return; |
||
131 | } |
||
132 | |||
133 | $this->sqlCondition = "`" . $field->name . "` " |
||
134 | . $this->type |
||
135 | . " (" . implode(", ", $dataList) . ")"; |
||
136 | |||
137 | $this->preparedCondition = "`" . $field->name . "` " . $this->type . " (" . $qmStr . ")"; |
||
138 | $this->preparedTypes = $tStr; |
||
139 | $this->preparedData = $dataList; |
||
140 | } else { |
||
141 | throw new DBQueryConditionException("Invalid data for 'IN'/'NOT IN' condition"); |
||
142 | } |
||
143 | break; |
||
144 | case ("BETWEEN"): |
||
145 | if (is_array($value) && count($value) == 2 && isset($value[0]) && isset($value[1])) { |
||
146 | $from = DBField::sqlValue($this->field->type, $value[0]); |
||
147 | $to = DBField::sqlValue($this->field->type, $value[1]); |
||
148 | $this->sqlCondition = "`" . $field->name . "` BETWEEN " . $from . " AND " . $to; |
||
149 | |||
150 | $this->preparedCondition = "`" . $field->name . "` BETWEEN ? AND ?"; |
||
151 | $this->preparedTypes = $this->field->type . $this->field->type; |
||
152 | $this->preparedData = [$from, $to]; |
||
153 | } else { |
||
154 | throw new DBQueryConditionException("Invalid data for 'BETWEEN' condition"); |
||
155 | } |
||
156 | break; |
||
157 | } |
||
158 | } else { |
||
159 | $field1 = $field; |
||
160 | $field2 = $value; |
||
161 | |||
162 | switch ($this->type) { |
||
163 | case ("="): |
||
164 | case ("<"): |
||
165 | case (">"): |
||
166 | case ("!="): |
||
167 | case ("LIKE"): |
||
168 | case ("NOT LIKE"): |
||
169 | $this->sqlCondition = "`" . $field1->name . "` " . $this->type . " `" . $field2->name . "`"; |
||
170 | break; |
||
171 | case ("IN"): |
||
172 | case ("NOT IN"): |
||
173 | // impossible, use array instead of DBField |
||
174 | break; |
||
175 | case ("BETWEEN"): |
||
176 | // impossible, use array instead of DBField |
||
177 | break; |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
290 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.