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 |
||
39 | class OC_JSON { |
||
40 | protected static $send_content_type_header = false; |
||
41 | /** |
||
42 | * set Content-Type header to jsonrequest |
||
43 | * @deprecated Use a AppFramework JSONResponse instead |
||
44 | */ |
||
45 | public static function setContentTypeHeader($type='application/json') { |
||
52 | |||
53 | /** |
||
54 | * Check if the app is enabled, send json error msg if not |
||
55 | * @param string $app |
||
56 | * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled. |
||
57 | */ |
||
58 | View Code Duplication | public static function checkAppEnabled($app) { |
|
65 | |||
66 | View Code Duplication | private static function sendErrorAndExit() { |
|
72 | |||
73 | /** |
||
74 | * Check if the user is logged in, send json error msg if not |
||
75 | * @deprecated Use annotation based ACLs from the AppFramework instead |
||
76 | */ |
||
77 | public static function checkLoggedIn() { |
||
97 | |||
98 | /** |
||
99 | * Check an ajax get/post call if the request token is valid, send json error msg if not. |
||
100 | * @deprecated Use annotation based CSRF checks from the AppFramework instead |
||
101 | */ |
||
102 | View Code Duplication | public static function callCheck() { |
|
109 | |||
110 | /** |
||
111 | * Check if the user is a admin, send json error msg if not. |
||
112 | * @deprecated Use annotation based ACLs from the AppFramework instead |
||
113 | */ |
||
114 | View Code Duplication | public static function checkAdminUser() { |
|
121 | |||
122 | /** |
||
123 | * Check is a given user exists - send json error msg if not |
||
124 | * @param string $user |
||
125 | * @deprecated Use a AppFramework JSONResponse instead |
||
126 | */ |
||
127 | View Code Duplication | public static function checkUserExists($user) { |
|
134 | |||
135 | /** |
||
136 | * Check if the user has administration privileges, send json error msg if not |
||
137 | * @deprecated Use annotation based ACLs from the AppFramework instead |
||
138 | */ |
||
139 | public static function checkSubAdminUser() { |
||
154 | |||
155 | /** |
||
156 | * Send json error msg |
||
157 | * @deprecated Use a AppFramework JSONResponse instead |
||
158 | */ |
||
159 | public static function error($data = []) { |
||
163 | |||
164 | /** |
||
165 | * Send json success msg |
||
166 | * @deprecated Use a AppFramework JSONResponse instead |
||
167 | */ |
||
168 | public static function success($data = []) { |
||
172 | |||
173 | /** |
||
174 | * Convert \OC\L10N\String to string, for use in json encodings |
||
175 | */ |
||
176 | protected static function to_string(&$value) { |
||
181 | |||
182 | /** |
||
183 | * Encode and print $data in json format |
||
184 | * @deprecated Use a AppFramework JSONResponse instead |
||
185 | */ |
||
186 | public static function encodedPrint($data, $setContentType=true) { |
||
192 | |||
193 | /** |
||
194 | * Encode JSON |
||
195 | * @deprecated Use a AppFramework JSONResponse instead |
||
196 | */ |
||
197 | public static function encode($data) { |
||
203 | } |
||
204 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: