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 |
||
30 | class Manager { |
||
31 | /** @var IAppData */ |
||
32 | private $appData; |
||
33 | /** @var ICrypto */ |
||
34 | private $crypto; |
||
35 | /** @var IConfig */ |
||
36 | private $config; |
||
37 | |||
38 | /** |
||
39 | * @param Factory $appDataFactory |
||
40 | * @param ICrypto $crypto |
||
41 | * @param IConfig $config |
||
42 | */ |
||
43 | public function __construct(Factory $appDataFactory, |
||
51 | |||
52 | /** |
||
53 | * Calls the openssl functions to generate a public and private key. |
||
54 | * In a separate function for unit testing purposes. |
||
55 | * |
||
56 | * @return array [$publicKey, $privateKey] |
||
57 | */ |
||
58 | protected function generateKeyPair() { |
||
74 | |||
75 | /** |
||
76 | * Generate a key for a given ID |
||
77 | * Note: If a key already exists it will be overwritten |
||
78 | * |
||
79 | * @param string $id key id |
||
80 | * @return Key |
||
81 | */ |
||
82 | protected function generateKey($id) { |
||
97 | |||
98 | /** |
||
99 | * Get key for a specific id |
||
100 | * |
||
101 | * @param string $id |
||
102 | * @return Key |
||
103 | */ |
||
104 | protected function retrieveKey($id) { |
||
116 | |||
117 | /** |
||
118 | * Get public and private key for $user |
||
119 | * |
||
120 | * @param IUser $user |
||
121 | * @return Key |
||
122 | */ |
||
123 | public function getKey(IUser $user) { |
||
127 | |||
128 | /** |
||
129 | * Get instance wide public and private key |
||
130 | * |
||
131 | * @return Key |
||
132 | * @throws \RuntimeException |
||
133 | */ |
||
134 | View Code Duplication | public function getSystemKey() { |
|
141 | |||
142 | |||
143 | } |
||
144 |