Conditions | 13 |
Paths | 274 |
Total Lines | 95 |
Code Lines | 57 |
Lines | 3 |
Ratio | 3.16 % |
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 |
||
160 | case 'views': |
||
161 | $query = $query->orderBy('views', 'DESC'); |
||
162 | break; |
||
163 | default: |
||
164 | $query = $query->orderBy('created_at', 'DESC'); |
||
165 | break; |
||
166 | } |
||
167 | |||
168 | // make select based on offset |
||
169 | return $query->skip($offset)->take($itemPerPage)->get(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Prepare category data to display |
||
174 | * @throws ForbiddenException |
||
175 | */ |
||
176 | private function buildCategory() |
||
177 | { |
||
178 | // prepare rss url link for current category if enabled |
||
179 | $rssUrl = false; |
||
180 | if ((int)$this->_configs['rss'] === 1) { |
||
181 | $rssUrl = App::$Alias->baseUrl . '/content/rss/' . $this->_currentCategory->path; |
||
182 | $rssUrl = rtrim($rssUrl, '/'); |
||
183 | } |
||
184 | |||
185 | // prepare sorting urls |
||
186 | $catSortParams = []; |
||
187 | if (App::$Request->query->get('page') !== null) { |
||
188 | $catSortParams['page'] = (int)App::$Request->query->get('page'); |
||
189 | } |
||
190 | $catSortUrls = [ |
||
191 | 'views' => Url::to('content/list', $this->_currentCategory->path, null, Arr::merge($catSortParams, ['sort' => 'views']), false), |
||
192 | 'rating' => Url::to('content/list', $this->_currentCategory->path, null, Arr::merge($catSortParams, ['sort' => 'rating']), false), |
||
193 | 'newest' => Url::to('content/list', $this->_currentCategory->path, null, $catSortParams, false) |
||
194 | ]; |
||
195 | |||
196 | // prepare current category data to output (unserialize locales and strip tags) |
||
197 | $this->category = [ |
||
198 | 'title' => App::$Security->strip_tags($this->_currentCategory->getLocaled('title')), |
||
|
|||
199 | 'description' => App::$Security->strip_tags($this->_currentCategory->getLocaled('description')), |
||
200 | 'configs' => Serialize::decode($this->_currentCategory->configs), |
||
201 | 'path' => $this->_currentCategory->path, |
||
202 | 'rss' => $rssUrl, |
||
203 | 'sort' => $catSortUrls |
||
204 | ]; |
||
205 | |||
206 | // check if this category is hidden |
||
207 | View Code Duplication | if ((int)$this->category['configs']['showCategory'] !== 1) { |
|
208 | throw new ForbiddenException(__('This category is not available to view')); |
||
209 | } |
||
210 | |||
211 | // make sorted tree of categories to display in breadcrumbs |
||
212 | foreach ($this->_allCategories as $cat) { |
||
213 | $this->categories[$cat->id] = $cat; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Build content data to model properties |
||
219 | * @param $records |
||
220 | * @throws ForbiddenException |
||
221 | * @throws NotFoundException |
||
222 | */ |
||
223 | private function buildContent($records) |
||
224 | { |
||
225 | $nullItems = 0; |
||
226 | foreach ($records as $row) { |
||
227 | /** @var Content $row */ |
||
228 | |||
229 | // check title length on current language locale |
||
230 | $localeTitle = App::$Security->strip_tags($row->getLocaled('title')); |
||
231 | if (Str::likeEmpty($localeTitle)) { |
||
232 | ++$nullItems; |
||
233 | continue; |
||
234 | } |
||
235 | |||
236 | // get snippet from full text for current locale |
||
237 | $text = Text::snippet($row->getLocaled('text')); |
||
238 | |||
239 | $itemPath = $this->categories[$row->category_id]->path; |
||
240 | if (!Str::likeEmpty($itemPath)) { |
||
241 | $itemPath .= '/'; |
||
242 | } |
||
243 | $itemPath .= $row->path; |
||
244 | |||
245 | // prepare tags data |
||
246 | $tags = $row->getLocaled('meta_keywords'); |
||
247 | if (!Str::likeEmpty($tags)) { |
||
248 | $tags = explode(',', $tags); |
||
249 | } else { |
||
250 | $tags = null; |
||
251 | } |
||
252 | |||
253 | $owner = App::$User->identity($row->author_id); |
||
254 | // make a fake if user is not exist over id |
||
255 | if ($owner === null) { |
||
301 | } |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.