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