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 |
||
25 | class PaginatedCollection implements Iterator |
||
26 | { |
||
27 | private $_pager; |
||
28 | private $_pageSize; |
||
29 | private $_currentPage; |
||
30 | private $_index; |
||
31 | private $_totalItems; |
||
32 | private $_items; |
||
33 | |||
34 | /** |
||
35 | * set up the paginated collection |
||
36 | * |
||
37 | * expects an array of an object and method to call on it |
||
38 | * |
||
39 | * @param array $pager |
||
40 | */ |
||
41 | View Code Duplication | public function __construct($pager) |
|
49 | |||
50 | /** |
||
51 | * returns the current item when iterating with foreach |
||
52 | */ |
||
53 | public function current() |
||
57 | |||
58 | public function key() |
||
62 | |||
63 | /** |
||
64 | * advances to the next item in the collection when iterating with foreach |
||
65 | */ |
||
66 | public function next() |
||
70 | |||
71 | /** |
||
72 | * rewinds the collection to the first item when iterating with foreach |
||
73 | */ |
||
74 | View Code Duplication | public function rewind() |
|
82 | |||
83 | /** |
||
84 | * returns whether the current item is valid when iterating with foreach |
||
85 | */ |
||
86 | public function valid() |
||
95 | |||
96 | private function _getNextPage() |
||
110 | } |
||
111 | class_alias('Braintree\PaginatedCollection', 'Braintree_PaginatedCollection'); |
||
112 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.