Conditions | 14 |
Paths | 266 |
Total Lines | 71 |
Code Lines | 50 |
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 |
||
16 | public function transform(Page $page, $filter = null) { |
||
17 | $config = $page->getAdapter(AdapterFactory::PAGINATION_ADAPTER); |
||
18 | |||
19 | if (!isset($config['variable'])) { |
||
20 | return [$page]; |
||
21 | } |
||
22 | |||
23 | $variable = $config['variable']; |
||
24 | |||
25 | if (!$source = $page->getVariable($variable)) { |
||
26 | throw new VariableNotFoundException("Variable \"{$variable}\" was not set as a data variable for page \"{$page->getId()}\""); |
||
27 | } |
||
28 | |||
29 | $pageId = rtrim($page->getId(), '/'); |
||
30 | $entries = $this->getData($source); |
||
31 | $amount = isset($config['amount']) ? $config['amount'] : 10; |
||
32 | $pageCount = (int) ceil(count($entries) / $amount); |
||
33 | |||
34 | $i = 0; |
||
35 | $result = []; |
||
36 | |||
37 | while ($i < $pageCount) { |
||
38 | $pageEntries = array_splice($entries, 0, $amount); |
||
39 | $pageIndex = $i + 1; |
||
40 | |||
41 | if ($filter && $pageIndex !== (int) $filter) { |
||
42 | $i += 1; |
||
43 | continue; |
||
44 | } |
||
45 | |||
46 | $url = "{$pageId}/page-{$pageIndex}"; |
||
47 | |||
48 | $next = count($entries) ? $pageIndex + 1 : null; |
||
49 | $nextUrl = $next ? "{$pageId}/page-{$next}" : null; |
||
50 | $previous = $pageIndex > 1 ? $pageIndex - 1 : null; |
||
51 | $previousUrl = $previous ? "{$pageId}/page-{$previous}" : null; |
||
52 | |||
53 | $entriesPage = clone $page; |
||
54 | $pagination = [ |
||
55 | 'current' => $pageIndex, |
||
56 | 'previous' => $previous ? [ |
||
57 | 'url' => $previousUrl, |
||
58 | 'index' => $previous, |
||
59 | ] : null, |
||
60 | 'next' => $next ? [ |
||
61 | 'url' => $nextUrl, |
||
62 | 'index' => $next, |
||
63 | ] : null, |
||
64 | 'pages' => $pageCount, |
||
65 | ]; |
||
66 | |||
67 | $entriesPage |
||
68 | ->clearAdapter(AdapterFactory::PAGINATION_ADAPTER) |
||
69 | ->setVariable($variable, $pageEntries) |
||
70 | ->setParsedField($variable) |
||
71 | ->setVariable('pagination', $pagination) |
||
72 | ->setParsedField('pagination') |
||
73 | ->setId($url); |
||
74 | |||
75 | $result[$url] = $entriesPage; |
||
76 | $i += 1; |
||
77 | } |
||
78 | |||
79 | if ($firstPage = reset($result)) { |
||
80 | $mainPage = clone $firstPage; |
||
81 | $mainPage->setId($pageId); |
||
82 | $result[$pageId] = $mainPage; |
||
83 | } |
||
84 | |||
85 | return $result; |
||
86 | } |
||
87 | } |
||
88 |