Conditions | 24 |
Paths | > 20000 |
Total Lines | 160 |
Code Lines | 96 |
Lines | 14 |
Ratio | 8.75 % |
Changes | 5 | ||
Bugs | 3 | Features | 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 |
||
151 | public function recursiveRender() |
||
152 | { |
||
153 | // get data source |
||
154 | if (!$this->source) { |
||
155 | |||
156 | // force grid sorting implemented in Grid_Advanced |
||
157 | if ($this->owner instanceof Grid_Advanced) { |
||
158 | $this->owner->getIterator(); |
||
159 | } |
||
160 | |||
161 | // set data source for Paginator |
||
162 | if ($this->owner->model) { |
||
163 | $this->setSource($this->owner->model); |
||
164 | } elseif ($this->owner->dq) { |
||
165 | $this->setSource($this->owner->dq); |
||
166 | } else { |
||
167 | throw $this->exception('Unable to find source for Paginator'); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | // calculate found rows |
||
172 | if ($this->source instanceof DB_dsql) { |
||
173 | $this->source->preexec(); |
||
174 | $this->found_rows = $this->source->foundRows(); |
||
175 | } elseif ($this->source instanceof Model) { |
||
176 | $this->found_rows = (int) $this->source->count(); |
||
177 | } else { |
||
178 | $this->found_rows = count($this->source); |
||
179 | } |
||
180 | |||
181 | // calculate current page and total pages |
||
182 | $this->cur_page = (int) floor($this->skip / $this->ipp) + 1; |
||
183 | $this->total_pages = (int) ceil($this->found_rows / $this->ipp); |
||
184 | |||
185 | if ($this->cur_page > $this->total_pages || ($this->cur_page == 1 && $this->skip != 0)) { |
||
186 | $this->cur_page = 1; |
||
187 | if ($this->memorize) { |
||
188 | $this->memorize('skip', $this->skip = 0); |
||
189 | } |
||
190 | if ($this->source instanceof DB_dsql) { |
||
191 | $this->source->limit($this->ipp, $this->skip); |
||
192 | $this->source->rewind(); // re-execute the query |
||
193 | } elseif ($this->source instanceof Model) { |
||
194 | $this->source->setLimit($this->ipp, $this->skip); |
||
195 | } else { |
||
196 | // Imants: not sure if this is correct, but it was like this before |
||
197 | $this->source->setLimit($this->ipp, $this->skip); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | // no need for paginator if there is only one page |
||
202 | if ($this->total_pages <= 1) { |
||
203 | return $this->destroy(); |
||
204 | } |
||
205 | |||
206 | if ($this->cur_page > 1) { |
||
207 | /** @type View $v */ |
||
208 | $v = $this->add('View', null, 'prev'); |
||
209 | $v->setElement('a') |
||
210 | ->setAttr('href', $this->app->url( |
||
211 | $this->base_page, |
||
212 | $u = array($this->skip_var => $pn = max(0, $this->skip - $this->ipp)) |
||
213 | )) |
||
214 | ->setAttr('data-skip', $pn) |
||
215 | ->set('« Prev') |
||
216 | ; |
||
217 | } else { |
||
218 | $this->template->tryDel('prev'); |
||
219 | } |
||
220 | |||
221 | if ($this->cur_page < $this->total_pages) { |
||
222 | /** @type View $v */ |
||
223 | $v = $this->add('View', null, 'next'); |
||
224 | $v->setElement('a') |
||
225 | ->setAttr('href', $this->app->url( |
||
226 | $this->base_page, |
||
227 | $u = array($this->skip_var => $pn = $this->skip + $this->ipp) |
||
228 | )) |
||
229 | ->setAttr('data-skip', $pn) |
||
230 | ->set('Next »') |
||
231 | ; |
||
232 | } else { |
||
233 | $this->template->tryDel('next'); |
||
234 | } |
||
235 | |||
236 | // First page |
||
237 | if ($this->cur_page > $this->range + 1) { |
||
238 | /** @type View $v */ |
||
239 | $v = $this->add('View', null, 'first'); |
||
240 | $v->setElement('a') |
||
241 | ->setAttr('href', $this->app->url( |
||
242 | $this->base_page, |
||
243 | $u = array($this->skip_var => $pn = max(0, 0)) |
||
244 | )) |
||
245 | ->setAttr('data-skip', $pn) |
||
246 | ->set('1') |
||
247 | ; |
||
248 | View Code Duplication | if ($this->cur_page > $this->range + 2) { |
|
249 | /** @type View $v */ |
||
250 | $v = $this->add('View', null, 'points_left'); |
||
251 | $v->setElement('span') |
||
252 | ->set('...') |
||
253 | ; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // Last page |
||
258 | if ($this->cur_page < $this->total_pages - $this->range) { |
||
259 | /** @type View $v */ |
||
260 | $v = $this->add('View', null, 'last'); |
||
261 | $v->setElement('a') |
||
262 | ->setAttr('href', $this->app->url( |
||
263 | $this->base_page, |
||
264 | $u = array($this->skip_var => $pn = max(0, ($this->total_pages - 1) * $this->ipp)) |
||
265 | )) |
||
266 | ->setAttr('data-skip', $pn) |
||
267 | ->set($this->total_pages) |
||
268 | ; |
||
269 | View Code Duplication | if ($this->cur_page < $this->total_pages - $this->range - 1) { |
|
270 | /** @type View $v */ |
||
271 | $v = $this->add('View', null, 'points_right'); |
||
272 | $v->setElement('span') |
||
273 | ->set('...') |
||
274 | ; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | // generate source for Paginator Lister (pages, links, labels etc.) |
||
279 | $data = array(); |
||
280 | |||
281 | //setting cur as array seems not working in atk4.3. String is working |
||
282 | $tplcur = $this->template->get('cur'); |
||
283 | $tplcur = (isset($tplcur[0])) ? $tplcur[0] : ''; |
||
284 | |||
285 | $range = range( |
||
286 | max(1, $this->cur_page - $this->range), |
||
287 | min($this->total_pages, $this->cur_page + $this->range) |
||
288 | ); |
||
289 | foreach ($range as $p) { |
||
290 | $data[] = array( |
||
291 | 'href' => $this->app->url($this->base_page, array($this->skip_var => $pn = ($p - 1) * $this->ipp)), |
||
292 | 'pn' => $pn, |
||
293 | 'cur' => $p == $this->cur_page ? $tplcur : '', |
||
294 | 'label' => $p, |
||
295 | ); |
||
296 | } |
||
297 | |||
298 | if ($this->ajax_reload) { |
||
299 | $this->js( |
||
300 | 'click', |
||
301 | $this->owner->js()->reload( |
||
302 | array($this->skip_var => $this->js()->_selectorThis()->attr('data-skip')) |
||
303 | ) |
||
304 | )->_selector('#'.$this->name.' a'); |
||
305 | } |
||
306 | |||
307 | parent::setSource($data); |
||
|
|||
308 | |||
309 | return parent::recursiveRender(); |
||
310 | } |
||
311 | |||
332 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.