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( |
||
56 | IdentityService $identityService, |
||
57 | AttributeDictionary $attributeDictionary, |
||
58 | PreferredLocaleProvider $preferredLocaleProvider, |
||
59 | LoggerInterface $logger |
||
60 | ) { |
||
61 | $this->identityService = $identityService; |
||
62 | $this->attributeDictionary = $attributeDictionary; |
||
63 | $this->preferredLocaleProvider = $preferredLocaleProvider; |
||
64 | $this->logger = $logger; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param SamlToken $token |
||
|
|||
69 | * @return TokenInterface|void |
||
70 | */ |
||
71 | public function authenticate(TokenInterface $token) |
||
72 | { |
||
73 | $translatedAssertion = $this->attributeDictionary->translate($token->assertion); |
||
74 | |||
75 | $nameId = $translatedAssertion->getNameID(); |
||
76 | $institution = $this->getInstitution($translatedAssertion); |
||
77 | $email = $this->getEmail($translatedAssertion); |
||
78 | $commonName = $this->getCommonName($translatedAssertion); |
||
79 | |||
80 | |||
81 | $identity = $this->identityService->findByNameIdAndInstitution($nameId, $institution); |
||
82 | |||
83 | if ($identity === null) { |
||
84 | $identity = new Identity(); |
||
85 | $identity->id = Uuid::generate(); |
||
86 | $identity->nameId = $nameId; |
||
87 | $identity->institution = $institution; |
||
88 | $identity->email = $email; |
||
89 | $identity->commonName = $commonName; |
||
90 | $identity->preferredLocale = $this->preferredLocaleProvider->providePreferredLocale(); |
||
91 | |||
92 | $this->identityService->createIdentity($identity); |
||
93 | } elseif ($identity->email !== $email || $identity->commonName !== $commonName) { |
||
94 | $identity->email = $email; |
||
95 | $identity->commonName = $commonName; |
||
96 | |||
97 | $this->identityService->updateIdentity($identity); |
||
98 | } |
||
99 | |||
100 | $authenticatedToken = new SamlToken(['ROLE_USER']); |
||
101 | |||
102 | $authenticatedToken->setUser($identity); |
||
103 | |||
104 | return $authenticatedToken; |
||
105 | } |
||
106 | |||
107 | public function supports(TokenInterface $token) |
||
111 | |||
112 | /** |
||
113 | * @param AssertionAdapter $translatedAssertion |
||
114 | * @return string |
||
115 | */ |
||
116 | View Code Duplication | private function getInstitution(AssertionAdapter $translatedAssertion) |
|
143 | |||
144 | /** |
||
145 | * @param AssertionAdapter $translatedAssertion |
||
146 | * @return string |
||
147 | */ |
||
148 | View Code Duplication | private function getEmail(AssertionAdapter $translatedAssertion) |
|
175 | |||
176 | /** |
||
177 | * @param AssertionAdapter $translatedAssertion |
||
178 | * @return string |
||
179 | */ |
||
180 | View Code Duplication | private function getCommonName(AssertionAdapter $translatedAssertion) |
|
207 | } |
||
208 |
This check looks for
@param
annotations 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.