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 |
||
| 23 | class PolicyStorage extends AbstractDataStorage |
||
| 24 | { |
||
| 25 | /** @var string */ |
||
| 26 | protected $dataGroup = 'webhemi_am_policy'; |
||
| 27 | /** @var string */ |
||
| 28 | protected $idKey = 'id_am_policy'; |
||
| 29 | /** @var string */ |
||
| 30 | private $resourceId = 'fk_am_resource'; |
||
| 31 | /** @var string */ |
||
| 32 | private $applicationId = 'fk_application'; |
||
| 33 | /** @var string */ |
||
| 34 | private $name = 'name'; |
||
| 35 | /** @var string */ |
||
| 36 | private $title = 'title'; |
||
| 37 | /** @var string */ |
||
| 38 | private $description = 'description'; |
||
| 39 | /** @var string */ |
||
| 40 | private $isReadOnly = 'is_read_only'; |
||
| 41 | /** @var string */ |
||
| 42 | private $isAllowed = 'is_allowed'; |
||
| 43 | /** @var string */ |
||
| 44 | private $dateCreated = 'date_created'; |
||
| 45 | /** @var string */ |
||
| 46 | private $dateModified = 'date_modified'; |
||
| 47 | |||
| 48 | /** @method bool|array<PolicyEntity> getEntityListFromDataSet(array $dataList) */ |
||
| 49 | use GetEntityListFromDataSetTrait; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Populates an entity with storage data. |
||
| 53 | * |
||
| 54 | * @param DataEntityInterface $entity |
||
| 55 | * @param array $data |
||
| 56 | */ |
||
| 57 | 5 | protected function populateEntity(DataEntityInterface &$entity, array $data) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get data from an entity. |
||
| 74 | * |
||
| 75 | * @param DataEntityInterface $entity |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | 3 | View Code Duplication | protected function getEntityData(DataEntityInterface $entity) |
| 97 | |||
| 98 | /** |
||
| 99 | * Returns a Policy entity identified by (unique) ID. |
||
| 100 | * |
||
| 101 | * @param int $identifier |
||
| 102 | * |
||
| 103 | * @return bool|PolicyEntity |
||
| 104 | */ |
||
| 105 | 1 | public function getPolicyById($identifier) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Returns a Policy entity by name. |
||
| 120 | * |
||
| 121 | * @param string $name |
||
| 122 | * |
||
| 123 | * @return bool|PolicyEntity |
||
| 124 | */ |
||
| 125 | 1 | View Code Duplication | public function getPolicyByName($name) |
| 137 | |||
| 138 | /** |
||
| 139 | * Returns a set of Policy entities identified by Resource ID. |
||
| 140 | * |
||
| 141 | * @param int $resourceId |
||
| 142 | * |
||
| 143 | * @return bool|array<PolicyEntity> |
||
| 144 | */ |
||
| 145 | 1 | public function getPoliciesByResourceId($resourceId) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Returns a set of Policy entities identified by Application ID. |
||
| 154 | * |
||
| 155 | * @param int $applicationId |
||
| 156 | * |
||
| 157 | * @return bool|array<PolicyEntity> |
||
| 158 | */ |
||
| 159 | 1 | public function getPoliciesByApplicationId($applicationId) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Returns a set of Policy entities identified by both Resource and Application IDs. |
||
| 168 | * |
||
| 169 | * @param int $resourceId |
||
| 170 | * @param int $applicationId |
||
| 171 | * |
||
| 172 | * @return bool|array<PolicyEntity> |
||
| 173 | */ |
||
| 174 | 1 | public function getPoliciesByResourceAndApplicationIds($resourceId, $applicationId) |
|
| 185 | } |
||
| 186 |
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.