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 | |||
39 | /** |
||
40 | * Default scope. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected static $defaultScope; |
||
45 | |||
46 | /** |
||
47 | * Enable or didable implicit grant. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected static $isEnableImplicitGrant = false; |
||
52 | |||
53 | /** |
||
54 | * Refresh token expire at. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected static $refreshTokensExpireAt = 'P1Y'; |
||
59 | |||
60 | /** |
||
61 | * token exipre at. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected static $tokensExpireAt = 'P1Y'; |
||
66 | |||
67 | /** |
||
68 | * Create RSA authorisation keys. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | public static function keys($dir = null) |
||
90 | |||
91 | /** |
||
92 | * Add New scopes. |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | public static function setScopes(array $scopes = []) |
||
104 | |||
105 | /** |
||
106 | * Get all available scopes. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public static function getScopes() |
||
114 | |||
115 | /** |
||
116 | * Given a client, grant type and optional user identifier validate the set of scopes requested are valid and optionally |
||
117 | * append additional scopes or remove requested scopes. |
||
118 | * |
||
119 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
120 | * @param string $grantType |
||
121 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $clientEntity |
||
122 | * @param null|string $userIdentifier |
||
123 | * |
||
124 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
125 | */ |
||
126 | public static function filterScopes($scopes, $grantType) |
||
127 | { |
||
128 | if (!in_array($grantType, ['password', 'personal_access', 'client_credentials'])) { |
||
129 | $scopes = array_filter($scopes, function ($scope) { |
||
130 | return trim($scope->getIdentifier()) !== '*'; |
||
131 | }); |
||
132 | } |
||
133 | |||
134 | return array_filter($scopes, function ($scope) { |
||
135 | return static::hasScope($scope->getIdentifier()); |
||
136 | }); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Check scope exists or not. |
||
141 | * |
||
142 | * @param string $identifier |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public static function hasScope($identifier) |
||
150 | |||
151 | /** |
||
152 | * Check is isset a scope or not. |
||
153 | * |
||
154 | * @param string $identifier |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public static function isValidateScope($identifier) |
||
162 | |||
163 | /** |
||
164 | * Make the authorization service instance. |
||
165 | * |
||
166 | * @return \League\OAuth2\Server\AuthorizationServer |
||
167 | */ |
||
168 | public function makeAuthorizationServer() |
||
185 | |||
186 | /** |
||
187 | * Enable All grant types. |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | protected function enableGrantTypes() |
||
209 | |||
210 | /** |
||
211 | * Enable Authorization code Grant. |
||
212 | * |
||
213 | * @return void |
||
214 | */ |
||
215 | View Code Duplication | protected function enableAuthCodeGrant() |
|
230 | |||
231 | /** |
||
232 | * Enable Password Grant. |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | View Code Duplication | protected function enablePasswordGrant() |
|
251 | |||
252 | /** |
||
253 | * Enable Refresh token Grant. |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | View Code Duplication | protected function enableRefreshTokenGrant() |
|
268 | |||
269 | /** |
||
270 | * Enable Implicit Grant. |
||
271 | * |
||
272 | * @param bool $isImplicit |
||
273 | * |
||
274 | * @return void |
||
275 | */ |
||
276 | public static function enableImplicitGrantType($isImplicit = true) |
||
280 | |||
281 | /** |
||
282 | * Create a resource server for validation. |
||
283 | * |
||
284 | * @return \League\OAuth2\Server\ResourServer |
||
285 | */ |
||
286 | public function getResourceServer() |
||
293 | |||
294 | /** |
||
295 | * Create a CryptKey instance without permissions check. |
||
296 | * |
||
297 | * @param string $key |
||
298 | * |
||
299 | * @return \League\OAuth2\Server\CryptKey |
||
300 | */ |
||
301 | protected function makeCryptKey($type) |
||
307 | |||
308 | public function validateUserForRequest($request) |
||
327 | |||
328 | /** |
||
329 | * Load all routes. |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | public static function routes() |
||
337 | |||
338 | /** |
||
339 | * Migrate tables. |
||
340 | * |
||
341 | * @param string $dir |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | View Code Duplication | public static function migrate($dir = null) |
|
357 | |||
358 | /** |
||
359 | * Drop tables. |
||
360 | * |
||
361 | * @param string $dir |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | View Code Duplication | public static function rollback($dir = null) |
|
377 | |||
378 | /** |
||
379 | * Drop and migrate fresh tables. |
||
380 | * |
||
381 | * @param string $dir |
||
382 | * |
||
383 | * @return void |
||
384 | */ |
||
385 | public static function refresh($dir = null) |
||
394 | } |
||
395 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: