Conditions | 26 |
Paths | 9312 |
Total Lines | 144 |
Lines | 8 |
Ratio | 5.56 % |
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 |
||
66 | protected function getQuery(\Xhgui_Storage_Filter $filter, $count = false) |
||
67 | { |
||
68 | $params = []; |
||
69 | |||
70 | if ($count === true) { |
||
71 | $columns = ' count(*) as c '; |
||
72 | } else { |
||
73 | $columns = ' p.*, i.*, m.*, p.profile_id as _id, main_wt as duration '; |
||
74 | } |
||
75 | |||
76 | $sql = " |
||
77 | select |
||
78 | $columns |
||
79 | from |
||
80 | profiles as p left join |
||
81 | profiles_info as i on (p.profile_id = i.id) LEFT JOIN |
||
82 | profiles_meta as m on (p.profile_id = m.profile_id) |
||
83 | "; |
||
84 | |||
85 | $where = []; |
||
86 | |||
87 | foreach ([ |
||
88 | 'url' => 'url', |
||
89 | 'method' => 'method', |
||
90 | 'application' => 'application', |
||
91 | 'version' => 'version', |
||
92 | 'branch' => 'branch', |
||
93 | 'controller' => 'controller', |
||
94 | 'action' => 'action', |
||
95 | ] as $dbField => $field) { |
||
96 | $method = 'get'.ucfirst($field); |
||
97 | |||
98 | if ($filter->{$method}()) { |
||
99 | switch($field) { |
||
100 | case 'url': |
||
101 | $url = $filter->{$method}(); |
||
102 | $where[] = ' ( url like :url OR simple_url like :simple_url)'; |
||
103 | $params['url'] = '%'.$url.'%'; |
||
104 | $params['simple_url'] = '%'.$url.'%'; |
||
105 | break; |
||
106 | |||
107 | case 'action': |
||
108 | View Code Duplication | case 'controller': |
|
|
|||
109 | $where[] = ' '.$dbField.' like :'.$field.' '; |
||
110 | $params[$field] = ($filter->{$method}()).'%'; |
||
111 | break; |
||
112 | |||
113 | View Code Duplication | default: |
|
114 | $where[] = ' '.$dbField.' = :'.$field.' '; |
||
115 | $params[$field] = $filter->{$method}(); |
||
116 | break; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if ($filter->getStartDate()) { |
||
122 | $where[] = ' request_time >= :startDate'; |
||
123 | $params['startDate'] = $this->getDateTimeFromString($filter->getStartDate(), 'start') |
||
124 | ->format('Y-m-d H:i:s'); |
||
125 | } |
||
126 | |||
127 | if ($filter->getEndDate()) { |
||
128 | $where[] = ' request_time <= :endDate'; |
||
129 | $params['endDate'] = $this->getDateTimeFromString($filter->getEndDate(), 'end') |
||
130 | ->format('Y-m-d H:i:s'); |
||
131 | } |
||
132 | |||
133 | if (!empty($where)) { |
||
134 | $sql .= ' WHERE '.join(' AND ', $where); |
||
135 | } |
||
136 | |||
137 | if ($count === true) { |
||
138 | return [$sql, $params]; |
||
139 | } |
||
140 | |||
141 | switch ($filter->getSort()) { |
||
142 | case 'ct': |
||
143 | $sql .= ' order by main_ct'; |
||
144 | break; |
||
145 | |||
146 | case 'wt': |
||
147 | $sql .= ' order by main_wt'; |
||
148 | break; |
||
149 | |||
150 | case 'cpu': |
||
151 | $sql .= ' order by main_cpu'; |
||
152 | break; |
||
153 | |||
154 | case 'mu': |
||
155 | $sql .= ' order by main_mu'; |
||
156 | break; |
||
157 | |||
158 | case 'pmu': |
||
159 | $sql .= ' order by main_pmu'; |
||
160 | break; |
||
161 | |||
162 | case 'controller': |
||
163 | $sql .= ' order by controller'; |
||
164 | break; |
||
165 | |||
166 | case 'action': |
||
167 | $sql .= ' order by action'; |
||
168 | break; |
||
169 | |||
170 | case 'application': |
||
171 | $sql .= ' order by application'; |
||
172 | break; |
||
173 | |||
174 | case 'branch': |
||
175 | $sql .= ' order by branch'; |
||
176 | break; |
||
177 | |||
178 | case 'version': |
||
179 | $sql .= ' order by version'; |
||
180 | break; |
||
181 | |||
182 | case 'time': |
||
183 | default: |
||
184 | $sql .= ' order by request_time'; |
||
185 | break; |
||
186 | } |
||
187 | |||
188 | switch ($filter->getDirection()) { |
||
189 | case 'asc': |
||
190 | $sql .= ' asc '; |
||
191 | break; |
||
192 | |||
193 | default: |
||
194 | case 'desc': |
||
195 | $sql .= ' desc '; |
||
196 | break; |
||
197 | } |
||
198 | |||
199 | if ($filter->getPerPage()) { |
||
200 | $sql .= ' LIMIT :limit '; |
||
201 | $params['limit'] = (int)$filter->getPerPage(); |
||
202 | } |
||
203 | |||
204 | if ($filter->getPage()) { |
||
205 | $sql .= ' OFFSET :offset '; |
||
206 | $params['offset'] = (int)($filter->getPerPage()*($filter->getPage()-1)); |
||
207 | } |
||
208 | return [$sql, $params]; |
||
209 | } |
||
210 | |||
392 |
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.