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 |
||
8 | class RsaClientCredentials extends ClientCredentials |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $rsaPublicKeyFile; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $rsaPrivateKeyFile; |
||
19 | |||
20 | /** |
||
21 | * @var resource|null |
||
22 | */ |
||
23 | protected $rsaPublicKey; |
||
24 | |||
25 | /** |
||
26 | * @var resource|null |
||
27 | */ |
||
28 | protected $rsaPrivateKey; |
||
29 | |||
30 | /** |
||
31 | * Sets the path to the RSA public key. |
||
32 | * |
||
33 | * @param string $filename |
||
34 | * |
||
35 | * @return self |
||
36 | */ |
||
37 | public function setRsaPublicKey($filename) |
||
44 | |||
45 | /** |
||
46 | * Sets the path to the RSA private key. |
||
47 | * |
||
48 | * @param string $filename |
||
49 | * |
||
50 | * @return self |
||
51 | */ |
||
52 | public function setRsaPrivateKey($filename) |
||
59 | |||
60 | /** |
||
61 | * Gets the RSA public key. |
||
62 | * |
||
63 | * @throws CredentialsException when the key could not be loaded. |
||
64 | * |
||
65 | * @return resource |
||
66 | */ |
||
67 | View Code Duplication | public function getRsaPublicKey() |
|
85 | |||
86 | /** |
||
87 | * Gets the RSA private key. |
||
88 | * |
||
89 | * @throws CredentialsException when the key could not be loaded. |
||
90 | * |
||
91 | * @return resource |
||
92 | */ |
||
93 | View Code Duplication | public function getRsaPrivateKey() |
|
111 | |||
112 | public function __destruct() |
||
122 | } |
||
123 |
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.