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