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