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 |
||
| 17 | class ModVarExtension extends \Twig_Extension |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var TranslatorInterface |
||
| 21 | */ |
||
| 22 | private $translator; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var VariableApiInterface |
||
| 26 | */ |
||
| 27 | private $variableApi; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * ExtensionsExtension constructor. |
||
| 31 | * @param TranslatorInterface $translator |
||
| 32 | * @param VariableApiInterface $variableApi |
||
| 33 | */ |
||
| 34 | public function __construct( |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Returns a list of functions to add to the existing list. |
||
| 44 | * |
||
| 45 | * @return array An array of functions |
||
| 46 | */ |
||
| 47 | public function getFunctions() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $module |
||
| 57 | * @param $name |
||
| 58 | * @param null $default |
||
| 59 | * @return mixed |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function getModVar($module, $name, $default = null) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param $name |
||
| 72 | * @param null $default |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function getSystemVar($name, $default = null) |
|
| 83 | } |
||
| 84 |
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.