Complex classes like Util often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class Util { |
||
60 | /** |
||
61 | * @deprecated 14.0.0 use \OCP\ILogger::DEBUG |
||
62 | */ |
||
63 | const DEBUG=0; |
||
64 | /** |
||
65 | * @deprecated 14.0.0 use \OCP\ILogger::INFO |
||
66 | */ |
||
67 | const INFO=1; |
||
68 | /** |
||
69 | * @deprecated 14.0.0 use \OCP\ILogger::WARN |
||
70 | */ |
||
71 | const WARN=2; |
||
72 | /** |
||
73 | * @deprecated 14.0.0 use \OCP\ILogger::ERROR |
||
74 | */ |
||
75 | const ERROR=3; |
||
76 | /** |
||
77 | * @deprecated 14.0.0 use \OCP\ILogger::FATAL |
||
78 | */ |
||
79 | const FATAL=4; |
||
80 | |||
81 | /** \OCP\Share\IManager */ |
||
82 | private static $shareManager; |
||
83 | |||
84 | /** |
||
85 | * get the current installed version of Nextcloud |
||
86 | * @return array |
||
87 | * @since 4.0.0 |
||
88 | */ |
||
89 | public static function getVersion() { |
||
92 | |||
93 | /** |
||
94 | * Set current update channel |
||
95 | * @param string $channel |
||
96 | * @since 8.1.0 |
||
97 | */ |
||
98 | public static function setChannel($channel) { |
||
101 | |||
102 | /** |
||
103 | * Get current update channel |
||
104 | * @return string |
||
105 | * @since 8.1.0 |
||
106 | */ |
||
107 | public static function getChannel() { |
||
110 | |||
111 | /** |
||
112 | * write a message in the log |
||
113 | * @param string $app |
||
114 | * @param string $message |
||
115 | * @param int $level |
||
116 | * @since 4.0.0 |
||
117 | * @deprecated 13.0.0 use log of \OCP\ILogger |
||
118 | */ |
||
119 | public static function writeLog( $app, $message, $level ) { |
||
123 | |||
124 | /** |
||
125 | * check if sharing is disabled for the current user |
||
126 | * |
||
127 | * @return boolean |
||
128 | * @since 7.0.0 |
||
129 | * @deprecated 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser |
||
130 | */ |
||
131 | public static function isSharingDisabledForUser() { |
||
143 | |||
144 | /** |
||
145 | * get l10n object |
||
146 | * @param string $application |
||
147 | * @param string|null $language |
||
148 | * @return \OCP\IL10N |
||
149 | * @since 6.0.0 - parameter $language was added in 8.0.0 |
||
150 | */ |
||
151 | public static function getL10N($application, $language = null) { |
||
154 | |||
155 | /** |
||
156 | * add a css file |
||
157 | * @param string $application |
||
158 | * @param string $file |
||
159 | * @since 4.0.0 |
||
160 | */ |
||
161 | public static function addStyle( $application, $file = null ) { |
||
164 | |||
165 | /** |
||
166 | * add a javascript file |
||
167 | * @param string $application |
||
168 | * @param string $file |
||
169 | * @since 4.0.0 |
||
170 | */ |
||
171 | public static function addScript( $application, $file = null ) { |
||
174 | |||
175 | /** |
||
176 | * Add a translation JS file |
||
177 | * @param string $application application id |
||
178 | * @param string $languageCode language code, defaults to the current locale |
||
179 | * @since 8.0.0 |
||
180 | */ |
||
181 | public static function addTranslations($application, $languageCode = null) { |
||
184 | |||
185 | /** |
||
186 | * Add a custom element to the header |
||
187 | * If $text is null then the element will be written as empty element. |
||
188 | * So use "" to get a closing tag. |
||
189 | * @param string $tag tag name of the element |
||
190 | * @param array $attributes array of attributes for the element |
||
191 | * @param string $text the text content for the element |
||
192 | * @since 4.0.0 |
||
193 | */ |
||
194 | public static function addHeader($tag, $attributes, $text=null) { |
||
197 | |||
198 | /** |
||
199 | * Creates an absolute url to the given app and file. |
||
200 | * @param string $app app |
||
201 | * @param string $file file |
||
202 | * @param array $args array with param=>value, will be appended to the returned url |
||
203 | * The value of $args will be urlencoded |
||
204 | * @return string the url |
||
205 | * @since 4.0.0 - parameter $args was added in 4.5.0 |
||
206 | */ |
||
207 | public static function linkToAbsolute( $app, $file, $args = array() ) { |
||
213 | |||
214 | /** |
||
215 | * Creates an absolute url for remote use. |
||
216 | * @param string $service id |
||
217 | * @return string the url |
||
218 | * @since 4.0.0 |
||
219 | */ |
||
220 | public static function linkToRemote( $service ) { |
||
227 | |||
228 | /** |
||
229 | * Creates an absolute url for public use |
||
230 | * @param string $service id |
||
231 | * @return string the url |
||
232 | * @since 4.5.0 |
||
233 | */ |
||
234 | public static function linkToPublic($service) { |
||
237 | |||
238 | /** |
||
239 | * Returns the server host name without an eventual port number |
||
240 | * @return string the server hostname |
||
241 | * @since 5.0.0 |
||
242 | */ |
||
243 | public static function getServerHostName() { |
||
252 | |||
253 | /** |
||
254 | * Returns the default email address |
||
255 | * @param string $user_part the user part of the address |
||
256 | * @return string the default email address |
||
257 | * |
||
258 | * Assembles a default email address (using the server hostname |
||
259 | * and the given user part, and returns it |
||
260 | * Example: when given lostpassword-noreply as $user_part param, |
||
261 | * and is currently accessed via http(s)://example.com/, |
||
262 | * it would return '[email protected]' |
||
263 | * |
||
264 | * If the configuration value 'mail_from_address' is set in |
||
265 | * config.php, this value will override the $user_part that |
||
266 | * is passed to this function |
||
267 | * @since 5.0.0 |
||
268 | */ |
||
269 | public static function getDefaultEmailAddress($user_part) { |
||
284 | |||
285 | /** |
||
286 | * Make a human file size (2048 to 2 kB) |
||
287 | * @param int $bytes file size in bytes |
||
288 | * @return string a human readable file size |
||
289 | * @since 4.0.0 |
||
290 | */ |
||
291 | public static function humanFileSize($bytes) { |
||
294 | |||
295 | /** |
||
296 | * Make a computer file size (2 kB to 2048) |
||
297 | * @param string $str file size in a fancy format |
||
298 | * @return float a file size in bytes |
||
299 | * |
||
300 | * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
||
301 | * @since 4.0.0 |
||
302 | */ |
||
303 | public static function computerFileSize($str) { |
||
306 | |||
307 | /** |
||
308 | * connects a function to a hook |
||
309 | * |
||
310 | * @param string $signalClass class name of emitter |
||
311 | * @param string $signalName name of signal |
||
312 | * @param string|object $slotClass class name of slot |
||
313 | * @param string $slotName name of slot |
||
314 | * @return bool |
||
315 | * |
||
316 | * This function makes it very easy to connect to use hooks. |
||
317 | * |
||
318 | * TODO: write example |
||
319 | * @since 4.0.0 |
||
320 | */ |
||
321 | static public function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
||
324 | |||
325 | /** |
||
326 | * Emits a signal. To get data from the slot use references! |
||
327 | * @param string $signalclass class name of emitter |
||
328 | * @param string $signalname name of signal |
||
329 | * @param array $params default: array() array with additional data |
||
330 | * @return bool true if slots exists or false if not |
||
331 | * |
||
332 | * TODO: write example |
||
333 | * @since 4.0.0 |
||
334 | */ |
||
335 | static public function emitHook($signalclass, $signalname, $params = array()) { |
||
338 | |||
339 | /** |
||
340 | * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
||
341 | * multiple OC_Template elements which invoke `callRegister`. If the value |
||
342 | * would not be cached these unit-tests would fail. |
||
343 | * @var string |
||
344 | */ |
||
345 | private static $token = ''; |
||
346 | |||
347 | /** |
||
348 | * Register an get/post call. This is important to prevent CSRF attacks |
||
349 | * @since 4.5.0 |
||
350 | */ |
||
351 | public static function callRegister() { |
||
357 | |||
358 | /** |
||
359 | * Check an ajax get/post call if the request token is valid. exit if not. |
||
360 | * @since 4.5.0 |
||
361 | * @deprecated 9.0.0 Use annotations based on the app framework. |
||
362 | */ |
||
363 | public static function callCheck() { |
||
373 | |||
374 | /** |
||
375 | * Used to sanitize HTML |
||
376 | * |
||
377 | * This function is used to sanitize HTML and should be applied on any |
||
378 | * string or array of strings before displaying it on a web page. |
||
379 | * |
||
380 | * @param string|array $value |
||
381 | * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
||
382 | * @since 4.5.0 |
||
383 | */ |
||
384 | public static function sanitizeHTML($value) { |
||
387 | |||
388 | /** |
||
389 | * Public function to encode url parameters |
||
390 | * |
||
391 | * This function is used to encode path to file before output. |
||
392 | * Encoding is done according to RFC 3986 with one exception: |
||
393 | * Character '/' is preserved as is. |
||
394 | * |
||
395 | * @param string $component part of URI to encode |
||
396 | * @return string |
||
397 | * @since 6.0.0 |
||
398 | */ |
||
399 | public static function encodePath($component) { |
||
402 | |||
403 | /** |
||
404 | * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
||
405 | * |
||
406 | * @param array $input The array to work on |
||
407 | * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
||
408 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
||
409 | * @return array |
||
410 | * @since 4.5.0 |
||
411 | */ |
||
412 | public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
||
415 | |||
416 | /** |
||
417 | * performs a search in a nested array |
||
418 | * |
||
419 | * @param array $haystack the array to be searched |
||
420 | * @param string $needle the search string |
||
421 | * @param mixed $index optional, only search this key name |
||
422 | * @return mixed the key of the matching field, otherwise false |
||
423 | * @since 4.5.0 |
||
424 | */ |
||
425 | public static function recursiveArraySearch($haystack, $needle, $index = null) { |
||
428 | |||
429 | /** |
||
430 | * calculates the maximum upload size respecting system settings, free space and user quota |
||
431 | * |
||
432 | * @param string $dir the current folder where the user currently operates |
||
433 | * @param int $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
||
434 | * @return int number of bytes representing |
||
435 | * @since 5.0.0 |
||
436 | */ |
||
437 | public static function maxUploadFilesize($dir, $free = null) { |
||
440 | |||
441 | /** |
||
442 | * Calculate free space left within user quota |
||
443 | * @param string $dir the current folder where the user currently operates |
||
444 | * @return int number of bytes representing |
||
445 | * @since 7.0.0 |
||
446 | */ |
||
447 | public static function freeSpace($dir) { |
||
450 | |||
451 | /** |
||
452 | * Calculate PHP upload limit |
||
453 | * |
||
454 | * @return int number of bytes representing |
||
455 | * @since 7.0.0 |
||
456 | */ |
||
457 | public static function uploadLimit() { |
||
460 | |||
461 | /** |
||
462 | * Returns whether the given file name is valid |
||
463 | * @param string $file file name to check |
||
464 | * @return bool true if the file name is valid, false otherwise |
||
465 | * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
||
466 | * @since 7.0.0 |
||
467 | * @suppress PhanDeprecatedFunction |
||
468 | */ |
||
469 | public static function isValidFileName($file) { |
||
472 | |||
473 | /** |
||
474 | * Compare two strings to provide a natural sort |
||
475 | * @param string $a first string to compare |
||
476 | * @param string $b second string to compare |
||
477 | * @return int -1 if $b comes before $a, 1 if $a comes before $b |
||
478 | * or 0 if the strings are identical |
||
479 | * @since 7.0.0 |
||
480 | */ |
||
481 | public static function naturalSortCompare($a, $b) { |
||
484 | |||
485 | /** |
||
486 | * check if a password is required for each public link |
||
487 | * @return boolean |
||
488 | * @since 7.0.0 |
||
489 | */ |
||
490 | public static function isPublicLinkPasswordRequired() { |
||
493 | |||
494 | /** |
||
495 | * check if share API enforces a default expire date |
||
496 | * @return boolean |
||
497 | * @since 8.0.0 |
||
498 | */ |
||
499 | public static function isDefaultExpireDateEnforced() { |
||
502 | |||
503 | protected static $needUpgradeCache = null; |
||
504 | |||
505 | /** |
||
506 | * Checks whether the current version needs upgrade. |
||
507 | * |
||
508 | * @return bool true if upgrade is needed, false otherwise |
||
509 | * @since 7.0.0 |
||
510 | */ |
||
511 | public static function needUpgrade() { |
||
517 | |||
518 | /** |
||
519 | * is this Internet explorer ? |
||
520 | * |
||
521 | * @return boolean |
||
522 | * @since 14.0.0 |
||
523 | */ |
||
524 | public static function isIe() { |
||
527 | } |
||
528 |