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 |
||
| 33 | class RoleDomainMapper |
||
| 34 | { |
||
| 35 | /** @var \eZ\Publish\Core\Repository\Helper\LimitationService */ |
||
| 36 | protected $limitationService; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param \eZ\Publish\Core\Repository\Helper\LimitationService $limitationService |
||
| 40 | */ |
||
| 41 | public function __construct(LimitationService $limitationService) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Maps provided SPI Role value object to API Role value object. |
||
| 48 | * |
||
| 49 | * @param \eZ\Publish\SPI\Persistence\User\Role $role |
||
| 50 | * |
||
| 51 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 52 | */ |
||
| 53 | public function buildDomainRoleObject(SPIRole $role) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Builds a RoleDraft domain object from value object returned by persistence |
||
| 72 | * Decorates Role. |
||
| 73 | * |
||
| 74 | * @param \eZ\Publish\SPI\Persistence\User\Role $spiRole |
||
| 75 | * |
||
| 76 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
| 77 | */ |
||
| 78 | public function buildDomainRoleDraftObject(SPIRole $spiRole) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Maps provided SPI Policy value object to API Policy value object. |
||
| 89 | * |
||
| 90 | * @param \eZ\Publish\SPI\Persistence\User\Policy $spiPolicy |
||
| 91 | * |
||
| 92 | * @return \eZ\Publish\API\Repository\Values\User\Policy|\eZ\Publish\API\Repository\Values\User\PolicyDraft |
||
| 93 | */ |
||
| 94 | public function buildDomainPolicyObject(SPIPolicy $spiPolicy) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Builds the API UserRoleAssignment object from provided SPI RoleAssignment object. |
||
| 123 | * |
||
| 124 | * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment |
||
| 125 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 126 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
| 127 | * |
||
| 128 | * @return \eZ\Publish\API\Repository\Values\User\UserRoleAssignment |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function buildDomainUserRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignment, User $user, APIRole $role) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Builds the API UserGroupRoleAssignment object from provided SPI RoleAssignment object. |
||
| 152 | * |
||
| 153 | * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment |
||
| 154 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 155 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
| 156 | * |
||
| 157 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function buildDomainUserGroupRoleAssignmentObject(SPIRoleAssignment $spiRoleAssignment, UserGroup $userGroup, APIRole $role) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Creates SPI Role create struct from provided API role create struct. |
||
| 181 | * |
||
| 182 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
||
| 183 | * |
||
| 184 | * @return \eZ\Publish\SPI\Persistence\User\RoleCreateStruct |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function buildPersistenceRoleCreateStruct(APIRoleCreateStruct $roleCreateStruct) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Creates SPI Role copy struct from provided API role copy struct. |
||
| 207 | * |
||
| 208 | * @param \eZ\Publish\API\Repository\Values\User\RoleCopyStruct $roleCopyStruct |
||
| 209 | * @param mixed $clonedRoleId |
||
| 210 | * |
||
| 211 | * @return \eZ\Publish\SPI\Persistence\User\RoleCopyStruct |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function buildPersistenceRoleCopyStruct(APIRoleCopyStruct $roleCopyStruct, $clonedId) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Creates SPI Policy value object from provided module, function and limitations. |
||
| 235 | * |
||
| 236 | * @param string $module |
||
| 237 | * @param string $function |
||
| 238 | * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations |
||
| 239 | * |
||
| 240 | * @return \eZ\Publish\SPI\Persistence\User\Policy |
||
| 241 | */ |
||
| 242 | public function buildPersistencePolicyObject($module, $function, array $limitations) |
||
| 260 | } |
||
| 261 |
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.