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 |
||
16 | class Between implements NodeInterface |
||
17 | { |
||
18 | private $leftOperand; |
||
19 | |||
20 | public function getLeftOperand() |
||
24 | |||
25 | /** |
||
26 | * Sets the leftOperand. |
||
27 | * |
||
28 | * @Important |
||
29 | * |
||
30 | * @param NodeInterface|NodeInterface[]|string $leftOperand |
||
31 | */ |
||
32 | public function setLeftOperand($leftOperand) |
||
36 | |||
37 | /** |
||
38 | * @var string|NodeInterface|NodeInterface[] |
||
39 | */ |
||
40 | private $minValueOperand; |
||
41 | |||
42 | /** |
||
43 | * @var string|NodeInterface|NodeInterface[] |
||
44 | */ |
||
45 | private $maxValueOperand; |
||
46 | |||
47 | /** |
||
48 | * @return NodeInterface|NodeInterface[]|string |
||
49 | */ |
||
50 | public function getMinValueOperand() |
||
54 | |||
55 | /** |
||
56 | * @param NodeInterface|NodeInterface[]|string $minValueOperand |
||
57 | */ |
||
58 | public function setMinValueOperand($minValueOperand) |
||
62 | |||
63 | /** |
||
64 | * @return NodeInterface|NodeInterface[]|string |
||
65 | */ |
||
66 | public function getMaxValueOperand() |
||
70 | |||
71 | /** |
||
72 | * @param NodeInterface|NodeInterface[]|string $maxValueOperand |
||
73 | */ |
||
74 | public function setMaxValueOperand($maxValueOperand) |
||
78 | |||
79 | /** |
||
80 | * @var ConditionInterface |
||
81 | */ |
||
82 | protected $minValueCondition; |
||
83 | |||
84 | /** |
||
85 | * Sets the condition. |
||
86 | * |
||
87 | * @Important IfSet |
||
88 | * |
||
89 | * @param ConditionInterface $minValueCondition |
||
90 | */ |
||
91 | public function setMinValueCondition(ConditionInterface $minValueCondition = null) |
||
95 | |||
96 | /** |
||
97 | * @var ConditionInterface |
||
98 | */ |
||
99 | protected $maxValueCondition; |
||
100 | |||
101 | /** |
||
102 | * Sets the condition. |
||
103 | * |
||
104 | * @Important IfSet |
||
105 | * |
||
106 | * @param ConditionInterface $maxValueCondition |
||
107 | */ |
||
108 | public function setMaxValueCondition(ConditionInterface $maxValueCondition = null) |
||
112 | |||
113 | /** |
||
114 | * Returns a Mouf instance descriptor describing this object. |
||
115 | * |
||
116 | * @param MoufManager $moufManager |
||
117 | * |
||
118 | * @return MoufInstanceDescriptor |
||
119 | */ |
||
120 | public function toInstanceDescriptor(MoufManager $moufManager) |
||
143 | |||
144 | /** |
||
145 | * Renders the object as a SQL string. |
||
146 | * |
||
147 | * @param Connection $dbConnection |
||
148 | * @param array $parameters |
||
149 | * @param number $indent |
||
150 | * @param int $conditionsMode |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
||
203 | |||
204 | /** |
||
205 | * Walks the tree of nodes, calling the visitor passed in parameter. |
||
206 | * |
||
207 | * @param VisitorInterface $visitor |
||
208 | */ |
||
209 | public function walk(VisitorInterface $visitor) |
||
241 | } |
||
242 |
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.