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 |
||
25 | View Code Duplication | class UploadFileCommand |
|
|
|||
26 | { |
||
27 | /** |
||
28 | * The file id. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $id; |
||
33 | |||
34 | /** |
||
35 | * The file name. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $name; |
||
40 | |||
41 | /** |
||
42 | * The real content of file. |
||
43 | * |
||
44 | * @var mixed |
||
45 | */ |
||
46 | private $uploadedFile; |
||
47 | |||
48 | /** |
||
49 | * The file mime type. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $mimeType; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param string $aName The file name |
||
59 | * @param mixed $anUploadedFile The real content of file |
||
60 | * @param string $aMimeType The file mime type |
||
61 | * @param string|null $anId The file id |
||
62 | * |
||
63 | * @throws FileNameInvalidException when the mime type given is null |
||
64 | * @throws FileMimeTypeDoesNotSupportException when the name given is null |
||
65 | */ |
||
66 | public function __construct($aName, $anUploadedFile, $aMimeType, $anId = null) |
||
82 | |||
83 | /** |
||
84 | * Gets the file id. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function id() |
||
92 | |||
93 | /** |
||
94 | * Gets the file name. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function name() |
||
102 | |||
103 | /** |
||
104 | * Gets the mime type. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function mimeType() |
||
112 | |||
113 | /** |
||
114 | * Gets the real content of file. |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function uploadedFile() |
||
122 | } |
||
123 |
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.