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