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 |
||
6 | View Code Duplication | class AnnotationBlock implements \ArrayAccess |
|
|
|||
7 | { |
||
8 | /** |
||
9 | * AnnotationBlock constructor. |
||
10 | * @param string $name |
||
11 | * @param string $summary |
||
12 | * @param string $description |
||
13 | * @param AnnotationTag[] $children |
||
14 | * @param AnnotationBlock|null $parent |
||
15 | */ |
||
16 | 21 | public function __construct($name='', |
|
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | public $name = ''; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | public $summary = ''; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | public $description=''; |
||
41 | /** |
||
42 | * @var AnnotationTag[] |
||
43 | */ |
||
44 | public $children=[]; |
||
45 | |||
46 | /** |
||
47 | * @var AnnotationTag|null |
||
48 | */ |
||
49 | public $parent; |
||
50 | |||
51 | |||
52 | 21 | public function offsetExists($offset) |
|
56 | |||
57 | |||
58 | 21 | public function offsetGet($offset) |
|
62 | |||
63 | |||
64 | public function offsetSet($offset, $value) |
||
68 | |||
69 | |||
70 | public function offsetUnset($offset) |
||
74 | } |
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.