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 |
||
| 23 | class Manager |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Authorization server. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $server; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * All scopes. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected static $scopes = [ |
||
| 38 | 'read' => 'Read', |
||
| 39 | 'read_write' => 'Read and Write', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Default scope. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected static $defaultScope; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Enable or didable implicit grant. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $isEnableImplicitGrant = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Refresh token expire at. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected static $refreshTokensExpireAt = 'P1Y'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * token exipre at. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected static $tokensExpireAt = 'P1Y'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create RSA authorisation keys. |
||
| 72 | * |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public static function keys($dir = null) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add New scopes. |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public static function setScopes(array $scopes = []) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get all available scopes. |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public static function getScopes() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Given a client, grant type and optional user identifier validate the set of scopes requested are valid and optionally |
||
| 120 | * append additional scopes or remove requested scopes. |
||
| 121 | * |
||
| 122 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
| 123 | * @param string $grantType |
||
| 124 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $clientEntity |
||
| 125 | * @param null|string $userIdentifier |
||
| 126 | * |
||
| 127 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
| 128 | */ |
||
| 129 | public static function filterScopes($scopes, $grantType) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Create a CryptKey instance without permissions check. |
||
| 144 | * |
||
| 145 | * @param string $identifier |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public static function hasScope($identifier) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Check is isset a scope or not. |
||
| 156 | * |
||
| 157 | * @param string $identifier |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public static function isValidateScope($identifier) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Make the authorization service instance. |
||
| 168 | * |
||
| 169 | * @return \League\OAuth2\Server\AuthorizationServer |
||
| 170 | */ |
||
| 171 | public function makeAuthorizationServer() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Enable All grant types. |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | protected function enableGrantTypes() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Enable Authorization code Grant. |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | View Code Duplication | protected function enableAuthCodeGrant() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Enable Password Grant. |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | View Code Duplication | protected function enablePasswordGrant() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Enable Refresh token Grant. |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | View Code Duplication | protected function enableRefreshTokenGrant() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Create a resource server for validation. |
||
| 274 | * |
||
| 275 | * @return \League\OAuth2\Server\ResourServer |
||
| 276 | */ |
||
| 277 | public function getResourceServer() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Create a CryptKey instance without permissions check. |
||
| 287 | * |
||
| 288 | * @param string $key |
||
| 289 | * |
||
| 290 | * @return \League\OAuth2\Server\CryptKey |
||
| 291 | */ |
||
| 292 | protected function makeCryptKey($type) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Validate user for current request. |
||
| 301 | * |
||
| 302 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 303 | * |
||
| 304 | * @return null|int |
||
| 305 | */ |
||
| 306 | public function validateUserForRequest($request) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Migrate tables. |
||
| 328 | * |
||
| 329 | * @param string $dir |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | View Code Duplication | public static function migrate($dir = null) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Drop tables. |
||
| 348 | * |
||
| 349 | * @param string $dir |
||
| 350 | * |
||
| 351 | * @return void |
||
| 352 | */ |
||
| 353 | View Code Duplication | public static function rollback($dir = null) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Drop and migrate fresh tables. |
||
| 368 | * |
||
| 369 | * @param string $dir |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public static function refresh($dir = null) |
||
| 382 | } |
||
| 383 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: