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 |
||
21 | abstract class ECDSAAlgorithm extends OpenSSLSignatureAlgorithm |
||
22 | { |
||
23 | /** |
||
24 | * Mapping from algorithm name to class name. |
||
25 | * |
||
26 | * @internal |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | const MAP_NAME_TO_CLASS = array( |
||
31 | /* @formatter:off */ |
||
32 | JWA::ALGO_ES256 => ES256Algorithm::class, |
||
33 | JWA::ALGO_ES384 => ES384Algorithm::class, |
||
34 | JWA::ALGO_ES512 => ES512Algorithm::class |
||
35 | /* @formatter:on */ |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * Signature size in bytes. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | private $_signatureSize; |
||
44 | |||
45 | /** |
||
46 | * Get the name of the curve used by this algorithm. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | abstract protected function _curveName(); |
||
51 | |||
52 | /** |
||
53 | * Constructor |
||
54 | * |
||
55 | * @param ECPublicKeyJWK $pub_key |
||
56 | * @param ECPrivateKeyJWK $priv_key |
||
57 | */ |
||
58 | 11 | protected function __construct(ECPublicKeyJWK $pub_key, |
|
71 | |||
72 | /** |
||
73 | * Initialize from a public key. |
||
74 | * |
||
75 | * @param ECPublicKeyJWK $jwk |
||
76 | * @return self |
||
77 | */ |
||
78 | 5 | public static function fromPublicKey(ECPublicKeyJWK $jwk) { |
|
81 | |||
82 | /** |
||
83 | * Initialize from a private key. |
||
84 | * |
||
85 | * @param ECPrivateKeyJWK $jwk |
||
86 | * @return self |
||
87 | */ |
||
88 | 6 | public static function fromPrivateKey(ECPrivateKeyJWK $jwk) { |
|
91 | |||
92 | /** |
||
93 | * Initialize from a JWK. |
||
94 | * |
||
95 | * If algorithm is not specified, look from JWK. |
||
96 | * |
||
97 | * @param JWK $jwk |
||
98 | * @param string|null $alg Optional algorithm name |
||
99 | * @throws \UnexpectedValueException |
||
100 | * @return self |
||
101 | */ |
||
102 | 8 | View Code Duplication | public static function fromJWK(JWK $jwk, $alg = null) { |
126 | |||
127 | /** |
||
128 | * |
||
129 | * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::computeSignature() |
||
130 | * @return string |
||
131 | */ |
||
132 | 4 | public function computeSignature($data) { |
|
141 | |||
142 | /** |
||
143 | * |
||
144 | * @see \JWX\JWS\Algorithm\OpenSSLSignatureAlgorithm::validateSignature() |
||
145 | * @return bool |
||
146 | */ |
||
147 | 5 | public function validateSignature($data, $signature) { |
|
159 | |||
160 | /** |
||
161 | * |
||
162 | * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
||
163 | * @return JWTParameter[] |
||
164 | */ |
||
165 | 2 | public function headerParameters() { |
|
168 | } |
||
169 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.