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 |
||
13 | class GroupBy extends AbstractSpecification |
||
14 | { |
||
15 | const GROUP_BY = 'groupBy'; |
||
16 | const ADD_GROUP_BY = 'addGroupBy'; |
||
17 | |||
18 | /** @var string[] */ |
||
19 | protected static $types = [self::GROUP_BY, self::ADD_GROUP_BY]; |
||
20 | |||
21 | /** @var string */ |
||
22 | protected $type; |
||
23 | |||
24 | /** |
||
25 | * Constructor. |
||
26 | * |
||
27 | * @param string $field |
||
28 | * @param string $type |
||
29 | * @param string|null $dqlAlias |
||
30 | */ |
||
31 | public function __construct($field, $type = self::ADD_GROUP_BY, $dqlAlias = null) |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function modify(QueryBuilder $queryBuilder, $dqlAlias) |
||
49 | |||
50 | /** |
||
51 | * @param string $type |
||
52 | * |
||
53 | * @throws InvalidArgumentException |
||
54 | */ |
||
55 | View Code Duplication | public function setType($type) |
|
67 | } |
||
68 |
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.