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 namespace nyx\auth\id\protocols\oauth2\providers; |
||
20 | class Github extends oauth2\Provider |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritDoc} |
||
24 | */ |
||
25 | const SCOPE_SEPARATOR = ','; |
||
26 | |||
27 | /** |
||
28 | * {@inheritDoc} |
||
29 | */ |
||
30 | const URL_AUTHORIZE = 'https://github.com/login/oauth/authorize'; |
||
31 | const URL_EXCHANGE = 'https://github.com/login/oauth/access_token'; |
||
32 | const URL_IDENTIFY = 'https://api.github.com/user'; |
||
33 | |||
34 | /** |
||
35 | * {@inheritDoc} |
||
36 | */ |
||
37 | const IDENTITY = auth\id\identities\Github::class; |
||
38 | |||
39 | /** |
||
40 | * {@inheritDoc} |
||
41 | */ |
||
42 | protected $defaultScopes = ['user:email']; |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | View Code Duplication | public function identify(oauth2\Token $token) : Promise |
|
74 | |||
75 | /** |
||
76 | * Returns a Promise for the e-mail address (primary and verified) belonging to the entity whose Access Token |
||
77 | * gets used to request that data. |
||
78 | * |
||
79 | * @param oauth2\Token $token The Access Token to use. |
||
80 | * @return Promise A Promise for the entity's e-mail address. |
||
81 | */ |
||
82 | View Code Duplication | protected function getEmail(oauth2\Token $token) : Promise |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | protected function onRequestSuccess(Response $response, auth\interfaces\Token $token = null) |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | View Code Duplication | protected function getDefaultRequestOptions(auth\interfaces\Token $token = null) : array |
|
120 | } |
||
121 |
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.