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 |
||
| 28 | final class ServiceFactory |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var \SpomkyLabs\JoseBundle\Service\AlgorithmManager |
||
| 32 | */ |
||
| 33 | private $algorithm_manager; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \SpomkyLabs\JoseBundle\Service\CompressionManager |
||
| 37 | */ |
||
| 38 | private $compression_manager; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \SpomkyLabs\JoseBundle\Service\CheckerManager |
||
| 42 | */ |
||
| 43 | private $checker_manager; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * ServiceFactory constructor. |
||
| 47 | * |
||
| 48 | * @param \SpomkyLabs\JoseBundle\Service\AlgorithmManager $algorithm_manager |
||
| 49 | * @param \SpomkyLabs\JoseBundle\Service\CompressionManager $compression_manager |
||
| 50 | * @param \SpomkyLabs\JoseBundle\Service\CheckerManager $checker_manager |
||
| 51 | */ |
||
| 52 | public function __construct(AlgorithmManager $algorithm_manager, CompressionManager $compression_manager, CheckerManager $checker_manager) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string[] $selected_key_encryption_algorithms |
||
| 61 | * @param string[] $selected_content_encryption_algorithms |
||
| 62 | * @param string[] $selected_compression_methods |
||
| 63 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 64 | * |
||
| 65 | * @return \Jose\EncrypterInterface |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function createEncrypter(array $selected_key_encryption_algorithms, array $selected_content_encryption_algorithms, array $selected_compression_methods, LoggerInterface $logger = null) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param string[] $selected_key_encryption_algorithms |
||
| 78 | * @param string[] $selected_content_encryption_algorithms |
||
| 79 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 80 | * |
||
| 81 | * @return \Jose\DecrypterInterface |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function createDecrypter(array $selected_key_encryption_algorithms, array $selected_content_encryption_algorithms, array $selected_compression_methods, LoggerInterface $logger = null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param string[] $selected_algorithms |
||
| 94 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 95 | * |
||
| 96 | * @return \Jose\SignerInterface |
||
| 97 | */ |
||
| 98 | public function createSigner(array $selected_algorithms, LoggerInterface $logger = null) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string[] $selected_algorithms |
||
| 107 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 108 | * |
||
| 109 | * @return \Jose\VerifierInterface |
||
| 110 | */ |
||
| 111 | public function createVerifier(array $selected_algorithms, LoggerInterface $logger = null) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string[] $selected_claim_checkers |
||
| 120 | * @param string[] $selected_header_checkers |
||
| 121 | * |
||
| 122 | * @return \Jose\Checker\CheckerManagerInterface |
||
| 123 | */ |
||
| 124 | public function createChecker(array $selected_claim_checkers, array $selected_header_checkers) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param \Jose\Checker\CheckerManagerInterface $checker_manager |
||
| 134 | * @param \Jose\VerifierInterface $verifier |
||
| 135 | * @param \Jose\DecrypterInterface|null $decrypter |
||
| 136 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 137 | * |
||
| 138 | * @return \Jose\JWTLoader |
||
| 139 | */ |
||
| 140 | public function createJWTLoader(CheckerManagerInterface $checker_manager, VerifierInterface $verifier, DecrypterInterface $decrypter = null, LoggerInterface $logger = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param \Jose\SignerInterface $signer |
||
| 152 | * @param \Jose\EncrypterInterface|null $encrypter |
||
| 153 | * |
||
| 154 | * @return \Jose\JWTCreator |
||
| 155 | */ |
||
| 156 | public function createJWTCreator(SignerInterface $signer, EncrypterInterface $encrypter = null) |
||
| 165 | } |
||
| 166 |
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.