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 |
||
| 8 | View Code Duplication | class Image extends Media |
|
|
|
|||
| 9 | { |
||
| 10 | const TYPE = 'image'; |
||
| 11 | const TYPE_PATH = 'images/'; |
||
| 12 | |||
| 13 | public $name = 'Image'; |
||
| 14 | public $description = 'Direct link to an image on an internet website. Hosted image is usually more appropriate.'; |
||
| 15 | public $html = '<img src="%data%" class="image" />'; |
||
| 16 | public $css = '%field% { text-align: center; vertical-align: middle; } %field% img { max-height: 100%; max-width: 100%; }'; |
||
| 17 | public $input = 'url'; |
||
| 18 | public $output = 'url'; |
||
| 19 | public $usable = true; |
||
| 20 | public $preview = '@web/images/image.preview.jpg'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | */ |
||
| 25 | public static function validateFile($realFilepath) |
||
| 35 | } |
||
| 36 |
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.