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 |
||
23 | class TagReader |
||
24 | { |
||
25 | /** |
||
26 | * @var MetadataInterface |
||
27 | */ |
||
28 | protected $metadata; |
||
29 | |||
30 | /** |
||
31 | * @var StreamInterface |
||
32 | */ |
||
33 | protected $stream; |
||
34 | |||
35 | /** |
||
36 | * @var Integer8Reader |
||
37 | */ |
||
38 | protected $integer8Reader; |
||
39 | |||
40 | /** |
||
41 | * Create ID3v1 tag reader. |
||
42 | * |
||
43 | * @param MetadataInterface $metadata |
||
44 | * @param StreamInterface $stream |
||
45 | */ |
||
46 | public function __construct(MetadataInterface $metadata, StreamInterface $stream) |
||
51 | |||
52 | /** |
||
53 | * Get 8-bit integer reader. |
||
54 | * |
||
55 | * @return Integer8Reader |
||
56 | */ |
||
57 | View Code Duplication | public function getInteger8Reader() |
|
65 | |||
66 | /** |
||
67 | * Trim data. |
||
68 | * |
||
69 | * @param string $data The data to trim |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | protected function trimData($data) |
||
77 | |||
78 | /** |
||
79 | * Read ID3v1 tag version. |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | protected function readVersion() |
||
92 | |||
93 | /** |
||
94 | * Read ID3v1 tag. |
||
95 | * |
||
96 | * @return null|TagInterface |
||
97 | */ |
||
98 | public function read() |
||
129 | } |
||
130 |
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.