Conditions | 27 |
Paths | 10864 |
Total Lines | 158 |
Lines | 8 |
Ratio | 5.06 % |
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 |
||
87 | protected function getQuery(\Xhgui_Storage_Filter $filter, $count = false) |
||
88 | { |
||
89 | $params = []; |
||
90 | |||
91 | if ($count === true) { |
||
92 | $columns = ' count(*) as c '; |
||
93 | } else { |
||
94 | $columns = ' p.*, i.*, m.*, p.profile_id as _id, main_wt as duration '; |
||
95 | } |
||
96 | |||
97 | $sql = " |
||
98 | select |
||
99 | $columns |
||
100 | from |
||
101 | profiles as p left join |
||
102 | profiles_info as i on (p.profile_id = i.id) LEFT JOIN |
||
103 | profiles_meta as m on (p.profile_id = m.profile_id) |
||
104 | "; |
||
105 | |||
106 | $where = []; |
||
107 | |||
108 | foreach ([ |
||
109 | 'url' => 'url', |
||
110 | 'method' => 'method', |
||
111 | 'application' => 'application', |
||
112 | 'version' => 'version', |
||
113 | 'branch' => 'branch', |
||
114 | 'controller' => 'controller', |
||
115 | 'action' => 'action', |
||
116 | 'cookie' => 'cookie', |
||
117 | 'remote_addr' => 'ip', |
||
118 | ] as $dbField => $field) { |
||
119 | $method = 'get' . ucfirst($field); |
||
120 | |||
121 | if ($filter->{$method}()) { |
||
122 | switch ($field) { |
||
123 | case 'url': |
||
124 | $url = $filter->{$method}(); |
||
125 | $where[] = ' ( url like :url OR simple_url like :simple_url)'; |
||
126 | $params['url'] = '%' . $url . '%'; |
||
127 | $params['simple_url'] = '%' . $url . '%'; |
||
128 | break; |
||
129 | |||
130 | case 'action': |
||
131 | View Code Duplication | case 'controller': |
|
132 | $where[] = ' ' . $dbField . ' like :' . $field . ' '; |
||
133 | $params[$field] = ($filter->{$method}()) . '%'; |
||
134 | break; |
||
135 | |||
136 | case 'cookie': |
||
137 | // @todo move this to driver specific storage class |
||
138 | list($where, $params) = $this->compareWithJson( |
||
139 | $where, |
||
140 | $params, |
||
141 | $field, |
||
142 | $filter->{$method}(), |
||
143 | 'meta', |
||
144 | ['SERVER', 'HTTP_COOKIE'] |
||
145 | ); |
||
146 | break; |
||
147 | |||
148 | View Code Duplication | default: |
|
149 | $where[] = ' ' . $dbField . ' = :' . $field . ' '; |
||
150 | $params[$field] = $filter->{$method}(); |
||
151 | break; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | if ($filter->getStartDate()) { |
||
157 | $where[] = ' request_time >= :startDate'; |
||
158 | $params['startDate'] = $this->getDateTimeFromString($filter->getStartDate(), 'start') |
||
159 | ->format('Y-m-d H:i:s'); |
||
160 | } |
||
161 | |||
162 | if ($filter->getEndDate()) { |
||
163 | $where[] = ' request_time <= :endDate'; |
||
164 | $params['endDate'] = $this->getDateTimeFromString($filter->getEndDate(), 'end') |
||
165 | ->format('Y-m-d H:i:s'); |
||
166 | } |
||
167 | |||
168 | if (!empty($where)) { |
||
169 | $sql .= ' WHERE ' . join(' AND ', $where); |
||
170 | } |
||
171 | |||
172 | if ($count === true) { |
||
173 | return [$sql, $params]; |
||
174 | } |
||
175 | |||
176 | switch ($filter->getSort()) { |
||
177 | case 'ct': |
||
178 | $sql .= ' order by main_ct'; |
||
179 | break; |
||
180 | |||
181 | case 'wt': |
||
182 | $sql .= ' order by main_wt'; |
||
183 | break; |
||
184 | |||
185 | case 'cpu': |
||
186 | $sql .= ' order by main_cpu'; |
||
187 | break; |
||
188 | |||
189 | case 'mu': |
||
190 | $sql .= ' order by main_mu'; |
||
191 | break; |
||
192 | |||
193 | case 'pmu': |
||
194 | $sql .= ' order by main_pmu'; |
||
195 | break; |
||
196 | |||
197 | case 'controller': |
||
198 | $sql .= ' order by controller'; |
||
199 | break; |
||
200 | |||
201 | case 'action': |
||
202 | $sql .= ' order by action'; |
||
203 | break; |
||
204 | |||
205 | case 'application': |
||
206 | $sql .= ' order by application'; |
||
207 | break; |
||
208 | |||
209 | case 'branch': |
||
210 | $sql .= ' order by branch'; |
||
211 | break; |
||
212 | |||
213 | case 'version': |
||
214 | $sql .= ' order by version'; |
||
215 | break; |
||
216 | |||
217 | case 'time': |
||
218 | default: |
||
219 | $sql .= ' order by request_time'; |
||
220 | break; |
||
221 | } |
||
222 | |||
223 | switch ($filter->getDirection()) { |
||
224 | case 'asc': |
||
225 | $sql .= ' asc '; |
||
226 | break; |
||
227 | |||
228 | default: |
||
229 | case 'desc': |
||
230 | $sql .= ' desc '; |
||
231 | break; |
||
232 | } |
||
233 | |||
234 | if ($filter->getPerPage()) { |
||
235 | $sql .= ' LIMIT :limit '; |
||
236 | $params['limit'] = (int)$filter->getPerPage(); |
||
237 | } |
||
238 | |||
239 | if ($filter->getPage()) { |
||
240 | $sql .= ' OFFSET :offset '; |
||
241 | $params['offset'] = (int)($filter->getPerPage() * ($filter->getPage() - 1)); |
||
242 | } |
||
243 | return [$sql, $params]; |
||
244 | } |
||
245 | |||
464 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.