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 |
||
19 | class Header implements HeaderInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $version; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $revision; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $flags; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $size; |
||
40 | |||
41 | /** |
||
42 | * Create header object. |
||
43 | * |
||
44 | * @param int $version The version (default is 3: v2.3) |
||
45 | * |
||
46 | * @throws InvalidArgumentException An exception is thrown on invalid version arguments |
||
47 | */ |
||
48 | View Code Duplication | public function __construct($version = Version::VERSION_23) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function getVersion() |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function getRevision() |
||
72 | |||
73 | /** |
||
74 | * Set revision |
||
75 | * |
||
76 | * @param int $revision |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function setRevision($revision) |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function isFlagEnabled($flag) |
||
97 | |||
98 | /** |
||
99 | * Set flags |
||
100 | * |
||
101 | * @param array $flags |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function setFlags(array $flags) |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function getSize() |
||
118 | |||
119 | /** |
||
120 | * Set size in bytes |
||
121 | * |
||
122 | * @param int $size |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function setSize($size) |
||
131 | } |
||
132 |
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.