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 |
||
13 | abstract class AbstractAuthentication extends AbstractAPI |
||
14 | { |
||
15 | /** |
||
16 | * The HTTP request instance. |
||
17 | * |
||
18 | * @var \Symfony\Component\HttpFoundation\Request |
||
19 | */ |
||
20 | protected $request; |
||
21 | |||
22 | /** |
||
23 | * The client ID. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $clientId; |
||
28 | |||
29 | /** |
||
30 | * The client secret. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $clientSecret; |
||
35 | |||
36 | /** |
||
37 | * The redirect URL. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $redirectUrl; |
||
42 | |||
43 | /** |
||
44 | * The custom parameters to be sent with the request. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $parameters = []; |
||
49 | |||
50 | /** |
||
51 | * The scopes being requested. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $scopes = []; |
||
56 | |||
57 | /** |
||
58 | * The separating character for the requested scopes. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $scopeSeparator = ','; |
||
63 | |||
64 | /** |
||
65 | * The type of the encoding in the query. |
||
66 | * |
||
67 | * @var int Can be either PHP_QUERY_RFC3986 or PHP_QUERY_RFC1738 |
||
68 | */ |
||
69 | protected $encodingType = PHP_QUERY_RFC1738; |
||
70 | |||
71 | /** |
||
72 | * Indicates if the session state should be utilized. |
||
73 | * |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $stateless = false; |
||
77 | |||
78 | /** |
||
79 | * AbstractService constructor. |
||
80 | * |
||
81 | * @param AccessToken $accessToken |
||
82 | */ |
||
83 | public function __construct(AccessToken $accessToken) |
||
91 | |||
92 | /** |
||
93 | * Get the authentication URL. |
||
94 | * |
||
95 | * @param string $state |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | abstract protected function getAuthUrl($state); |
||
100 | |||
101 | /** |
||
102 | * Redirect the user of the application to the provider's authentication screen. |
||
103 | * |
||
104 | * @param string $redirectUrl |
||
105 | * |
||
106 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
107 | */ |
||
108 | public function redirect($redirectUrl = null) |
||
122 | |||
123 | /** |
||
124 | * Set redirect url. |
||
125 | * |
||
126 | * @param string $redirectUrl |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function setRedirectUrl($redirectUrl) |
||
136 | |||
137 | /** |
||
138 | * Set redirect url. |
||
139 | * |
||
140 | * @param string $redirectUrl |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function withRedirectUrl($redirectUrl) |
||
150 | |||
151 | /** |
||
152 | * Return the redirect url. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getRedirectUrl() |
||
160 | |||
161 | /** |
||
162 | * Set the scopes of the requested access. |
||
163 | * |
||
164 | * @param array $scopes |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function scopes(array $scopes) |
||
174 | |||
175 | /** |
||
176 | * Set the request instance. |
||
177 | * |
||
178 | * @param Request $request |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setRequest(Request $request) |
||
188 | |||
189 | /** |
||
190 | * Get the request instance. |
||
191 | * |
||
192 | * @return Request |
||
193 | */ |
||
194 | public function getRequest() |
||
198 | |||
199 | /** |
||
200 | * Indicates that the provider should operate as stateless. |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function stateless() |
||
210 | |||
211 | /** |
||
212 | * Set the custom parameters of the request. |
||
213 | * |
||
214 | * @param array $parameters |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function with(array $parameters) |
||
224 | |||
225 | /** |
||
226 | * Get the authentication URL for the provider. |
||
227 | * |
||
228 | * @param string $url |
||
229 | * @param string $state |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | protected function buildAuthUrlFromBase($url, $state) |
||
237 | |||
238 | /** |
||
239 | * Get the GET parameters for the code request. |
||
240 | * |
||
241 | * @param string|null $state |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | protected function getCodeFields($state = null) |
||
260 | |||
261 | /** |
||
262 | * Format the given scopes. |
||
263 | * |
||
264 | * @param array $scopes |
||
265 | * @param string $scopeSeparator |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | protected function formatScopes(array $scopes, $scopeSeparator) |
||
273 | |||
274 | /** |
||
275 | * Determine if the current request / session has a mismatching "state". |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | protected function hasInvalidState() |
||
289 | |||
290 | /** |
||
291 | * Get the POST fields for the token request. |
||
292 | * |
||
293 | * @param string $code |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | protected function getTokenFields($code) |
||
306 | |||
307 | /** |
||
308 | * Get the code from the request. |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | protected function getCode() |
||
316 | |||
317 | /** |
||
318 | * Determine if the provider is operating with state. |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | protected function usesState() |
||
326 | |||
327 | /** |
||
328 | * Determine if the provider is operating as stateless. |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | protected function isStateless() |
||
336 | |||
337 | /** |
||
338 | * Return array item by key. |
||
339 | * |
||
340 | * @param array $array |
||
341 | * @param string $key |
||
342 | * @param mixed $default |
||
343 | * |
||
344 | * @return mixed |
||
345 | */ |
||
346 | View Code Duplication | protected function arrayItem(array $array, $key, $default = null) |
|
366 | |||
367 | /** |
||
368 | * Put state to session storage and return it. |
||
369 | * |
||
370 | * @return string|bool |
||
371 | */ |
||
372 | protected function makeState() |
||
387 | } |
||
388 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.