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 |
||
11 | class Like implements Filter |
||
12 | { |
||
13 | const CONTAINS = LikePattern::CONTAINS; |
||
14 | |||
15 | const ENDS_WITH = LikePattern::ENDS_WITH; |
||
16 | |||
17 | const STARTS_WITH = LikePattern::STARTS_WITH; |
||
18 | |||
19 | /** |
||
20 | * @var Operand|string |
||
21 | */ |
||
22 | private $field; |
||
23 | |||
24 | /** |
||
25 | * @var LikePattern |
||
26 | */ |
||
27 | private $value; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $dqlAlias; |
||
33 | |||
34 | /** |
||
35 | * @param Operand|string $field |
||
36 | * @param LikePattern|string $value |
||
37 | * @param string $format |
||
38 | * @param string|null $dqlAlias |
||
39 | */ |
||
40 | public function __construct($field, $value, $format = LikePattern::CONTAINS, $dqlAlias = null) |
||
49 | |||
50 | /** |
||
51 | * @param QueryBuilder $qb |
||
52 | * @param string $dqlAlias |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | View Code Duplication | public function getFilter(QueryBuilder $qb, $dqlAlias) |
|
69 | } |
||
70 |
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.