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 |
||
15 | View Code Duplication | class Header |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $size; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $flags; |
||
26 | |||
27 | /** |
||
28 | * Create ID3v2 header object. |
||
29 | */ |
||
30 | public function __construct() |
||
35 | |||
36 | /** |
||
37 | * Get size in bytes. |
||
38 | * |
||
39 | * @return int |
||
40 | */ |
||
41 | public function getSize() |
||
45 | |||
46 | /** |
||
47 | * Set size in bytes. |
||
48 | * |
||
49 | * @param int $size |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function setSize($size) |
||
58 | |||
59 | /** |
||
60 | * Whether the flag is enabled. |
||
61 | * |
||
62 | * @param int $flag |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function isFlagEnabled($flag) |
||
74 | |||
75 | /** |
||
76 | * Set flags. |
||
77 | * |
||
78 | * @param array $flags |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function setFlags(array $flags) |
||
87 | } |
||
88 |
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.