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 |
||
| 32 | class RoleDomainMapper |
||
| 33 | { |
||
| 34 | /** @var \eZ\Publish\Core\Repository\Permission\LimitationService */ |
||
| 35 | protected $limitationService; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param \eZ\Publish\Core\Repository\Permission\LimitationService $limitationService |
||
| 39 | */ |
||
| 40 | public function __construct(LimitationService $limitationService) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Maps provided SPI Role value object to API Role value object. |
||
| 47 | * |
||
| 48 | * @param \eZ\Publish\SPI\Persistence\User\Role $role |
||
| 49 | * |
||
| 50 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 51 | */ |
||
| 52 | public function buildDomainRoleObject(SPIRole $role) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Builds a RoleDraft domain object from value object returned by persistence |
||
| 71 | * Decorates Role. |
||
| 72 | * |
||
| 73 | * @param \eZ\Publish\SPI\Persistence\User\Role $spiRole |
||
| 74 | * |
||
| 75 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
| 76 | */ |
||
| 77 | public function buildDomainRoleDraftObject(SPIRole $spiRole) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Maps provided SPI Policy value object to API Policy value object. |
||
| 88 | * |
||
| 89 | * @param \eZ\Publish\SPI\Persistence\User\Policy $spiPolicy |
||
| 90 | * |
||
| 91 | * @return \eZ\Publish\API\Repository\Values\User\Policy|\eZ\Publish\API\Repository\Values\User\PolicyDraft |
||
| 92 | */ |
||
| 93 | public function buildDomainPolicyObject(SPIPolicy $spiPolicy) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Builds the API UserRoleAssignment object from provided SPI RoleAssignment object. |
||
| 122 | * |
||
| 123 | * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment |
||
| 124 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 125 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
| 126 | * |
||
| 127 | * @return \eZ\Publish\API\Repository\Values\User\UserRoleAssignment |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function buildDomainUserRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignment, User $user, APIRole $role) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Builds the API UserGroupRoleAssignment object from provided SPI RoleAssignment object. |
||
| 151 | * |
||
| 152 | * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment |
||
| 153 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 154 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
| 155 | * |
||
| 156 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function buildDomainUserGroupRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignment, UserGroup $userGroup, APIRole $role) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Creates SPI Role create struct from provided API role create struct. |
||
| 180 | */ |
||
| 181 | public function buildPersistenceRoleCreateStruct(APIRoleCreateStruct $roleCreateStruct): SPIRoleCreateStruct |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Creates SPI Role copy struct from provided API role copy struct. |
||
| 195 | */ |
||
| 196 | public function buildPersistenceRoleCopyStruct(APIRoleCopyStruct $roleCopyStruct, int $clonedId, int $status): SPIRoleCopyStruct |
||
| 209 | |||
| 210 | protected function fillRoleStructWithPolicies(APIRoleCreateStruct $struct) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Creates SPI Policy value object from provided module, function and limitations. |
||
| 226 | * |
||
| 227 | * @param string $module |
||
| 228 | * @param string $function |
||
| 229 | * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations |
||
| 230 | * |
||
| 231 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 232 | */ |
||
| 233 | public function buildPersistencePolicyObject($module, $function, array $limitations) |
||
| 251 | } |
||
| 252 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.