| Conditions | 8 |
| Paths | 20 |
| Total Lines | 94 |
| Code Lines | 58 |
| Lines | 30 |
| Ratio | 31.91 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 93 | public function paginatorPaginationRender($pagination) |
||
| 94 | { |
||
| 95 | $output = ''; |
||
| 96 | |||
| 97 | $paginationData = $pagination->getPaginationData(); |
||
| 98 | $maxPageRange = isset($paginationData['pageRangeLimit']) |
||
| 99 | ? intval($paginationData['pageRangeLimit']) |
||
| 100 | : 10 |
||
| 101 | ; |
||
| 102 | $route = $paginationData['route']; |
||
| 103 | $routeParameters = $this->app['request']->query->all(); |
||
| 104 | if (isset($paginationData['routeParameters'])) { |
||
| 105 | $routeParameters = array_merge( |
||
| 106 | $routeParameters, |
||
| 107 | $paginationData['routeParameters'] |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | $pageCount = ceil( |
||
| 111 | intval($paginationData['totalCount']) / |
||
| 112 | intval($paginationData['numItemsPerPage']) |
||
| 113 | ); |
||
| 114 | $currentPage = intval($paginationData['current']); |
||
| 115 | |||
| 116 | if ($pageCount > 1) { |
||
| 117 | $pageRange = range(1, $pageCount); |
||
| 118 | |||
| 119 | // Page range by max page numbers |
||
| 120 | $pageRangeTmp = array(); |
||
| 121 | $rangeFrom = $currentPage - ceil($maxPageRange / 2); |
||
| 122 | $rangeTo = $currentPage + ceil($maxPageRange / 2); |
||
| 123 | |||
| 124 | foreach (range($rangeFrom, $rangeTo) as $singleRangePage) { |
||
| 125 | if (in_array($singleRangePage, $pageRange)) { |
||
| 126 | $pageRangeTmp[] = $singleRangePage; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $pageRange = $pageRangeTmp; |
||
| 131 | // Page range by max page numbers /END |
||
| 132 | |||
| 133 | // Prev |
||
| 134 | View Code Duplication | if ($currentPage > 1) { |
|
| 135 | $routeParameters = array_merge( |
||
| 136 | $routeParameters, |
||
| 137 | array( |
||
| 138 | $pagination->getPaginatorOption('pageParameterName') => $currentPage - 1, |
||
| 139 | ) |
||
| 140 | ); |
||
| 141 | |||
| 142 | $prevUrl = $this->app['url_generator']->generate( |
||
| 143 | $route, |
||
| 144 | $routeParameters |
||
| 145 | ); |
||
| 146 | } else { |
||
| 147 | $prevUrl = '#'; |
||
| 148 | } |
||
| 149 | // Prev /END |
||
| 150 | |||
| 151 | // Next |
||
| 152 | View Code Duplication | if ($currentPage < $pageCount) { |
|
| 153 | $routeParameters = array_merge( |
||
| 154 | $routeParameters, |
||
| 155 | array( |
||
| 156 | $pagination->getPaginatorOption('pageParameterName') => $currentPage + 1, |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | |||
| 160 | $nextUrl = $this->app['url_generator']->generate( |
||
| 161 | $route, |
||
| 162 | $routeParameters |
||
| 163 | ); |
||
| 164 | } else { |
||
| 165 | $nextUrl = '#'; |
||
| 166 | } |
||
| 167 | // Next /END |
||
| 168 | |||
| 169 | $output = $this->app['twig']->render( |
||
| 170 | 'twig/paginator/pagination.html.twig', |
||
| 171 | array( |
||
| 172 | 'app' => $this->app, |
||
| 173 | 'prevUrl' => $prevUrl, |
||
| 174 | 'nextUrl' => $nextUrl, |
||
| 175 | 'pageRange' => $pageRange, |
||
| 176 | 'routeParameters' => $routeParameters, |
||
| 177 | 'pagination' => $pagination, |
||
| 178 | 'route' => $route, |
||
| 179 | 'currentPage' => $currentPage, |
||
| 180 | 'pageCount' => $pageCount, |
||
| 181 | ) |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $output; |
||
| 186 | } |
||
| 187 | |||
| 360 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.