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