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 |
||
18 | class AesKeyWrapAlgorithm implements JweAlgorithm |
||
19 | { |
||
20 | /** @var int */ |
||
21 | private $kekLengthBits; |
||
22 | |||
23 | /** @var string */ |
||
24 | private $wrapperClass; |
||
25 | |||
26 | /** @var array */ |
||
27 | private static $wrapperMap = [ |
||
28 | 128 => 'AESKW\A128KW', |
||
29 | 192 => 'AESKW\A192KW', |
||
30 | 256 => 'AESKW\A256KW', |
||
31 | ]; |
||
32 | |||
33 | /** @var RandomGenerator */ |
||
34 | private $randomGenerator; |
||
35 | |||
36 | /** |
||
37 | * @param int $keySize |
||
38 | * @param RandomGenerator $randomGenerator |
||
39 | */ |
||
40 | public function __construct($keySize, RandomGenerator $randomGenerator) |
||
50 | |||
51 | /** |
||
52 | * @param int $cekSizeBits |
||
53 | * @param string|resource $kek |
||
54 | * @param array $header |
||
55 | * |
||
56 | * @return array [cek, encryptedCek] |
||
57 | */ |
||
58 | public function wrapNewKey($cekSizeBits, $kek, array $header) |
||
74 | |||
75 | /** |
||
76 | * @param string $encryptedCek |
||
77 | * @param string $kek |
||
78 | * @param int $cekSizeBits |
||
79 | * @param array $header |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function unwrap($encryptedCek, $kek, $cekSizeBits, array $header) |
||
92 | |||
93 | /** |
||
94 | * @param string $kek |
||
95 | * @param string $key |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | private function aesWrap($kek, $key) |
||
103 | |||
104 | /** |
||
105 | * @param string $kek |
||
106 | * @param string $wrappedKey |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | private function aesUnwrap($kek, $wrappedKey) |
||
114 | } |
||
115 |
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.