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 |
||
12 | class JwtAuth extends \Object implements IAuth { |
||
13 | |||
14 | public static function authenticate($email, $password) { |
||
20 | |||
21 | /** |
||
22 | * @param Member $user |
||
23 | * @return ApiSession |
||
24 | */ |
||
25 | public static function createSession($user) { |
||
32 | |||
33 | public static function delete($request) { |
||
36 | |||
37 | /** |
||
38 | * @param \SS_HTTPRequest $request |
||
39 | * @return \Member |
||
40 | */ |
||
41 | View Code Duplication | public static function current($request) { |
|
50 | |||
51 | /** |
||
52 | * |
||
53 | * |
||
54 | * @param string $token |
||
55 | * @throws RestUserException |
||
56 | * @return \Member |
||
57 | */ |
||
58 | private static function get_member_from_token($token) { |
||
82 | |||
83 | /** |
||
84 | * @param Member $user |
||
85 | * @return string |
||
86 | */ |
||
87 | private static function generate_token($user) { |
||
99 | |||
100 | /** |
||
101 | * @param array $data |
||
102 | * @param string $key |
||
103 | * @return string |
||
104 | */ |
||
105 | public static function jwt_encode($data, $key) { |
||
112 | |||
113 | private static function get_key() { |
||
116 | |||
117 | /** |
||
118 | * @param string $token |
||
119 | * @param string $key |
||
120 | * @return array |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | public static function jwt_decode($token, $key) { |
||
135 | |||
136 | static function base64_url_encode($data) { |
||
139 | |||
140 | static function base64_url_decode($base64) { |
||
143 | |||
144 | } |
||
145 |
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.