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 |
||
10 | class Comparison extends AbstractSpecification |
||
11 | { |
||
12 | const EQ = '='; |
||
13 | const NEQ = '<>'; |
||
14 | const LT = '<'; |
||
15 | const LTE = '<='; |
||
16 | const GT = '>'; |
||
17 | const GTE = '>='; |
||
18 | const LIKE = 'LIKE'; |
||
19 | |||
20 | /** |
||
21 | * @var string[] |
||
22 | */ |
||
23 | protected static $operators = [self::EQ, self::NEQ, self::LT, self::LTE, self::GT, self::GTE, self::LIKE]; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $value; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $operator; |
||
34 | |||
35 | /** |
||
36 | * @param string $operator |
||
37 | * @param string $field |
||
38 | * @param string $value |
||
39 | * @param string|null $dqlAlias |
||
40 | * |
||
41 | * @throws InvalidArgumentException |
||
42 | */ |
||
43 | public function __construct($operator, $field, $value, $dqlAlias = null) |
||
60 | |||
61 | /** |
||
62 | * Return a string expression which can be used as condition (in WHERE-clause). |
||
63 | * |
||
64 | * @param QueryBuilder $queryBuilder |
||
65 | * @param string $dqlAlias |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | View Code Duplication | public function modify(QueryBuilder $queryBuilder, $dqlAlias) |
|
80 | |||
81 | /** |
||
82 | * Return automatically generated parameter name. |
||
83 | * |
||
84 | * @param QueryBuilder $queryBuilder |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function generateParameterName(QueryBuilder $queryBuilder) |
||
92 | } |
||
93 |
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.