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 |
||
11 | class VariableExpression extends Expression |
||
12 | { |
||
13 | /** |
||
14 | * @var Expression |
||
15 | */ |
||
16 | private $name; |
||
17 | |||
18 | public function __construct(Expression $name) |
||
22 | |||
23 | public function asEvaluator(IEvaluationContext $context = null) |
||
32 | |||
33 | public function traverse(ExpressionWalker $walker) |
||
37 | |||
38 | /** |
||
39 | * @return Expression |
||
40 | */ |
||
41 | public function getName() |
||
45 | |||
46 | /** |
||
47 | * @param Expression $name |
||
48 | * |
||
49 | * @return self |
||
50 | */ |
||
51 | public function update(Expression $name) |
||
59 | |||
60 | View Code Duplication | protected function compileCode(&$code) |
|
70 | |||
71 | public function serialize() |
||
75 | |||
76 | public function unserialize($serialized) |
||
80 | |||
81 | public function __clone() |
||
85 | } |
||
86 |
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.