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