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; |
||
22 | class Bitbucket extends oauth2\Provider |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritDoc} |
||
26 | */ |
||
27 | const URL_AUTHORIZE = 'https://bitbucket.org/site/oauth2/authorize'; |
||
28 | const URL_EXCHANGE = 'https://bitbucket.org/site/oauth2/access_token'; |
||
29 | const URL_IDENTIFY = 'https://api.bitbucket.org/2.0/user'; |
||
30 | |||
31 | /** |
||
32 | * {@inheritDoc} |
||
33 | * |
||
34 | * Note: self::createIdentity() is an override in Bitbucket's case - we provide subclasses to make a distinction |
||
35 | * between User and Team Identities, but the below class name points to the shared parent of those. |
||
36 | */ |
||
37 | const IDENTITY = auth\id\identities\Bitbucket::class; |
||
38 | |||
39 | /** |
||
40 | * {@inheritDoc} |
||
41 | * |
||
42 | * Note: BitBucket's OAuth 2.0 implementation does not currently (Sept 3rd 2016) support scope requests |
||
43 | * on the Authorize requests. Scopes are defined on a per-consumer basis in their admin panel instead, |
||
44 | * so this below is just a reminder of what scope needs to be set in the panel for this Provider to be able |
||
45 | * to request basic Identity data. Also - the 'account' scope already includes the 'email' scope. |
||
46 | */ |
||
47 | protected $defaultScopes = ['account']; |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | View Code Duplication | public function identify(oauth2\Token $token) : Promise |
|
79 | |||
80 | /** |
||
81 | * Returns a Promise for the e-mail address (primary and verified) belonging to the entity whose Access Token |
||
82 | * gets used to request that data. |
||
83 | * |
||
84 | * @param oauth2\Token $token The Access Token to use. |
||
85 | * @return Promise A Promise for the entity's e-mail address. |
||
86 | */ |
||
87 | View Code Duplication | protected function getEmail(oauth2\Token $token) : Promise |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | * |
||
101 | * Overridden because we return different Identity objects depending on what kind of entity's data we got. |
||
102 | */ |
||
103 | protected function createIdentity(oauth2\Token $token, array $data) : oauth2\Identity |
||
111 | } |
||
112 |
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.