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 |
||
11 | class ProtocolParameter implements ProtocolParameterInterface |
||
12 | { |
||
13 | /** |
||
14 | * The ConfigInterface instance. |
||
15 | * |
||
16 | * @var \Risan\OAuth1\Config\ConfigInterface |
||
17 | */ |
||
18 | protected $config; |
||
19 | |||
20 | /** |
||
21 | * The SignerInterface instance. |
||
22 | * |
||
23 | * @var \Risan\OAuth1\Signature\SignerInterface |
||
24 | */ |
||
25 | protected $signer; |
||
26 | |||
27 | /** |
||
28 | * The NonceGeneratorInterface instance. |
||
29 | * |
||
30 | * @var \Risan\OAuth1\Request\NonceGeneratorInterface |
||
31 | */ |
||
32 | protected $nonceGenerator; |
||
33 | |||
34 | /** |
||
35 | * Create ProtocolParameter instance. |
||
36 | * |
||
37 | * @param \Risan\OAuth1\ConfigInterface $config |
||
38 | * @param \Risan\OAuth1\Signature\SignerInterface $signer |
||
39 | * @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator |
||
40 | */ |
||
41 | public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator) |
||
47 | |||
48 | /** |
||
49 | * {@inheritDoc} |
||
50 | */ |
||
51 | public function getConfig() |
||
55 | |||
56 | /** |
||
57 | * {@inheritDoc} |
||
58 | */ |
||
59 | public function getSigner() |
||
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | public function getNonceGenerator() |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | public function getCurrentTimestamp() |
||
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public function getVersion() |
||
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | View Code Duplication | public function getBase() |
|
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | */ |
||
105 | View Code Duplication | public function forTemporaryCredentials() |
|
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function forTokenCredentials(TemporaryCredentials $temporaryCredentials, $verificationCode) |
||
138 | |||
139 | /** |
||
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | public function getSignature(array $protocolParameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST') |
||
149 | |||
150 | /** |
||
151 | * Build the signature parameters to be signed. |
||
152 | * |
||
153 | * @param array $protocolParameters |
||
154 | * @param array $requestOptions |
||
155 | * @return array |
||
156 | */ |
||
157 | public function signatureParameters(array $protocolParameters, array $requestOptions = []) |
||
171 | |||
172 | /** |
||
173 | * Setup the signer. |
||
174 | * |
||
175 | * @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials |
||
176 | * @return \Risan\OAuth1\Signature\SignerInterface |
||
177 | */ |
||
178 | public function setupSigner(ServerIssuedCredentials $serverIssuedCredentials = null) |
||
190 | |||
191 | /** |
||
192 | * Should sign with the client credentials. |
||
193 | * |
||
194 | * @return boolean |
||
195 | */ |
||
196 | public function shouldSignWithClientCredentials() |
||
200 | |||
201 | /** |
||
202 | * Should sign with the server issued credentials. |
||
203 | * |
||
204 | * @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials |
||
205 | * @return boolean |
||
206 | */ |
||
207 | public function shouldSignWithServerIssuedCredentials(ServerIssuedCredentials $serverIssuedCredentials = null) |
||
211 | |||
212 | /** |
||
213 | * Check if request options has the given key option. |
||
214 | * |
||
215 | * @param array $requestOptions |
||
216 | * @param string $key |
||
217 | * @return boolean |
||
218 | */ |
||
219 | public function requestOptionsHas(array $requestOptions, $key) |
||
225 | } |
||
226 |
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.