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 |
||
28 | class Metadata |
||
29 | { |
||
30 | /** |
||
31 | * @var Stream |
||
32 | */ |
||
33 | private $stream; |
||
34 | |||
35 | /** |
||
36 | * @var CompressionFilter |
||
37 | */ |
||
38 | private $compressionFilter; |
||
39 | |||
40 | /** |
||
41 | * @var UnsynchronisationFilter |
||
42 | */ |
||
43 | private $unsynchronisationFilter; |
||
44 | |||
45 | /** |
||
46 | * @var FrameFactory |
||
47 | */ |
||
48 | private $frameFactory; |
||
49 | |||
50 | /** |
||
51 | * Create ID3v2 metadata object from resource. |
||
52 | * |
||
53 | * @param resource $resource |
||
54 | * |
||
55 | * @throws InvalidArgumentException An exception will be thrown for invalid resource arguments. |
||
56 | * |
||
57 | * @return static |
||
58 | */ |
||
59 | 4 | public static function fromResource($resource) |
|
60 | { |
||
61 | 4 | if (!is_resource($resource)) { |
|
62 | throw new InvalidArgumentException('Invalid resource'); |
||
63 | } |
||
64 | |||
65 | 4 | $stream = Stream::fromResource($resource); |
|
66 | 4 | $stream->setByteOrder(ByteOrder::BIG_ENDIAN); |
|
67 | |||
68 | 4 | $metadata = new static(); |
|
69 | 4 | $metadata->stream = $stream; |
|
70 | 4 | $metadata->compressionFilter = new CompressionFilter(); |
|
71 | 4 | $metadata->unsynchronisationFilter = new UnsynchronisationFilter(); |
|
72 | 4 | $metadata->frameFactory = new FrameFactory(); |
|
73 | |||
74 | 4 | return $metadata; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns whether ID3v2 metadata exists. |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | 4 | View Code Duplication | public function exists() |
|
|||
83 | { |
||
84 | 4 | if ($this->stream->getSize() < 10) { |
|
85 | return false; |
||
86 | } |
||
87 | |||
88 | 4 | $this->stream->seek(0); |
|
89 | |||
90 | 4 | return 'ID3' === $this->stream->read(3); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Strip ID3v2 metadata. |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function strip() |
||
104 | |||
105 | /** |
||
106 | * Read ID3v2 version. |
||
107 | * |
||
108 | * @throws RuntimeException An exception is thrown on invalid versions. |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | 4 | protected function readVersion() |
|
132 | |||
133 | /** |
||
134 | * Create readable stream from data. |
||
135 | * |
||
136 | * @param string $data |
||
137 | * |
||
138 | * @return Stream |
||
139 | */ |
||
140 | 4 | protected function createReadableStreamFromData($data) |
|
149 | |||
150 | /** |
||
151 | * Read ID3v2 tag. |
||
152 | * |
||
153 | * @return null|Tag |
||
154 | */ |
||
155 | 4 | public function read() |
|
233 | |||
234 | /** |
||
235 | * Write ID3v2 tag. |
||
236 | * |
||
237 | * @param Tag $tag The tag to write. |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function write(Tag $tag) |
||
261 | } |
||
262 |
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.