| Conditions | 14 |
| Paths | 587 |
| Total Lines | 98 |
| 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 |
||
| 202 | public function getTemplateParameters(GridField $gridField) |
||
| 203 | { |
||
| 204 | if (!$this->checkDataType($gridField->getList())) { |
||
| 205 | return null; |
||
| 206 | } |
||
| 207 | |||
| 208 | $state = $this->getGridPagerState($gridField); |
||
| 209 | |||
| 210 | // Figure out which page and record range we're on |
||
| 211 | $totalRows = $this->totalItems; |
||
| 212 | if (!$totalRows) { |
||
| 213 | return null; |
||
| 214 | } |
||
| 215 | |||
| 216 | $totalPages = 1; |
||
| 217 | $firstShownRecord = 1; |
||
| 218 | $lastShownRecord = $totalRows; |
||
| 219 | if ($itemsPerPage = $this->getItemsPerPage()) { |
||
| 220 | $totalPages = (int)ceil($totalRows / $itemsPerPage); |
||
| 221 | if ($totalPages == 0) { |
||
| 222 | $totalPages = 1; |
||
| 223 | } |
||
| 224 | $firstShownRecord = ($state->currentPage - 1) * $itemsPerPage + 1; |
||
| 225 | if ($firstShownRecord > $totalRows) { |
||
| 226 | $firstShownRecord = $totalRows; |
||
| 227 | } |
||
| 228 | $lastShownRecord = $state->currentPage * $itemsPerPage; |
||
| 229 | if ($lastShownRecord > $totalRows) { |
||
| 230 | $lastShownRecord = $totalRows; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | // If there is only 1 page for all the records in list, we don't need to go further |
||
| 235 | // to sort out those first page, last page, pre and next pages, etc |
||
| 236 | // we are not render those in to the paginator. |
||
| 237 | if ($totalPages === 1) { |
||
| 238 | return new ArrayData(array( |
||
| 239 | 'OnlyOnePage' => true, |
||
| 240 | 'FirstShownRecord' => $firstShownRecord, |
||
| 241 | 'LastShownRecord' => $lastShownRecord, |
||
| 242 | 'NumRecords' => $totalRows, |
||
| 243 | 'NumPages' => $totalPages |
||
| 244 | )); |
||
| 245 | } else { |
||
| 246 | // First page button |
||
| 247 | $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); |
||
| 248 | $firstPage->addExtraClass('btn btn-secondary btn--hide-text btn-sm font-icon-angle-double-left ss-gridfield-firstpage'); |
||
| 249 | if ($state->currentPage == 1) { |
||
| 250 | $firstPage = $firstPage->performDisabledTransformation(); |
||
| 251 | } |
||
| 252 | |||
| 253 | // Previous page button |
||
| 254 | $previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1; |
||
| 255 | $previousPage = new GridField_FormAction( |
||
| 256 | $gridField, |
||
| 257 | 'pagination_prev', |
||
| 258 | 'Previous', |
||
| 259 | 'paginate', |
||
| 260 | $previousPageNum |
||
| 261 | ); |
||
| 262 | $previousPage->addExtraClass('btn btn-secondary btn--hide-text btn-sm font-icon-angle-left ss-gridfield-previouspage'); |
||
| 263 | if ($state->currentPage == 1) { |
||
| 264 | $previousPage = $previousPage->performDisabledTransformation(); |
||
| 265 | } |
||
| 266 | |||
| 267 | // Next page button |
||
| 268 | $nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1; |
||
| 269 | $nextPage = new GridField_FormAction( |
||
| 270 | $gridField, |
||
| 271 | 'pagination_next', |
||
| 272 | 'Next', |
||
| 273 | 'paginate', |
||
| 274 | $nextPageNum |
||
| 275 | ); |
||
| 276 | $nextPage->addExtraClass('btn btn-secondary btn--hide-text btn-sm font-icon-angle-right ss-gridfield-nextpage'); |
||
| 277 | if ($state->currentPage == $totalPages) { |
||
| 278 | $nextPage = $nextPage->performDisabledTransformation(); |
||
| 279 | } |
||
| 280 | |||
| 281 | // Last page button |
||
| 282 | $lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages); |
||
| 283 | $lastPage->addExtraClass('btn btn-secondary btn--hide-text btn-sm font-icon-angle-double-right ss-gridfield-lastpage'); |
||
| 284 | if ($state->currentPage == $totalPages) { |
||
| 285 | $lastPage = $lastPage->performDisabledTransformation(); |
||
| 286 | } |
||
| 287 | |||
| 288 | // Render in template |
||
| 289 | return new ArrayData(array( |
||
| 290 | 'OnlyOnePage' => false, |
||
| 291 | 'FirstPage' => $firstPage, |
||
| 292 | 'PreviousPage' => $previousPage, |
||
| 293 | 'CurrentPageNum' => $state->currentPage, |
||
| 294 | 'NumPages' => $totalPages, |
||
| 295 | 'NextPage' => $nextPage, |
||
| 296 | 'LastPage' => $lastPage, |
||
| 297 | 'FirstShownRecord' => $firstShownRecord, |
||
| 298 | 'LastShownRecord' => $lastShownRecord, |
||
| 299 | 'NumRecords' => $totalRows |
||
| 300 | )); |
||
| 342 |