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 |
||
| 3 | namespace SAML2\Assertion; |
||
| 4 | |||
| 5 | use Psr\Log\LoggerInterface; |
||
| 6 | use SAML2\Assertion\Exception\NotDecryptedException; |
||
| 7 | use SAML2\Certificate\PrivateKeyLoader; |
||
| 8 | use SAML2\Configuration\IdentityProvider; |
||
| 9 | use SAML2\Configuration\ServiceProvider; |
||
| 10 | use SAML2\EncryptedAssertion; |
||
| 11 | |||
| 12 | class Decrypter |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var \SAML2\Configuration\IdentityProvider |
||
| 16 | */ |
||
| 17 | private $identityProvider; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var \SAML2\Configuration\ServiceProvider |
||
| 21 | */ |
||
| 22 | private $serviceProvider; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \SAML2\Certificate\PrivateKeyLoader |
||
| 26 | */ |
||
| 27 | private $privateKeyLoader; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Psr\Log\LoggerInterface |
||
| 31 | */ |
||
| 32 | private $logger; |
||
| 33 | |||
| 34 | public function __construct( |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Allows for checking whether either the SP or the IdP requires assertion encryption |
||
| 48 | */ |
||
| 49 | public function isEncryptionRequired() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param \SAML2\EncryptedAssertion $assertion |
||
| 57 | * |
||
| 58 | * @return \SAML2\Assertion |
||
| 59 | */ |
||
| 60 | public function decrypt(EncryptedAssertion $assertion) |
||
| 61 | { |
||
| 62 | $decryptionKeys = $this->privateKeyLoader->loadDecryptionKeys($this->identityProvider, $this->serviceProvider); |
||
| 63 | $blacklistedKeys = $this->identityProvider->getBlacklistedAlgorithms(); |
||
| 64 | if (is_null($blacklistedKeys)) { |
||
| 65 | $blacklistedKeys = $this->serviceProvider->getBlacklistedAlgorithms(); |
||
| 66 | } |
||
| 67 | |||
| 68 | // reflects the simplesamlphp behaviour for BC, see |
||
| 69 | // https://github.com/simplesamlphp/simplesamlphp/blob/3d735912342767d391297cc5e13272a76730aca0/modules/saml/lib/Message.php#L369 |
||
| 70 | View Code Duplication | foreach ($decryptionKeys as $index => $key) { |
|
|
|
|||
| 71 | try { |
||
| 72 | $decryptedAssertion = $assertion->getAssertion($key, $blacklistedKeys); |
||
| 73 | $this->logger->debug(sprintf('Decrypted Assertion with key "#%d"', $index)); |
||
| 74 | |||
| 75 | return $decryptedAssertion; |
||
| 76 | } catch (\Exception $e) { |
||
| 77 | $this->logger->debug(sprintf( |
||
| 78 | 'Could not decrypt assertion with key "#%d", "%s" thrown: "%s"', |
||
| 79 | $index, |
||
| 80 | get_class($e), |
||
| 81 | $e->getMessage() |
||
| 82 | )); |
||
| 83 | } |
||
| 84 | } |
||
| 92 |
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.