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 Join extends AbstractSpecification |
||
14 | { |
||
15 | const JOIN = 'join'; |
||
16 | const LEFT_JOIN = 'leftJoin'; |
||
17 | const INNER_JOIN = 'innerJoin'; |
||
18 | |||
19 | protected static $types = [self::JOIN, self::LEFT_JOIN, self::INNER_JOIN]; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $newAlias; |
||
25 | |||
26 | /** |
||
27 | * @var string|null |
||
28 | */ |
||
29 | private $conditionType = null; |
||
30 | |||
31 | /** |
||
32 | * @var string|SpecificationInterface|null |
||
33 | */ |
||
34 | private $condition = null; |
||
35 | |||
36 | /** |
||
37 | * @var string|null |
||
38 | */ |
||
39 | private $indexedBy = null; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $type = self::JOIN; |
||
45 | |||
46 | /** |
||
47 | * @param string $field |
||
48 | * @param string $newAlias |
||
49 | * @param string|null $dqlAlias |
||
50 | * |
||
51 | * @throws InvalidArgumentException |
||
52 | */ |
||
53 | public function __construct($field, $newAlias, $dqlAlias = null) |
||
54 | { |
||
55 | $this->newAlias = $newAlias; |
||
56 | |||
57 | parent::__construct($field, $dqlAlias); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $type |
||
62 | * |
||
63 | * @throws InvalidArgumentException |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | View Code Duplication | public function setType($type) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function modify(QueryBuilder $queryBuilder, $dqlAlias) |
||
103 | |||
104 | /** |
||
105 | * Set the condition type to be used on the join (WITH/ON). |
||
106 | * |
||
107 | * @param string $conditionType |
||
108 | * |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setConditionType($conditionType) |
||
117 | |||
118 | /** |
||
119 | * Set the condition to be used for the join statement. |
||
120 | * |
||
121 | * @param string|SpecificationInterface $condition |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setCondition($condition) |
||
131 | |||
132 | /** |
||
133 | * Set the property which will be used as index for the returned collection. |
||
134 | * |
||
135 | * @param mixed $indexedBy |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function setIndexedBy($indexedBy) |
||
145 | } |
||
146 |
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.