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 |
||
14 | class RequestConfig implements RequestConfigInterface |
||
15 | { |
||
16 | /** |
||
17 | * The ConfigInterface instance. |
||
18 | * |
||
19 | * @var \Risan\OAuth1\ConfigInterface |
||
20 | */ |
||
21 | protected $config; |
||
22 | |||
23 | /** |
||
24 | * The SignerInterface instance. |
||
25 | * |
||
26 | * @var \Risan\OAuth1\Signature\SignerInterface |
||
27 | */ |
||
28 | protected $signer; |
||
29 | |||
30 | /** |
||
31 | * The NonceGeneratorInterface instance. |
||
32 | * |
||
33 | * @var \Risan\OAuth1\Request\NonceGeneratorInterface |
||
34 | */ |
||
35 | protected $nonceGenerator; |
||
36 | |||
37 | /** |
||
38 | * Create RequestBuilder instance. |
||
39 | * |
||
40 | * @param \Risan\OAuth1\ConfigInterface $config |
||
41 | * @param \Risan\OAuth1\Signature\SignerInterface $signer |
||
42 | * @param \Risan\OAuth1\Request\NonceGeneratorInterface $nonceGenerator |
||
43 | */ |
||
44 | public function __construct(ConfigInterface $config, SignerInterface $signer, NonceGeneratorInterface $nonceGenerator) |
||
54 | |||
55 | /** |
||
56 | * {@inheritDoc} |
||
57 | */ |
||
58 | public function getConfig() |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | public function getSigner() |
||
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | */ |
||
74 | public function getNonceGenerator() |
||
78 | |||
79 | /** |
||
80 | * {@inheritDoc} |
||
81 | */ |
||
82 | public function getCurrentTimestamp() |
||
86 | |||
87 | /** |
||
88 | * {@inheritDoc} |
||
89 | */ |
||
90 | public function getTemporaryCredentialsUrl() |
||
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | View Code Duplication | public function getTemporaryCredentialsAuthorizationHeader() |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | public function buildAuthorizationUrl(TemporaryCredentials $temporaryCredentials) |
||
120 | |||
121 | /** |
||
122 | * {@inheritDoc} |
||
123 | */ |
||
124 | public function getTokenCredentialsUrl() |
||
128 | |||
129 | /** |
||
130 | * {@inheritDoc} |
||
131 | */ |
||
132 | public function getTokenCredentialsAuthorizationHeader(TemporaryCredentials $temporaryCredentials, $verificationCode) |
||
151 | |||
152 | /** |
||
153 | * Get base protocol parameters for the authorization header. |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | View Code Duplication | public function getBaseProtocolParameters() |
|
167 | |||
168 | /** |
||
169 | * Add signature parameter to the given protocol parameters. |
||
170 | * |
||
171 | * @param array &$parameters |
||
172 | * @param string $uri |
||
173 | * @param \Risan\OAuth1\Credentials\ServerIssuedCredentials|null $serverIssuedCredentials |
||
174 | * @param array $requestOptions |
||
175 | * @param string $httpMethod |
||
176 | */ |
||
177 | public function addSignatureParameter(array &$parameters, $uri, ServerIssuedCredentials $serverIssuedCredentials = null, array $requestOptions = [], $httpMethod = 'POST') |
||
197 | |||
198 | /** |
||
199 | * Check if request options has the given key option. |
||
200 | * |
||
201 | * @param array $requestOptions |
||
202 | * @param string $key |
||
203 | * @return boolean |
||
204 | */ |
||
205 | public function requestOptionsHas(array $requestOptions, $key) |
||
211 | |||
212 | /** |
||
213 | * Append query parameters to URI. |
||
214 | * |
||
215 | * @param string $uri |
||
216 | * @param array $parameters |
||
217 | * @return string |
||
218 | */ |
||
219 | public function appendQueryParametersToUri($uri, array $parameters) |
||
229 | |||
230 | /** |
||
231 | * Normalize protocol parameters to be used as authorization header. |
||
232 | * |
||
233 | * @param array $parameters |
||
234 | * @return string |
||
235 | */ |
||
236 | public function normalizeProtocolParameters(array $parameters) |
||
244 | } |
||
245 |
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.