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 |
||
| 26 | class FluentSession extends AbstractFluentAdapter implements SessionInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Get a session from it's identifier. |
||
| 30 | * |
||
| 31 | * @param string $sessionId |
||
| 32 | * |
||
| 33 | * @return \League\OAuth2\Server\Entity\SessionEntity |
||
|
|
|||
| 34 | */ |
||
| 35 | 6 | public function get($sessionId) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get a session from an access token. |
||
| 52 | * |
||
| 53 | * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken The access token |
||
| 54 | * |
||
| 55 | * @return \League\OAuth2\Server\Entity\SessionEntity |
||
| 56 | */ |
||
| 57 | 6 | View Code Duplication | public function getByAccessToken(AccessTokenEntity $accessToken) |
| 58 | { |
||
| 59 | 6 | $result = $this->getConnection()->table('oauth_sessions') |
|
| 60 | 6 | ->select('oauth_sessions.*') |
|
| 61 | 6 | ->join('oauth_access_tokens', 'oauth_sessions.id', '=', 'oauth_access_tokens.session_id') |
|
| 62 | 6 | ->where('oauth_access_tokens.id', $accessToken->getId()) |
|
| 63 | 6 | ->first(); |
|
| 64 | |||
| 65 | 6 | if (is_null($result)) { |
|
| 66 | 3 | return; |
|
| 67 | } |
||
| 68 | |||
| 69 | 3 | return (new SessionEntity($this->getServer())) |
|
| 70 | 3 | ->setId($result->id) |
|
| 71 | 3 | ->setOwner($result->owner_type, $result->owner_id); |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get a session's scopes. |
||
| 76 | * |
||
| 77 | * @param \League\OAuth2\Server\Entity\SessionEntity |
||
| 78 | * |
||
| 79 | * @return array Array of \League\OAuth2\Server\Entity\ScopeEntity |
||
| 80 | */ |
||
| 81 | 3 | View Code Duplication | public function getScopes(SessionEntity $session) |
| 101 | |||
| 102 | /** |
||
| 103 | * Create a new session. |
||
| 104 | * |
||
| 105 | * @param string $ownerType Session owner's type (user, client) |
||
| 106 | * @param string $ownerId Session owner's ID |
||
| 107 | * @param string $clientId Client ID |
||
| 108 | * @param string $clientRedirectUri Client redirect URI (default = null) |
||
| 109 | * |
||
| 110 | * @return int The session's ID |
||
| 111 | */ |
||
| 112 | 3 | public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Associate a scope with a session. |
||
| 126 | * |
||
| 127 | * @param \League\OAuth2\Server\Entity\SessionEntity $session |
||
| 128 | * @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scopes ID might be an integer or string |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 3 | View Code Duplication | public function associateScope(SessionEntity $session, ScopeEntity $scope) |
| 141 | |||
| 142 | /** |
||
| 143 | * Get a session from an auth code. |
||
| 144 | * |
||
| 145 | * @param \League\OAuth2\Server\Entity\AuthCodeEntity $authCode The auth code |
||
| 146 | * |
||
| 147 | * @return \League\OAuth2\Server\Entity\SessionEntity |
||
| 148 | */ |
||
| 149 | 6 | View Code Duplication | public function getByAuthCode(AuthCodeEntity $authCode) |
| 165 | } |
||
| 166 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.