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 |
||
20 | class Metadata implements MetadataInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var StreamInterface |
||
24 | */ |
||
25 | protected $stream; |
||
26 | |||
27 | /** |
||
28 | * @var TagReader |
||
29 | */ |
||
30 | protected $tagReader; |
||
31 | |||
32 | /** |
||
33 | * Create ID3v2 metadata. |
||
34 | * |
||
35 | * @param StreamInterface $stream |
||
36 | */ |
||
37 | public function __construct(StreamInterface $stream) |
||
41 | |||
42 | /** |
||
43 | * Get tag reader. |
||
44 | * |
||
45 | * @return TagReader |
||
46 | */ |
||
47 | public function getTagReader() |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | View Code Duplication | public function exists() |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function strip() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function read() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function write(TagInterface $tag) |
||
97 | } |
||
98 |
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.