| Conditions | 13 |
| Paths | 522 |
| Total Lines | 74 |
| Code Lines | 53 |
| 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 |
||
| 173 | public function getTemplateParameters(GridField $gridField) { |
||
| 174 | if(!$this->checkDataType($gridField->getList())) return null; |
||
| 175 | |||
| 176 | $state = $this->getGridPagerState($gridField); |
||
| 177 | |||
| 178 | // Figure out which page and record range we're on |
||
| 179 | $totalRows = $this->totalItems; |
||
| 180 | if(!$totalRows) return null; |
||
| 181 | |||
| 182 | $totalPages = (int)ceil($totalRows/$this->itemsPerPage); |
||
| 183 | if($totalPages == 0) |
||
| 184 | $totalPages = 1; |
||
| 185 | $firstShownRecord = ($state->currentPage - 1) * $this->itemsPerPage + 1; |
||
| 186 | if($firstShownRecord > $totalRows) |
||
| 187 | $firstShownRecord = $totalRows; |
||
| 188 | $lastShownRecord = $state->currentPage * $this->itemsPerPage; |
||
| 189 | if($lastShownRecord > $totalRows) |
||
| 190 | $lastShownRecord = $totalRows; |
||
| 191 | |||
| 192 | // If there is only 1 page for all the records in list, we don't need to go further |
||
| 193 | // to sort out those first page, last page, pre and next pages, etc |
||
| 194 | // we are not render those in to the paginator. |
||
| 195 | if($totalPages === 1){ |
||
| 196 | return new ArrayData(array( |
||
| 197 | 'OnlyOnePage' => true, |
||
| 198 | 'FirstShownRecord' => $firstShownRecord, |
||
| 199 | 'LastShownRecord' => $lastShownRecord, |
||
| 200 | 'NumRecords' => $totalRows, |
||
| 201 | 'NumPages' => $totalPages |
||
| 202 | )); |
||
| 203 | } else { |
||
| 204 | // First page button |
||
| 205 | $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); |
||
| 206 | $firstPage->addExtraClass('ss-gridfield-firstpage'); |
||
| 207 | if($state->currentPage == 1) |
||
| 208 | $firstPage = $firstPage->performDisabledTransformation(); |
||
| 209 | |||
| 210 | // Previous page button |
||
| 211 | $previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1; |
||
| 212 | $previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous', |
||
| 213 | 'paginate', $previousPageNum); |
||
| 214 | $previousPage->addExtraClass('ss-gridfield-previouspage'); |
||
| 215 | if($state->currentPage == 1) |
||
| 216 | $previousPage = $previousPage->performDisabledTransformation(); |
||
| 217 | |||
| 218 | // Next page button |
||
| 219 | $nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1; |
||
| 220 | $nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next', |
||
| 221 | 'paginate', $nextPageNum); |
||
| 222 | $nextPage->addExtraClass('ss-gridfield-nextpage'); |
||
| 223 | if($state->currentPage == $totalPages) |
||
| 224 | $nextPage = $nextPage->performDisabledTransformation(); |
||
| 225 | |||
| 226 | // Last page button |
||
| 227 | $lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages); |
||
| 228 | $lastPage->addExtraClass('ss-gridfield-lastpage'); |
||
| 229 | if($state->currentPage == $totalPages) |
||
| 230 | $lastPage = $lastPage->performDisabledTransformation(); |
||
| 231 | |||
| 232 | // Render in template |
||
| 233 | return new ArrayData(array( |
||
| 234 | 'OnlyOnePage' => false, |
||
| 235 | 'FirstPage' => $firstPage, |
||
| 236 | 'PreviousPage' => $previousPage, |
||
| 237 | 'CurrentPageNum' => $state->currentPage, |
||
| 238 | 'NumPages' => $totalPages, |
||
| 239 | 'NextPage' => $nextPage, |
||
| 240 | 'LastPage' => $lastPage, |
||
| 241 | 'FirstShownRecord' => $firstShownRecord, |
||
| 242 | 'LastShownRecord' => $lastShownRecord, |
||
| 243 | 'NumRecords' => $totalRows |
||
| 244 | )); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 280 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.