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 |
||
21 | class DeleteQuery extends AbstractQuery |
||
22 | { |
||
23 | use Executable, OrderBy, WhereConditions; |
||
24 | |||
25 | /** |
||
26 | * @var FromStatement |
||
27 | */ |
||
28 | protected $from; |
||
29 | |||
30 | /** |
||
31 | * @var LimitStatement |
||
32 | */ |
||
33 | protected $limit; |
||
34 | |||
35 | View Code Duplication | public function __construct() |
|
42 | |||
43 | /** |
||
44 | * Sets the table for the query. |
||
45 | * |
||
46 | * @param string $table table name |
||
47 | * |
||
48 | * @return self |
||
49 | */ |
||
50 | public function from($table) |
||
56 | |||
57 | /** |
||
58 | * Sets the limit for the query. |
||
59 | * |
||
60 | * @param int $limit |
||
61 | * @param int $offset |
||
62 | * |
||
63 | * @return self |
||
64 | */ |
||
65 | public function limit($limit, $offset = 0) |
||
71 | |||
72 | /** |
||
73 | * Gets the from statement for the query. |
||
74 | * |
||
75 | * @return FromStatement |
||
76 | */ |
||
77 | public function getFrom() |
||
81 | |||
82 | /** |
||
83 | * Gets the limit statement for the query. |
||
84 | * |
||
85 | * @return LimitStatement |
||
86 | */ |
||
87 | public function getLimit() |
||
91 | |||
92 | /** |
||
93 | * Generates the raw SQL string for the query. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | public function build() |
||
110 | |||
111 | public function __clone() |
||
118 | } |
||
119 |
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.