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 LikePattern implements Operand |
||
20 | { |
||
21 | const CONTAINS = '%%%s%%'; |
||
22 | |||
23 | const ENDS_WITH = '%%%s'; |
||
24 | |||
25 | const STARTS_WITH = '%s%%'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $value; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $format; |
||
36 | |||
37 | /** |
||
38 | * @param string $value |
||
39 | * @param string $format |
||
40 | */ |
||
41 | public function __construct($value, $format = self::CONTAINS) |
||
46 | |||
47 | /** |
||
48 | * @param QueryBuilder $qb |
||
49 | * @param string $dqlAlias |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | View Code Duplication | public function transform(QueryBuilder $qb, $dqlAlias) |
|
62 | |||
63 | /** |
||
64 | * @param string $format |
||
65 | * @param string $value |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | private function formatValue($format, $value) |
||
73 | } |
||
74 |
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.