Conditions | 8 |
Paths | 14 |
Total Lines | 53 |
Code Lines | 36 |
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 |
||
30 | public function transform(Page $page, $filter = null) { |
||
31 | $config = $page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER); |
||
32 | |||
33 | if (!isset($config['variable'])) { |
||
34 | return [$page]; |
||
35 | } |
||
36 | |||
37 | $variable = $config['variable']; |
||
38 | |||
39 | if (!$source = $page->getVariable($variable)) { |
||
40 | throw new VariableNotFoundException("Variable \"{$variable}\" was not set as a data variable for page \"{$page->getId()}\""); |
||
41 | } |
||
42 | |||
43 | $pageId = rtrim($page->getId(), '/'); |
||
44 | $entries = $this->getData($source); |
||
45 | $entriesPerPage = isset($config['entriesPerPage']) ? $config['entriesPerPage'] : 10; |
||
46 | $pageCount = (int) ceil(count($entries) / $entriesPerPage); |
||
47 | |||
48 | $i = 0; |
||
49 | $result = []; |
||
50 | |||
51 | while ($i < $pageCount) { |
||
52 | $pageEntries = array_splice($entries, 0, $entriesPerPage); |
||
53 | $pageIndex = $i + 1; |
||
54 | |||
55 | if ($filter && $pageIndex !== (int) $filter) { |
||
56 | $i += 1; |
||
57 | continue; |
||
58 | } |
||
59 | |||
60 | $url = "{$pageId}/page-{$pageIndex}"; |
||
61 | $pagination = $this->createPagination($pageId, $pageIndex, $pageCount, $entries); |
||
62 | $entriesPage = clone $page; |
||
63 | $entriesPage |
||
64 | ->removeAdapter(AdapterFactory::PAGINATION_ADAPTER) |
||
65 | ->setVariableValue($variable, $pageEntries) |
||
66 | ->setVariableIsParsed($variable) |
||
67 | ->setVariableValue('pagination', $pagination) |
||
68 | ->setVariableIsParsed('pagination') |
||
69 | ->setId($url); |
||
70 | |||
71 | $result[$url] = $entriesPage; |
||
72 | $i += 1; |
||
73 | } |
||
74 | |||
75 | if ($firstPage = reset($result)) { |
||
76 | $mainPage = clone $firstPage; |
||
77 | $mainPage->setId($pageId); |
||
78 | $result[$pageId] = $mainPage; |
||
79 | } |
||
80 | |||
81 | return $result; |
||
82 | } |
||
83 | |||
114 |