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 SamlProvider implements AuthenticationProviderInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var \Surfnet\StepupSelfService\SelfServiceBundle\Service\IdentityService |
||
| 37 | */ |
||
| 38 | private $identityService; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary |
||
| 42 | */ |
||
| 43 | private $attributeDictionary; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Surfnet\StepupSelfService\SelfServiceBundle\Locale\PreferredLocaleProvider |
||
| 47 | */ |
||
| 48 | private $preferredLocaleProvider; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \Psr\Log\LoggerInterface |
||
| 52 | */ |
||
| 53 | private $logger; |
||
| 54 | |||
| 55 | public function __construct( |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param SamlToken $token |
||
|
|
|||
| 69 | * @return TokenInterface|void |
||
| 70 | */ |
||
| 71 | public function authenticate(TokenInterface $token) |
||
| 106 | |||
| 107 | public function supports(TokenInterface $token) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param AssertionAdapter $translatedAssertion |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | private function getCommonName(AssertionAdapter $translatedAssertion) |
||
| 117 | { |
||
| 118 | $commonNames = $translatedAssertion->getAttributeValue('commonName'); |
||
| 119 | |||
| 120 | if (empty($commonNames)) { |
||
| 121 | throw new BadCredentialsException( |
||
| 122 | 'No commonName provided' |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (count($commonNames) > 1) { |
||
| 127 | $this->logger->warning('Multiple commonNames provided, picking first one', ['commonNamesCount' => count($commonNames)]); |
||
| 128 | } |
||
| 129 | |||
| 130 | $commonName = $commonNames[0]; |
||
| 131 | |||
| 132 | if (!is_string($commonName)) { |
||
| 133 | $this->logger->warning('Received invalid commonName', ['commonNameCount' => count($commonName)]); |
||
| 134 | throw new BadCredentialsException( |
||
| 135 | 'commonName is not a string' |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $commonName; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param AssertionAdapter $translatedAssertion |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | View Code Duplication | private function getEmail(AssertionAdapter $translatedAssertion) |
|
| 147 | { |
||
| 148 | $emails = $translatedAssertion->getAttributeValue('mail'); |
||
| 149 | |||
| 150 | if (empty($emails)) { |
||
| 151 | throw new BadCredentialsException( |
||
| 152 | 'No schacHomeOrganization provided' |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | if (count($emails) > 1) { |
||
| 157 | $this->logger->warning('Multiple emails provided, picking first one', ['emailsCount' => count($emails)]); |
||
| 158 | } |
||
| 159 | |||
| 160 | $email = $emails[0]; |
||
| 161 | |||
| 162 | if (!is_string($email)) { |
||
| 163 | $this->logger->warning('Received invalid email'); |
||
| 164 | throw new BadCredentialsException( |
||
| 165 | 'email is not a string' |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $email; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param AssertionAdapter $translatedAssertion |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | View Code Duplication | private function getInstitution(AssertionAdapter $translatedAssertion) |
|
| 203 | } |
||
| 204 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.