| Conditions | 3 |
| Paths | 3 |
| Total Lines | 52 |
| 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 |
||
| 70 | protected function _getInitialThreads(CurrentUserInterface $User, $order) |
||
| 71 | { |
||
| 72 | Stopwatch::start('Entries->_getInitialThreads() Paginate'); |
||
| 73 | $categories = $User->getCategories()->getCurrent('read'); |
||
| 74 | if (empty($categories)) { |
||
| 75 | // no readable categories for user (e.g. no public categories |
||
| 76 | return []; |
||
| 77 | } |
||
| 78 | |||
| 79 | ////! Check DB performance after changing conditions/sorting! |
||
| 80 | $customFinderOptions = [ |
||
| 81 | 'conditions' => [ |
||
| 82 | 'Entries.category_id IN' => $categories |
||
| 83 | ], |
||
| 84 | // @td sanitize input? |
||
| 85 | 'limit' => Configure::read('Saito.Settings.topics_per_page'), |
||
| 86 | 'order' => $order, |
||
| 87 | // Performance: Custom counter from categories counter-cache; |
||
| 88 | // avoids a costly COUNT(*) DB call counting all pages for pagination. |
||
| 89 | 'counter' => function ($query) use ($categories) { |
||
|
|
|||
| 90 | $results = $this->Entries->Categories->find('all') |
||
| 91 | ->select(['thread_count']) |
||
| 92 | ->where(['id IN' => $categories]) |
||
| 93 | ->all(); |
||
| 94 | $count = array_reduce( |
||
| 95 | $results->toArray(), |
||
| 96 | function ($carry, Entity $entity) { |
||
| 97 | return $carry + $entity->get('thread_count'); |
||
| 98 | }, |
||
| 99 | 0 |
||
| 100 | ); |
||
| 101 | |||
| 102 | return $count; |
||
| 103 | } |
||
| 104 | ]; |
||
| 105 | |||
| 106 | $settings = [ |
||
| 107 | 'finder' => ['indexPaginator' => $customFinderOptions], |
||
| 108 | ]; |
||
| 109 | |||
| 110 | // use setConfig on Component to not merge but overwrite/set the config |
||
| 111 | $this->Paginator->setConfig('whitelist', ['page'], false); |
||
| 112 | $initialThreads = $this->Paginator->paginate($this->Entries, $settings); |
||
| 113 | |||
| 114 | $initialThreadsNew = []; |
||
| 115 | foreach ($initialThreads as $k => $v) { |
||
| 116 | $initialThreadsNew[$k] = $v['id']; |
||
| 117 | } |
||
| 118 | Stopwatch::stop('Entries->_getInitialThreads() Paginate'); |
||
| 119 | |||
| 120 | return $initialThreadsNew; |
||
| 121 | } |
||
| 122 | |||
| 174 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.