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 Tag implements TagInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $version; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $title; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $artist; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $album; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $year; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $comment; |
||
52 | |||
53 | /** |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $track; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $genre; |
||
62 | |||
63 | /** |
||
64 | * Create tag object. |
||
65 | * |
||
66 | * @param int $version The version (default is 1: v1.1) |
||
67 | * |
||
68 | * @throws InvalidArgumentException An exception is thrown on invalid version arguments |
||
69 | */ |
||
70 | View Code Duplication | public function __construct($version = Version::VERSION_11) |
|
71 | { |
||
72 | if (!in_array($version, Version::values())) { |
||
73 | throw new InvalidArgumentException('Invalid version.'); |
||
74 | } |
||
75 | |||
76 | $this->version = $version; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function getVersion() |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function getTitle() |
||
94 | |||
95 | /** |
||
96 | * Set title |
||
97 | * |
||
98 | * @param string $title The title |
||
99 | * |
||
100 | * @throws InvalidArgumentException An exception is thrown when the title exceeds 30 characters |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setTitle($title) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getArtist() |
||
122 | |||
123 | /** |
||
124 | * Set artist |
||
125 | * |
||
126 | * @param string $artist The artist |
||
127 | * |
||
128 | * @throws InvalidArgumentException An exception is thrown when the artist exceeds 30 characters |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function setArtist($artist) |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getAlbum() |
||
150 | |||
151 | /** |
||
152 | * Set album |
||
153 | * |
||
154 | * @param string $album The album |
||
155 | * |
||
156 | * @throws InvalidArgumentException An exception is thrown when the album exceeds 30 characters |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setAlbum($album) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function getYear() |
||
178 | |||
179 | /** |
||
180 | * Set year |
||
181 | * |
||
182 | * @param int $year The year |
||
183 | * |
||
184 | * @throws InvalidArgumentException An exception is thrown when the year does not have exactly 4 digits |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function setYear($year) |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function getComment() |
||
206 | |||
207 | /** |
||
208 | * Set comment |
||
209 | * |
||
210 | * @param string $comment The comment |
||
211 | * |
||
212 | * @throws InvalidArgumentException An exception is thrown when the comment exceeds 28 characters |
||
213 | * (ID3 v1.1) or 30 characters (ID3 v1.0) |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function setComment($comment) |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function getTrack() |
||
236 | |||
237 | /** |
||
238 | * Set track |
||
239 | * |
||
240 | * @param int $track The track number |
||
241 | * |
||
242 | * @throws BadMethodCallException An exception is thrown on ID3 v1.0 tag |
||
243 | * @throws InvalidArgumentException An exception is thrown when the year does not have exactly 2 digits |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function setTrack($track) |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function getGenre() |
||
269 | |||
270 | /** |
||
271 | * Set genre |
||
272 | * |
||
273 | * @param int $genre |
||
274 | * |
||
275 | * @throws InvalidArgumentException An exception is thrown on invalid genre arguments |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function setGenre($genre) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function getFrames() |
||
297 | } |
||
298 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: