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 |
||
8 | View Code Duplication | class MethodCall extends Expr |
|
|
|||
9 | { |
||
10 | /** @var Expr Variable holding object */ |
||
11 | public $var; |
||
12 | /** @var string|Expr Method name */ |
||
13 | public $name; |
||
14 | /** @var Arg[] Arguments */ |
||
15 | public $args; |
||
16 | |||
17 | /** |
||
18 | * Constructs a function call node. |
||
19 | * |
||
20 | * @param Expr $var Variable holding object |
||
21 | * @param string|Expr $name Method name |
||
22 | * @param Arg[] $args Arguments |
||
23 | * @param array $attributes Additional attributes |
||
24 | */ |
||
25 | public function __construct(Expr $var, $name, array $args = array(), array $attributes = array()) { |
||
26 | parent::__construct($attributes); |
||
27 | $this->var = $var; |
||
28 | $this->name = $name; |
||
29 | $this->args = $args; |
||
30 | } |
||
31 | |||
32 | public function getSubNodeNames() { |
||
35 | } |
||
36 |
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.