Conditions | 14 |
Paths | 587 |
Total Lines | 82 |
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 = 1; |
||
183 | $firstShownRecord = 1; |
||
184 | $lastShownRecord = $totalRows; |
||
185 | if ($itemsPerPage = $this->getItemsPerPage()) { |
||
186 | $totalPages = (int)ceil($totalRows / $itemsPerPage); |
||
187 | if ($totalPages == 0) { |
||
188 | $totalPages = 1; |
||
189 | } |
||
190 | $firstShownRecord = ($state->currentPage - 1) * $itemsPerPage + 1; |
||
191 | if ($firstShownRecord > $totalRows) { |
||
192 | $firstShownRecord = $totalRows; |
||
193 | } |
||
194 | $lastShownRecord = $state->currentPage * $itemsPerPage; |
||
195 | if ($lastShownRecord > $totalRows) { |
||
196 | $lastShownRecord = $totalRows; |
||
197 | } |
||
198 | } |
||
199 | |||
200 | // If there is only 1 page for all the records in list, we don't need to go further |
||
201 | // to sort out those first page, last page, pre and next pages, etc |
||
202 | // we are not render those in to the paginator. |
||
203 | if($totalPages === 1){ |
||
204 | return new ArrayData(array( |
||
205 | 'OnlyOnePage' => true, |
||
206 | 'FirstShownRecord' => $firstShownRecord, |
||
207 | 'LastShownRecord' => $lastShownRecord, |
||
208 | 'NumRecords' => $totalRows, |
||
209 | 'NumPages' => $totalPages |
||
210 | )); |
||
211 | } else { |
||
212 | // First page button |
||
213 | $firstPage = new GridField_FormAction($gridField, 'pagination_first', 'First', 'paginate', 1); |
||
214 | $firstPage->addExtraClass('ss-gridfield-firstpage'); |
||
215 | if($state->currentPage == 1) |
||
216 | $firstPage = $firstPage->performDisabledTransformation(); |
||
217 | |||
218 | // Previous page button |
||
219 | $previousPageNum = $state->currentPage <= 1 ? 1 : $state->currentPage - 1; |
||
220 | $previousPage = new GridField_FormAction($gridField, 'pagination_prev', 'Previous', |
||
221 | 'paginate', $previousPageNum); |
||
222 | $previousPage->addExtraClass('ss-gridfield-previouspage'); |
||
223 | if($state->currentPage == 1) |
||
224 | $previousPage = $previousPage->performDisabledTransformation(); |
||
225 | |||
226 | // Next page button |
||
227 | $nextPageNum = $state->currentPage >= $totalPages ? $totalPages : $state->currentPage + 1; |
||
228 | $nextPage = new GridField_FormAction($gridField, 'pagination_next', 'Next', |
||
229 | 'paginate', $nextPageNum); |
||
230 | $nextPage->addExtraClass('ss-gridfield-nextpage'); |
||
231 | if($state->currentPage == $totalPages) |
||
232 | $nextPage = $nextPage->performDisabledTransformation(); |
||
233 | |||
234 | // Last page button |
||
235 | $lastPage = new GridField_FormAction($gridField, 'pagination_last', 'Last', 'paginate', $totalPages); |
||
236 | $lastPage->addExtraClass('ss-gridfield-lastpage'); |
||
237 | if($state->currentPage == $totalPages) |
||
238 | $lastPage = $lastPage->performDisabledTransformation(); |
||
239 | |||
240 | // Render in template |
||
241 | return new ArrayData(array( |
||
242 | 'OnlyOnePage' => false, |
||
243 | 'FirstPage' => $firstPage, |
||
244 | 'PreviousPage' => $previousPage, |
||
245 | 'CurrentPageNum' => $state->currentPage, |
||
246 | 'NumPages' => $totalPages, |
||
247 | 'NextPage' => $nextPage, |
||
248 | 'LastPage' => $lastPage, |
||
249 | 'FirstShownRecord' => $firstShownRecord, |
||
250 | 'LastShownRecord' => $lastShownRecord, |
||
251 | 'NumRecords' => $totalRows |
||
252 | )); |
||
253 | } |
||
254 | } |
||
255 | |||
288 |
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@property
annotation 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.