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 |
||
18 | class Metadata |
||
19 | { |
||
20 | /** |
||
21 | * The stream. |
||
22 | * |
||
23 | * @var Stream |
||
24 | */ |
||
25 | protected $stream; |
||
26 | |||
27 | /** |
||
28 | * The filter. |
||
29 | * |
||
30 | * @var Filter |
||
31 | */ |
||
32 | protected $filter; |
||
33 | |||
34 | /** |
||
35 | * Create ID3v1 metadata object from resource. |
||
36 | * |
||
37 | * @param resource $resource |
||
38 | * |
||
39 | * @throws InvalidArgumentException An exception will be thrown for invalid resource arguments. |
||
40 | * |
||
41 | * @return static |
||
42 | */ |
||
43 | 24 | public static function fromResource($resource) |
|
57 | |||
58 | /** |
||
59 | * Returns whether ID3v1 metadata exists. |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | 22 | View Code Duplication | public function exists() |
73 | |||
74 | /** |
||
75 | * Strip ID3v1 metadata. |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | 4 | public function strip() |
|
90 | |||
91 | /** |
||
92 | * Read ID3v1 tag version. |
||
93 | * |
||
94 | * @return int |
||
95 | */ |
||
96 | 4 | protected function readVersion() |
|
106 | |||
107 | /** |
||
108 | * Read ID3v1 tag. |
||
109 | * |
||
110 | * @return null|Tag |
||
111 | */ |
||
112 | 6 | public function read() |
|
142 | |||
143 | /** |
||
144 | * Write ID3v1 tag. |
||
145 | * |
||
146 | * @param Tag $tag The tag to write. |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | 8 | public function write(Tag $tag) |
|
179 | } |
||
180 |
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.