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 |
||
7 | class Helper |
||
8 | { |
||
9 | /** |
||
10 | * Stores the app key of current api client. |
||
11 | */ |
||
12 | const CURRENT_APP_KEY = 'current_app_key'; |
||
13 | |||
14 | /** |
||
15 | * Set current app key for the request. |
||
16 | * |
||
17 | * @param \Illuminate\Http\Request $request |
||
18 | * @param string $key |
||
19 | * @return \Illuminate\Http\Request |
||
20 | */ |
||
21 | public static function setCurrentAppKeyForRequest(Request $request, $key) |
||
27 | |||
28 | /** |
||
29 | * Get the app key of current api client. |
||
30 | * |
||
31 | * @param \Illuminate\Http\Request $request |
||
32 | * @return string|null |
||
33 | */ |
||
34 | public static function getCurrentAppKey(Request $request) |
||
38 | |||
39 | /** |
||
40 | * Generate data of api token for HTTP headers. |
||
41 | * |
||
42 | * @param string $appKey |
||
43 | * @param string|int|null $time |
||
44 | * @return array |
||
45 | */ |
||
46 | View Code Duplication | public static function generateApiTokenForHttpHeaders($appKey, $time = null) |
|
58 | |||
59 | /** |
||
60 | * Generate data of api token for URL query. |
||
61 | * |
||
62 | * @param string $appKey |
||
63 | * @param string|int|null $time |
||
64 | * @return array |
||
65 | */ |
||
66 | View Code Duplication | public static function generateApiTokenForUrlQuery($appKey, $time = null) |
|
78 | |||
79 | /** |
||
80 | * Generate data of api token for URL query string. |
||
81 | * |
||
82 | * @param string $appKey |
||
83 | * @param string|int|null $time |
||
84 | * @return string |
||
85 | */ |
||
86 | public static function generateApiTokenForUrlQueryString($appKey, $time = null) |
||
90 | } |
||
91 |
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.