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 |
||
| 5 | View Code Duplication | class TransformerGenerator extends Generator |
|
|
|
|||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Get stub name. |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | protected $stub = 'transformer/transformer'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Get root namespace. |
||
| 16 | * |
||
| 17 | * @return string |
||
| 18 | */ |
||
| 19 | public function getRootNamespace() |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get generator path config node. |
||
| 26 | * |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | public function getPathConfigNode() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get destination path for generated file. |
||
| 36 | * |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | public function getPath() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get base path of destination file. |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public function getBasePath() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get array replacements. |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getReplacements() |
||
| 74 | } |
||
| 75 |
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.