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 |
||
19 | class Builder |
||
20 | { |
||
21 | /** |
||
22 | * @var Router |
||
23 | */ |
||
24 | private $router; |
||
25 | |||
26 | /** |
||
27 | * The number of pages displayed in the navigation. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | private $max_navigate = Configuration::DEFAULT_LIST_LENGTH; |
||
32 | |||
33 | /** |
||
34 | * Name of URL parameter for page number |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | private $parameter_name = 'page'; |
||
39 | |||
40 | /** |
||
41 | * @param Router $router Router service |
||
42 | * @param int $max_navigate Maximum showing navigation links in pagination |
||
43 | * @param string $parameter_name Name of URL parameter for page number |
||
44 | */ |
||
45 | 13 | public function __construct(Router $router, $max_navigate, $parameter_name) |
|
51 | |||
52 | /** |
||
53 | * @param int $total_pages Total available pages |
||
54 | * @param int $current_page The current page number |
||
55 | * |
||
56 | * @return Configuration |
||
57 | */ |
||
58 | 6 | public function paginate($total_pages = 1, $current_page = 1) |
|
64 | |||
65 | /** |
||
66 | * @param QueryBuilder $query Query for select entities |
||
67 | * @param int $per_page Entities per page |
||
68 | * @param int $current_page The current page number |
||
69 | * |
||
70 | * @return Configuration |
||
71 | */ |
||
72 | 7 | public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1) |
|
91 | |||
92 | /** |
||
93 | * @param Request $request Current HTTP request |
||
94 | * @param int $total_pages Total available pages |
||
95 | * @param string $parameter_name Name of URL parameter for page number |
||
96 | * @param int $reference_type The type of reference (one of the constants in UrlGeneratorInterface) |
||
97 | * |
||
98 | * @return Configuration |
||
99 | */ |
||
100 | 4 | View Code Duplication | public function paginateRequest( |
116 | |||
117 | /** |
||
118 | * @param Request $request Current HTTP request |
||
119 | * @param QueryBuilder $query Query for select entities |
||
120 | * @param int $per_page Entities per page |
||
121 | * @param string $parameter_name Name of URL parameter for page number |
||
122 | * @param int $reference_type The type of reference (one of the constants in UrlGeneratorInterface) |
||
123 | * |
||
124 | * @return Configuration |
||
125 | */ |
||
126 | 4 | View Code Duplication | public function paginateRequestQuery( |
142 | |||
143 | /** |
||
144 | * @param mixed $current_page |
||
145 | * @param int $total_pages |
||
146 | * |
||
147 | * @return int |
||
148 | */ |
||
149 | 11 | private function validateCurrentPage($current_page, $total_pages) |
|
161 | |||
162 | /** |
||
163 | * @param Request $request |
||
164 | * @param Configuration $configuration |
||
165 | * @param string $parameter_name |
||
166 | * @param int $reference_type |
||
167 | * |
||
168 | * @return Configuration |
||
169 | */ |
||
170 | 2 | private function configureFromRequest( |
|
188 | } |
||
189 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.