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 |
||
| 27 | abstract class AbstractLinkContainer implements LinkContainerInterface |
||
| 28 | { |
||
| 29 | use TranslatorTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var RouterInterface |
||
| 33 | */ |
||
| 34 | protected $router; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var PermissionApi |
||
| 38 | */ |
||
| 39 | protected $permissionApi; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ControllerHelper |
||
| 43 | */ |
||
| 44 | protected $controllerHelper; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var CurrentUserApi |
||
| 48 | */ |
||
| 49 | private $currentUserApi; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * Initialises member vars. |
||
| 54 | * |
||
| 55 | * @param TranslatorInterface $translator Translator service instance |
||
| 56 | * @param Routerinterface $router Router service instance |
||
| 57 | * @param PermissionApi $permissionApi PermissionApi service instance |
||
| 58 | * @param ControllerHelper $controllerHelper ControllerHelper service instance |
||
| 59 | * @param CurrentUserApi $currentUserApi CurrentUserApi service instance |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function __construct(TranslatorInterface $translator, RouterInterface $router, PermissionApi $permissionApi, ControllerHelper $controllerHelper, CurrentUserApi $currentUserApi) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Sets the translator. |
||
| 72 | * |
||
| 73 | * @param TranslatorInterface $translator Translator service instance |
||
| 74 | */ |
||
| 75 | public function setTranslator(/*TranslatorInterface */$translator) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns available header links. |
||
| 82 | * |
||
| 83 | * @param string $type The type to collect links for |
||
| 84 | * |
||
| 85 | * @return array Array of header links |
||
| 86 | */ |
||
| 87 | public function getLinks($type = LinkContainerInterface::TYPE_ADMIN) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the name of the providing bundle. |
||
| 142 | * |
||
| 143 | * @return string The bundle name |
||
| 144 | */ |
||
| 145 | public function getBundleName() |
||
| 149 | } |
||
| 150 |
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.