Conditions | 2 |
Paths | 2 |
Total Lines | 58 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
12 | private function index( |
||
13 | Request $request, |
||
14 | Response $response, |
||
15 | string $context |
||
16 | ): Response { |
||
17 | // Get data using the new getData method |
||
18 | $data = $this->model->getIndexData($context, $request->getQueryParams()); |
||
19 | |||
20 | // render table |
||
21 | $content = $this->tableService->tableHtml( |
||
22 | $data, |
||
23 | $this->adminDirName, |
||
24 | $this->getRoutes(), |
||
|
|||
25 | $context |
||
26 | ); |
||
27 | |||
28 | // set messages |
||
29 | $error = $this->flashManager->getData('errors', []); |
||
30 | $success = $this->flashManager->getData('success', []); |
||
31 | |||
32 | // render messages |
||
33 | $content = $this->tableService->addMessages( |
||
34 | $content, |
||
35 | $error, |
||
36 | $success |
||
37 | ); |
||
38 | |||
39 | // pagination |
||
40 | $paginationSuffix = $context == 'all' ? '' : '/' . $context; |
||
41 | $content .= $this->model->getPagination()->render( |
||
42 | env('BASEPATH', '') . "/{$this->adminDirName}/index" . $paginationSuffix |
||
43 | ); |
||
44 | $totalItems = $this->model->getPagination()->getTotalItems(); |
||
45 | $currentPage = $this->model->getPagination()->getCurrentPage(); |
||
46 | |||
47 | $title = 'x_index_' . $context; |
||
48 | $title_placeholder = $context . ' index of :name'; |
||
49 | |||
50 | $pageTitle = __( |
||
51 | $title, |
||
52 | $title_placeholder, |
||
53 | ['name' => __($this->label)] |
||
54 | ); |
||
55 | |||
56 | $pageNum = __( |
||
57 | 'x_page', |
||
58 | ' index of :name', |
||
59 | ['name' => $currentPage] |
||
60 | ); |
||
61 | |||
62 | return $this->renderResponse( |
||
63 | $response, |
||
64 | $pageTitle, |
||
65 | $content, |
||
66 | 'layout.php', |
||
67 | [ |
||
68 | 'title' => $pageTitle . ' (' . $pageNum . ')', |
||
69 | 'h1' => $pageTitle . ' (' . count($data) . '/' . $totalItems . ')', |
||
70 | ] |
||
74 |