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 | class SamlEntityService implements ServiceProviderRepository |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var \Surfnet\StepupGateway\GatewayBundle\Entity\DoctrineSamlEntityRepository |
||
| 31 | */ |
||
| 32 | private $samlEntityRepository; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \Surfnet\SamlBundle\Entity\IdentityProvider[] |
||
| 36 | */ |
||
| 37 | private $loadedIdentityProviders; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \Surfnet\SamlBundle\Entity\ServiceProvider[] |
||
| 41 | */ |
||
| 42 | private $loadedServiceProviders; |
||
| 43 | |||
| 44 | public function __construct(SamlEntityRepository $samlEntityRepository) |
||
| 45 | { |
||
| 46 | $this->samlEntityRepository = $samlEntityRepository; |
||
|
|
|||
| 47 | $this->loadedIdentityProviders = []; |
||
| 48 | $this->loadedServiceProviders = []; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $entityId |
||
| 53 | * @return IdentityProvider |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function getIdentityProvider($entityId) |
|
| 56 | { |
||
| 57 | if (!array_key_exists($entityId, $this->loadedIdentityProviders) && !$this->hasIdentityProvider($entityId)) { |
||
| 58 | throw new RuntimeException(sprintf( |
||
| 59 | 'Failed at attempting to load unknown IdentityProvider with EntityId "%s"', |
||
| 60 | $entityId |
||
| 61 | )); |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->loadedIdentityProviders[$entityId]; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $entityId |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | public function hasIdentityProvider($entityId) |
||
| 72 | { |
||
| 73 | $samlEntity = $this->samlEntityRepository->getIdentityProvider($entityId); |
||
| 74 | |||
| 75 | if (!$samlEntity) { |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | $identityProvider = $samlEntity->toIdentityProvider(); |
||
| 80 | $this->loadedIdentityProviders[$identityProvider->getEntityId()] = $identityProvider; |
||
| 81 | |||
| 82 | return true; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $entityId |
||
| 87 | * @return ServiceProvider |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function getServiceProvider($entityId) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $entityId |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function hasServiceProvider($entityId) |
||
| 118 | } |
||
| 119 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.