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 |
||
36 | class ProxyResponseService |
||
37 | { |
||
38 | /** |
||
39 | * @var \Surfnet\SamlBundle\Entity\IdentityProvider |
||
40 | */ |
||
41 | private $hostedIdentityProvider; |
||
42 | |||
43 | /** |
||
44 | * @var \Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler |
||
45 | */ |
||
46 | private $proxyStateHandler; |
||
47 | |||
48 | /** |
||
49 | * @var \Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary |
||
50 | */ |
||
51 | private $attributeDictionary; |
||
52 | |||
53 | /** |
||
54 | * @var \Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition |
||
55 | */ |
||
56 | private $eptiAttribute; |
||
57 | |||
58 | /** |
||
59 | * @var \DateTime |
||
60 | */ |
||
61 | private $currentTime; |
||
62 | |||
63 | /** |
||
64 | * @var \Surfnet\StepupGateway\GatewayBundle\Saml\AssertionSigningService |
||
65 | */ |
||
66 | private $assertionSigningService; |
||
67 | |||
68 | /** |
||
69 | * @var \Surfnet\StepupBundle\Value\Loa |
||
70 | */ |
||
71 | private $intrinsicLoa; |
||
72 | |||
73 | public function __construct( |
||
74 | IdentityProvider $hostedIdentityProvider, |
||
75 | ProxyStateHandler $proxyStateHandler, |
||
76 | AssertionSigningService $assertionSigningService, |
||
77 | AttributeDictionary $attributeDictionary, |
||
78 | AttributeDefinition $eptiAttribute, |
||
79 | Loa $intrinsicLoa |
||
80 | ) { |
||
81 | $this->hostedIdentityProvider = $hostedIdentityProvider; |
||
82 | $this->proxyStateHandler = $proxyStateHandler; |
||
83 | $this->assertionSigningService = $assertionSigningService; |
||
84 | $this->attributeDictionary = $attributeDictionary; |
||
85 | $this->eptiAttribute = $eptiAttribute; |
||
86 | $this->intrinsicLoa = $intrinsicLoa; |
||
87 | $this->currentTime = new \DateTime('now', new \DateTimeZone('UTC')); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param SAML2_Assertion $assertion |
||
92 | * @param ServiceProvider $targetServiceProvider |
||
93 | * @param string|null $loa |
||
94 | * @return \SAML2_Response |
||
95 | */ |
||
96 | public function createProxyResponse(SAML2_Assertion $assertion, ServiceProvider $targetServiceProvider, $loa = null) |
||
123 | |||
124 | /** |
||
125 | * @param SAML2_Assertion $newAssertion |
||
126 | * @param ServiceProvider $targetServiceProvider |
||
127 | */ |
||
128 | View Code Duplication | private function addSubjectConfirmationFor(SAML2_Assertion $newAssertion, ServiceProvider $targetServiceProvider) |
|
142 | |||
143 | /** |
||
144 | * @param SAML2_Assertion $newAssertion |
||
145 | * @param SAML2_Assertion $assertion |
||
146 | */ |
||
147 | private function addAuthenticationStatementTo(SAML2_Assertion $newAssertion, SAML2_Assertion $assertion) |
||
160 | |||
161 | /** |
||
162 | * @param SAML2_Assertion $newAssertion |
||
163 | * @param ServiceProvider $targetServiceProvider |
||
164 | * @return \SAML2_Response |
||
165 | */ |
||
166 | View Code Duplication | private function createNewAuthnResponse(SAML2_Assertion $newAssertion, ServiceProvider $targetServiceProvider) |
|
167 | { |
||
168 | $response = new \SAML2_Response(); |
||
169 | $response->setAssertions([$newAssertion]); |
||
170 | $response->setIssuer($this->hostedIdentityProvider->getEntityId()); |
||
171 | $response->setIssueInstant($this->getTimestamp()); |
||
172 | $response->setDestination($targetServiceProvider->getAssertionConsumerUrl()); |
||
173 | $response->setInResponseTo($this->proxyStateHandler->getRequestId()); |
||
174 | |||
175 | return $response; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $interval a \DateInterval compatible interval to skew the time with |
||
180 | * @return int |
||
181 | */ |
||
182 | View Code Duplication | private function getTimestamp($interval = null) |
|
183 | { |
||
184 | $time = clone $this->currentTime; |
||
185 | |||
186 | if ($interval) { |
||
187 | $time->add(new \DateInterval($interval)); |
||
188 | } |
||
189 | |||
190 | return $time->getTimestamp(); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param AssertionAdapter $translatedAssertion |
||
195 | * @return array |
||
196 | */ |
||
197 | private function parseEptiNameId(AssertionAdapter $translatedAssertion) |
||
214 | } |
||
215 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.