Conditions | 23 |
Paths | 4656 |
Total Lines | 127 |
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 |
||
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 | $where[] = ' '.$dbField.' = :'.$field.' '; |
||
100 | $params[$field] = $filter->{$method}(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | if ($filter->getStartDate()) { |
||
105 | $where[] = ' request_time >= :startDate'; |
||
106 | $params['startDate'] = $this->getDateTimeFromString($filter->getStartDate(), 'start') |
||
|
|||
107 | ->format('Y-m-d H:i:s'); |
||
108 | } |
||
109 | |||
110 | if ($filter->getEndDate()) { |
||
111 | $where[] = ' request_time <= :endDate'; |
||
112 | $params['endDate'] = $this->getDateTimeFromString($filter->getEndDate(), 'end') |
||
113 | ->format('Y-m-d H:i:s'); |
||
114 | } |
||
115 | |||
116 | if (!empty($where)) { |
||
117 | $sql .= ' WHERE '.join(' AND ', $where); |
||
118 | } |
||
119 | |||
120 | if ($count === true) { |
||
121 | return [$sql, $params]; |
||
122 | } |
||
123 | |||
124 | switch ($filter->getSort()) { |
||
125 | case 'ct': |
||
126 | $sql .= ' order by main_ct'; |
||
127 | break; |
||
128 | |||
129 | case 'wt': |
||
130 | $sql .= ' order by main_wt'; |
||
131 | break; |
||
132 | |||
133 | case 'cpu': |
||
134 | $sql .= ' order by main_cpu'; |
||
135 | break; |
||
136 | |||
137 | case 'mu': |
||
138 | $sql .= ' order by main_mu'; |
||
139 | break; |
||
140 | |||
141 | case 'pmu': |
||
142 | $sql .= ' order by main_pmu'; |
||
143 | break; |
||
144 | |||
145 | case 'controller': |
||
146 | $sql .= ' order by controller'; |
||
147 | break; |
||
148 | |||
149 | case 'action': |
||
150 | $sql .= ' order by action'; |
||
151 | break; |
||
152 | |||
153 | case 'application': |
||
154 | $sql .= ' order by application'; |
||
155 | break; |
||
156 | |||
157 | case 'branch': |
||
158 | $sql .= ' order by branch'; |
||
159 | break; |
||
160 | |||
161 | case 'version': |
||
162 | $sql .= ' order by version'; |
||
163 | break; |
||
164 | |||
165 | case 'time': |
||
166 | default: |
||
167 | $sql .= ' order by request_time'; |
||
168 | break; |
||
169 | } |
||
170 | |||
171 | switch ($filter->getDirection()) { |
||
172 | case 'asc': |
||
173 | $sql .= ' asc '; |
||
174 | break; |
||
175 | |||
176 | default: |
||
177 | case 'desc': |
||
178 | $sql .= ' desc '; |
||
179 | break; |
||
180 | } |
||
181 | |||
182 | if ($filter->getPerPage()) { |
||
183 | $sql .= ' LIMIT :limit '; |
||
184 | $params['limit'] = (int)$filter->getPerPage(); |
||
185 | } |
||
186 | |||
187 | if ($filter->getPage()) { |
||
188 | $sql .= ' OFFSET :offset '; |
||
189 | $params['offset'] = (int)($filter->getPerPage()*($filter->getPage()-1)); |
||
190 | } |
||
191 | return [$sql, $params]; |
||
192 | } |
||
193 | |||
394 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: