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 |
||
| 25 | class UserToGroupCoupler extends AbstractDataCoupler |
||
| 26 | { |
||
| 27 | /** @var string */ |
||
| 28 | protected $connectorIdKey = 'id_user_to_user_group'; |
||
| 29 | /** @var string */ |
||
| 30 | protected $connectorDataGroup = 'webhemi_user_to_user_group'; |
||
| 31 | /** @var array */ |
||
| 32 | protected $dependentDataGroups = [ |
||
| 33 | UserEntity::class => [ |
||
| 34 | 'source_key' => 'fk_user', |
||
| 35 | 'connector_key' => 'fk_user_group', |
||
| 36 | 'depending_group' => 'webhemi_user_group', |
||
| 37 | 'depending_id_key' => 'id_user_group', |
||
| 38 | ], |
||
| 39 | UserGroupEntity::class => [ |
||
| 40 | 'source_key' => 'fk_user_group', |
||
| 41 | 'connector_key' => 'fk_user', |
||
| 42 | 'depending_group' => 'webhemi_user', |
||
| 43 | 'depending_id_key' => 'id_user', |
||
| 44 | ] |
||
| 45 | ]; |
||
| 46 | |||
| 47 | use UserEntityTrait; |
||
| 48 | use UserGroupEntityTrait; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Returns a new instance of the required entity. |
||
| 52 | * |
||
| 53 | * @param string $entityClassName |
||
| 54 | * @throws RuntimeException |
||
| 55 | * @return DataEntityInterface |
||
| 56 | */ |
||
| 57 | View Code Duplication | protected function getNewEntityInstance($entityClassName) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Gets all the entities those are depending from the given entity. |
||
| 68 | * |
||
| 69 | * @param DataEntityInterface $entity |
||
| 70 | * @throws RuntimeException |
||
| 71 | * @return array<DataEntityInterface> |
||
| 72 | */ |
||
| 73 | public function getEntityDependencies(DataEntityInterface $entity) |
||
| 93 | } |
||
| 94 |
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.