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 |
||
21 | class Metadata implements MetadataInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var StreamInterface |
||
25 | */ |
||
26 | protected $stream; |
||
27 | |||
28 | /** |
||
29 | * @var TagReader |
||
30 | */ |
||
31 | protected $tagReader; |
||
32 | |||
33 | /** |
||
34 | * @var TagWriter |
||
35 | */ |
||
36 | protected $tagWriter; |
||
37 | |||
38 | /** |
||
39 | * Create ID3v1 metadata. |
||
40 | * |
||
41 | * @param StreamInterface $stream |
||
42 | */ |
||
43 | public function __construct(StreamInterface $stream) |
||
47 | |||
48 | /** |
||
49 | * Get tag reader. |
||
50 | * |
||
51 | * @return TagReader |
||
52 | */ |
||
53 | public function getTagReader() |
||
61 | |||
62 | /** |
||
63 | * Get tag writer. |
||
64 | * |
||
65 | * @return TagWriter |
||
66 | */ |
||
67 | public function getTagWriter() |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | View Code Duplication | public function exists() |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function strip() |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function read() |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function write(TagInterface $tag) |
||
122 | } |
||
123 |
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.