Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 3 | class ChildPaginatorControllerExtension extends Extension |
||
|
|
|||
| 4 | { |
||
| 5 | /* |
||
| 6 | Call this from a template to iterate through a number of items (default 10) for the |
||
| 7 | currently selected page. The result is saved as a variable called $this->lastPagedResults for |
||
| 8 | caching purposes when it comes to rendering the pagination |
||
| 9 | |||
| 10 | <code> |
||
| 11 | <% control PagedChildren(NewsItem,8) %> |
||
| 12 | <li> |
||
| 13 | <a href="$Link">$Title</a> |
||
| 14 | <br/> |
||
| 15 | <a href="$Link">$NewsItemImage.SetWidth(200)</a> |
||
| 16 | <br/> |
||
| 17 | <a href="$Link">$NewsItemDate.Nice</a> |
||
| 18 | |||
| 19 | </li> |
||
| 20 | <% end_control %> |
||
| 21 | </code> |
||
| 22 | */ |
||
| 23 | View Code Duplication | public function PagedChildren($klazz, $pageLength = 10, $prime = false, $relationship_key = 'ParentID') |
|
| 38 | |||
| 39 | View Code Duplication | public function AllPagedChildren($pageLength = 10, $sort = 'Sort', $prime = false, $relationship_key = 'ParentID') |
|
| 54 | |||
| 55 | public function PagedDataObjectsByClassName($klazz, $pageLength = 10, $sort = 'ASC') |
||
| 64 | |||
| 65 | public function SetPagedOffset($newoffset) |
||
| 69 | |||
| 70 | public function PagedChildrenAllButFirst() |
||
| 73 | |||
| 74 | /* |
||
| 75 | The current page number. This is only populated after the call to PagedChildren |
||
| 76 | */ |
||
| 77 | public function PageNumber() |
||
| 81 | |||
| 82 | /* |
||
| 83 | A cached copy of the pagination results |
||
| 84 | */ |
||
| 85 | public function LastPagedResults() |
||
| 89 | } |
||
| 90 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.