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 |
||
24 | class DataSourceBuilder implements DataSourceBuilderInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var Repository |
||
28 | */ |
||
29 | private $repository; |
||
30 | |||
31 | /** |
||
32 | * @var QueryBuilder |
||
33 | */ |
||
34 | private $queryBuilder; |
||
35 | |||
36 | /** |
||
37 | * @var ExpressionBuilder |
||
38 | */ |
||
39 | private $expressionBuilder; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $limit = 10; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $page = 1; |
||
50 | |||
51 | /** |
||
52 | * @param Repository $repository |
||
53 | * @param mixed[] $options |
||
54 | */ |
||
55 | 20 | View Code Duplication | public function __construct(Repository $repository, array $options = []) |
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 1 | public function select($select) |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 1 | public function innerJoin($join, $alias) |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 1 | public function leftJoin($join, $alias) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 1 | public function andWhere($where) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 1 | public function orWhere($where) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 2 | public function orderBy($sort, $order = 'ASC') |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 1 | public function setLimit($limit) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 1 | public function setPage($page) |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 4 | public function setParameter($parameter, $value, $type = null) |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 2 | public function createPlaceholder($parameter, $value, $type = null) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 1 | public function getProperty($field, $root = null) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 1 | public function getAliases() |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 1 | public function getExpressionBuilder() |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 4 | public function createDataSource(array $options = []) |
|
216 | } |
||
217 |
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.