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 | final class ResponseFactory |
||
28 | { |
||
29 | /** |
||
30 | * @var \Surfnet\SamlBundle\Entity\IdentityProvider |
||
31 | */ |
||
32 | private $hostedIdentityProvider; |
||
33 | |||
34 | /** |
||
35 | * @var \Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler |
||
36 | */ |
||
37 | private $proxyStateHandler; |
||
38 | |||
39 | /** |
||
40 | * @var \DateTime |
||
41 | */ |
||
42 | private $currentTime; |
||
43 | |||
44 | /** |
||
45 | * @var \Surfnet\StepupGateway\GatewayBundle\Saml\AssertionSigningService |
||
46 | */ |
||
47 | private $assertionSigningService; |
||
48 | |||
49 | View Code Duplication | public function __construct( |
|
59 | |||
60 | /** |
||
61 | * @param string $nameId |
||
62 | * @param ServiceProvider $targetServiceProvider |
||
63 | * @param string|null $authnContextClassRef |
||
64 | * @return \SAML2_Response |
||
65 | */ |
||
66 | public function createSecondFactorOnlyResponse( |
||
81 | |||
82 | /** |
||
83 | * @param SAML2_Assertion $newAssertion |
||
84 | * @param ServiceProvider $targetServiceProvider |
||
85 | * @return \SAML2_Response |
||
86 | */ |
||
87 | View Code Duplication | private function createNewAuthnResponse(SAML2_Assertion $newAssertion, ServiceProvider $targetServiceProvider) |
|
88 | { |
||
89 | $response = new \SAML2_Response(); |
||
90 | $response->setAssertions([$newAssertion]); |
||
91 | $response->setIssuer($this->hostedIdentityProvider->getEntityId()); |
||
92 | $response->setIssueInstant($this->getTimestamp()); |
||
93 | $response->setDestination($targetServiceProvider->getAssertionConsumerUrl()); |
||
94 | $response->setInResponseTo($this->proxyStateHandler->getRequestId()); |
||
95 | |||
96 | return $response; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $nameId |
||
101 | * @param ServiceProvider $targetServiceProvider |
||
102 | * @param string $authnContextClassRef |
||
103 | * @return SAML2_Assertion |
||
104 | */ |
||
105 | private function createNewAssertion( |
||
126 | |||
127 | /** |
||
128 | * @param SAML2_Assertion $newAssertion |
||
129 | * @param ServiceProvider $targetServiceProvider |
||
130 | */ |
||
131 | View Code Duplication | private function addSubjectConfirmationFor(SAML2_Assertion $newAssertion, ServiceProvider $targetServiceProvider) |
|
145 | |||
146 | /** |
||
147 | * @param SAML2_Assertion $assertion |
||
148 | * @param SAML2_Assertion $assertion |
||
149 | */ |
||
150 | private function addAuthenticationStatementTo(SAML2_Assertion $assertion, $authnContextClassRef) |
||
156 | |||
157 | /** |
||
158 | * @param string $interval a \DateInterval compatible interval to skew the time with |
||
159 | * @return int |
||
160 | */ |
||
161 | View Code Duplication | private function getTimestamp($interval = null) |
|
171 | } |
||
172 |
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.