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 |
||
20 | class AesCbcHmacEncryption implements JweEncryption |
||
21 | { |
||
22 | /** @var JwsAlgorithm */ |
||
23 | private $hashAlgorithm; |
||
24 | |||
25 | /** @var int */ |
||
26 | private $keySize; |
||
27 | |||
28 | /** @var RandomGenerator */ |
||
29 | private $randomGenerator; |
||
30 | |||
31 | /** |
||
32 | * @param int $keySize |
||
33 | * @param JwsAlgorithm $hashAlgorithm |
||
34 | * @param RandomGenerator $randomGenerator |
||
35 | */ |
||
36 | public function __construct($keySize, JwsAlgorithm $hashAlgorithm, RandomGenerator $randomGenerator) |
||
42 | |||
43 | /** |
||
44 | * @return int |
||
45 | */ |
||
46 | public function getKeySize() |
||
50 | |||
51 | /** |
||
52 | * @param string $aad |
||
53 | * @param string $plainText |
||
54 | * @param string|resource $cek |
||
55 | * |
||
56 | * @return array [iv, cipherText, authTag] |
||
57 | */ |
||
58 | public function encrypt($aad, $plainText, $cek) |
||
80 | |||
81 | /** |
||
82 | * @param string $aad |
||
83 | * @param string|resource $cek |
||
84 | * @param string $iv |
||
85 | * @param string $cipherText |
||
86 | * @param string $authTag |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function decrypt($aad, $cek, $iv, $cipherText, $authTag) |
||
113 | |||
114 | /** |
||
115 | * @param $aad |
||
116 | * @param $iv |
||
117 | * @param $cipherText |
||
118 | * @param $hmacKey |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | private function computeAuthTag($aad, $iv, $cipherText, $hmacKey) |
||
138 | } |
||
139 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.