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 |
||
15 | class AudioProperty extends FileProperty |
||
16 | { |
||
17 | /** |
||
18 | * Minimum audio length, in seconds. |
||
19 | * |
||
20 | * @var integer |
||
21 | */ |
||
22 | private $minLength = 0; |
||
23 | |||
24 | /** |
||
25 | * Maximum audio length, in seconds. |
||
26 | * |
||
27 | * @var integer |
||
28 | */ |
||
29 | private $maxLength = 0; |
||
30 | |||
31 | /** |
||
32 | * @return string |
||
33 | */ |
||
34 | public function type() |
||
38 | |||
39 | /** |
||
40 | * @param integer $minLength The minimum length allowed, in seconds. |
||
41 | * @throws InvalidArgumentException If the length is not an integer. |
||
42 | * @return AudioProperty Chainable |
||
43 | */ |
||
44 | public function setMinLength($minLength) |
||
54 | |||
55 | /** |
||
56 | * @return integer |
||
57 | */ |
||
58 | public function getMinLength() |
||
62 | |||
63 | /** |
||
64 | * @param integer $maxLength The maximum length allowed, in seconds. |
||
65 | * @throws InvalidArgumentException If the length is not an integer. |
||
66 | * @return AudioProperty Chainable |
||
67 | */ |
||
68 | public function setMaxLength($maxLength) |
||
78 | |||
79 | /** |
||
80 | * @return integer |
||
81 | */ |
||
82 | public function getMaxLength() |
||
86 | |||
87 | /** |
||
88 | * Retrieves the default list of acceptable MIME types for uploaded files. |
||
89 | * |
||
90 | * This method should be overriden. |
||
91 | * |
||
92 | * @return string[] |
||
93 | */ |
||
94 | View Code Duplication | public function getDefaultAcceptedMimetypes() |
|
107 | |||
108 | /** |
||
109 | * Resolve the file extension from the given MIME type. |
||
110 | * |
||
111 | * @param string $type The MIME type to resolve. |
||
112 | * @return string|null The extension based on the MIME type. |
||
113 | */ |
||
114 | View Code Duplication | protected function resolveExtensionFromMimeType($type) |
|
136 | } |
||
137 |
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.