| Conditions | 21 |
| Paths | > 20000 |
| Total Lines | 85 |
| Code Lines | 60 |
| 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 |
||
| 15 | public function index(Request $request, string $id = '') |
||
| 16 | { |
||
| 17 | $book = new Books(['Settings' => $this->settings]); |
||
|
|
|||
| 18 | |||
| 19 | $boocats = Category::getChildren(Category::BOOKS_ROOT); |
||
| 20 | |||
| 21 | $btmp = []; |
||
| 22 | foreach ($boocats as $bcat) { |
||
| 23 | $btmp[] = |
||
| 24 | [ |
||
| 25 | 'id' => $bcat->id, |
||
| 26 | 'title' => $bcat->title, |
||
| 27 | ]; |
||
| 28 | } |
||
| 29 | $category = $request->has('t') ? $request->input('t') : Category::BOOKS_ROOT; |
||
| 30 | if ($id && \in_array($id, Arr::pluck($btmp, 'title'), false)) { |
||
| 31 | $cat = Category::query() |
||
| 32 | ->where('title', $id) |
||
| 33 | ->where('root_categories_id', '=', Category::BOOKS_ROOT) |
||
| 34 | ->first(['id']); |
||
| 35 | $category = $cat !== null ? $cat['id'] : Category::BOOKS_ROOT; |
||
| 36 | } |
||
| 37 | |||
| 38 | $catarray = []; |
||
| 39 | $catarray[] = $category; |
||
| 40 | |||
| 41 | $ordering = $book->getBookOrdering(); |
||
| 42 | $orderby = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : ''; |
||
| 43 | |||
| 44 | $books = []; |
||
| 45 | $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1; |
||
| 46 | $offset = ($page - 1) * config('nntmux.items_per_cover_page'); |
||
| 47 | $rslt = $book->getBookRange($page, $catarray, $offset, config('nntmux.items_per_cover_page'), $orderby, $this->userdata->categoryexclusions); |
||
| 48 | $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_cover_page'), $page, $request->url(), $request->query()); |
||
| 49 | $maxwords = 50; |
||
| 50 | foreach ($results as $result) { |
||
| 51 | if (! empty($result->overview)) { |
||
| 52 | $words = explode(' ', $result->overview); |
||
| 53 | if (\count($words) > $maxwords) { |
||
| 54 | $newwords = \array_slice($words, 0, $maxwords); |
||
| 55 | $result->overview = implode(' ', $newwords).'...'; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | $books[] = $result; |
||
| 59 | } |
||
| 60 | |||
| 61 | $author = ($request->has('author') && ! empty($request->input('author'))) ? stripslashes($request->input('author')) : ''; |
||
| 62 | |||
| 63 | $title = ($request->has('title') && ! empty($request->input('title'))) ? stripslashes($request->input('title')) : ''; |
||
| 64 | |||
| 65 | $browseby_link = '&title='.$title.'&author='.$author; |
||
| 66 | |||
| 67 | if ((int) $category === -1) { |
||
| 68 | $catname = 'All'; |
||
| 69 | } else { |
||
| 70 | $cdata = Category::find($category); |
||
| 71 | if ($cdata !== null) { |
||
| 72 | $catname = $cdata->title; |
||
| 73 | } else { |
||
| 74 | $catname = 'All'; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // Build order by URLs |
||
| 79 | $orderByUrls = []; |
||
| 80 | foreach ($ordering as $ordertype) { |
||
| 81 | $orderByUrls['orderby'.$ordertype] = url('/Books/'.($id ?: 'All').'?t='.$category.$browseby_link.'&ob='.$ordertype.'&offset=0'); |
||
| 82 | } |
||
| 83 | |||
| 84 | $this->viewData = array_merge($this->viewData, [ |
||
| 85 | 'catlist' => $btmp, |
||
| 86 | 'category' => $category, |
||
| 87 | 'categorytitle' => $id, |
||
| 88 | 'catname' => $catname, |
||
| 89 | 'author' => $author, |
||
| 90 | 'title' => $title, |
||
| 91 | 'resultsadd' => $books, |
||
| 92 | 'results' => $results, |
||
| 93 | 'covgroup' => 'books', |
||
| 94 | 'meta_title' => 'Browse Books', |
||
| 95 | 'meta_keywords' => 'browse,nzb,books,description,details', |
||
| 96 | 'meta_description' => 'Browse for Books', |
||
| 97 | ], $orderByUrls); |
||
| 98 | |||
| 99 | return view('books.index', $this->viewData); |
||
| 100 | } |
||
| 102 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.