Conditions | 10 |
Paths | 21 |
Total Lines | 66 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
121 | public function advanced() |
||
122 | { |
||
123 | $this->set('titleForPage', __d('saito_search', 'advanced.t')); |
||
124 | |||
125 | $queryData = $this->request->getQueryParams(); |
||
126 | |||
127 | $now = Chronos::now(); |
||
128 | |||
129 | /// Setup time filter data |
||
130 | $first = $this->Entries->find() |
||
131 | ->order(['id' => 'ASC']) |
||
132 | ->first(); |
||
133 | if ($first) { |
||
134 | $startDate = $first->get('time'); |
||
135 | /// Limit default search range to one year in the past |
||
136 | $aYearAgo = new FrozenDate('-1 year'); |
||
137 | $defaultDate = $startDate < $aYearAgo ? $aYearAgo : $startDate; |
||
138 | } else { |
||
139 | /// No entries yet |
||
140 | $startDate = $defaultDate = $now; |
||
141 | } |
||
142 | $startYear = $startDate->format('Y'); |
||
143 | |||
144 | // calculate current month and year |
||
145 | $month = $queryData['month']['month'] ?? $defaultDate->month; |
||
146 | $year = $queryData['year']['year'] ?? $defaultDate->year; |
||
147 | $this->set(compact('month', 'year', 'startYear')); |
||
148 | |||
149 | /// Category drop-down data |
||
150 | $categories = $this->CurrentUser->getCategories()->getAll('read', 'select'); |
||
151 | $this->set('categories', $categories); |
||
152 | |||
153 | if (empty($queryData['subject']) && empty($queryData['text']) && empty($queryData['name'])) { |
||
154 | // just show form; |
||
155 | return; |
||
156 | } |
||
157 | |||
158 | /// setup find |
||
159 | $query = $this->Entries |
||
160 | ->find('search', ['search' => $queryData]) |
||
161 | ->contain(['Categories', 'Users']) |
||
162 | ->order(['Entries.id' => 'DESC']); |
||
163 | |||
164 | /// Time filter |
||
165 | $time = Chronos::createFromDate($year, $month, 1); |
||
166 | if ($now->year !== $defaultDate->year || $now->month !== $defaultDate->month) { |
||
167 | $query->where(['time >=' => $time]); |
||
168 | } |
||
169 | |||
170 | /// Category filter |
||
171 | $categories = array_flip($categories); |
||
172 | if (!empty($queryData['category_id'])) { |
||
173 | $category = $queryData['category_id']; |
||
174 | if (!in_array($category, $categories)) { |
||
175 | throw new SaitoForbiddenException( |
||
176 | "Tried to search category $category.", |
||
177 | ['CurrentUser' => $this->CurrentUser] |
||
178 | ); |
||
179 | } |
||
180 | $categories = [$category]; |
||
181 | } |
||
182 | $query->where(['category_id IN' => $categories]); |
||
183 | |||
184 | $results = $this->paginate($query); |
||
185 | $this->set(compact('results')); |
||
186 | $this->set('showBottomNavigation', true); |
||
187 | } |
||
189 |