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 | public function validateUserForRequest($request) |
||
318 | |||
319 | /** |
||
320 | * Load all routes. |
||
321 | * |
||
322 | * @return void |
||
323 | */ |
||
324 | public static function routes() |
||
328 | |||
329 | /** |
||
330 | * Migrate tables. |
||
331 | * |
||
332 | * @param string $dir |
||
333 | * |
||
334 | * @return void |
||
335 | */ |
||
336 | View Code Duplication | public static function migrate($dir = null) |
|
348 | |||
349 | /** |
||
350 | * Drop tables. |
||
351 | * |
||
352 | * @param string $dir |
||
353 | * |
||
354 | * @return void |
||
355 | */ |
||
356 | View Code Duplication | public static function rollback($dir = null) |
|
368 | |||
369 | /** |
||
370 | * Drop and migrate fresh tables. |
||
371 | * |
||
372 | * @param string $dir |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | public static function refresh($dir = null) |
||
385 | } |
||
386 |
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: