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 |
||
| 13 | View Code Duplication | class Script extends AbstractScript |
|
|
|
|||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $_scriptCode; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param string $scriptCode Script source code |
||
| 22 | * @param array|null $params |
||
| 23 | * @param string|null $lang |
||
| 24 | * @param string|null $documentId Document ID the script action should be performed on (only relevant in update context) |
||
| 25 | */ |
||
| 26 | public function __construct($scriptCode, array $params = null, $lang = null, $documentId = null) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $scriptCode |
||
| 35 | * |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function setScript($scriptCode) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function getScript() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | protected function getScriptTypeArray() |
||
| 60 | } |
||
| 61 |
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.