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 |
||
9 | class PaginationExtension extends \Extension { |
||
10 | |||
11 | /** |
||
12 | * The default limit. |
||
13 | * Can be overridden in children. |
||
14 | * @var int |
||
15 | */ |
||
16 | protected static $default_limit = 20; |
||
17 | |||
18 | /** |
||
19 | * The default offset. |
||
20 | * Can be overridden in children. |
||
21 | * @var int |
||
22 | */ |
||
23 | protected static $default_offset = 0; |
||
24 | |||
25 | /** |
||
26 | * Returns the offset, either given in request by `offset` or from the default settings in the controller. |
||
27 | * |
||
28 | * @param \SS_HTTPRequest $request |
||
29 | * @return int the offset value |
||
30 | */ |
||
31 | View Code Duplication | public function offset($request) { |
|
39 | |||
40 | /** |
||
41 | * Returns the limit, either given in request by `limit` or from the default settings in the controller. |
||
42 | * |
||
43 | * @param \SS_HTTPRequest $request |
||
44 | * @return int the limit value |
||
45 | */ |
||
46 | View Code Duplication | public function limit($request) { |
|
54 | } |
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.