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 |
||
| 9 | class UploadImage |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var UUID |
||
| 13 | */ |
||
| 14 | protected $fileId; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var String |
||
| 18 | */ |
||
| 19 | protected $description; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var String |
||
| 23 | */ |
||
| 24 | protected $copyrightHolder; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var MIMEType |
||
| 28 | */ |
||
| 29 | protected $mimeType; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param UUID $fileId |
||
| 33 | * @param MIMEType $mimeType |
||
| 34 | * @param String $description |
||
| 35 | * @param String $copyrightHolder |
||
| 36 | */ |
||
| 37 | View Code Duplication | public function __construct( |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return UUID |
||
| 51 | */ |
||
| 52 | public function getFileId() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return String |
||
| 59 | */ |
||
| 60 | public function getDescription() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return String |
||
| 67 | */ |
||
| 68 | public function getCopyrightHolder() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return MIMEType |
||
| 75 | */ |
||
| 76 | public function getMimeType() |
||
| 80 | } |
||
| 81 |
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.