Total Complexity | 56 |
Total Lines | 542 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
58 | class Util { |
||
59 | /** @var \OCP\Share\IManager */ |
||
60 | private static $shareManager; |
||
61 | |||
62 | /** @var array */ |
||
63 | private static $scripts = []; |
||
64 | |||
65 | /** @var array */ |
||
66 | private static $scriptDeps = []; |
||
67 | |||
68 | /** @var array */ |
||
69 | private static $sortedScriptDeps = []; |
||
|
|||
70 | |||
71 | /** |
||
72 | * get the current installed version of Nextcloud |
||
73 | * @return array |
||
74 | * @since 4.0.0 |
||
75 | */ |
||
76 | public static function getVersion() { |
||
77 | return \OC_Util::getVersion(); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @since 17.0.0 |
||
82 | */ |
||
83 | public static function hasExtendedSupport(): bool { |
||
84 | try { |
||
85 | /** @var \OCP\Support\Subscription\IRegistry */ |
||
86 | $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class); |
||
87 | return $subscriptionRegistry->delegateHasExtendedSupport(); |
||
88 | } catch (AppFramework\QueryException $e) { |
||
89 | } |
||
90 | return \OC::$server->getConfig()->getSystemValueBool('extendedSupport', false); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set current update channel |
||
95 | * @param string $channel |
||
96 | * @since 8.1.0 |
||
97 | */ |
||
98 | public static function setChannel($channel) { |
||
99 | \OC::$server->getConfig()->setSystemValue('updater.release.channel', $channel); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get current update channel |
||
104 | * @return string |
||
105 | * @since 8.1.0 |
||
106 | */ |
||
107 | public static function getChannel() { |
||
108 | return \OC_Util::getChannel(); |
||
109 | } |
||
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) { |
||
120 | $context = ['app' => $app]; |
||
121 | \OC::$server->getLogger()->log($level, $message, $context); |
||
122 | } |
||
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() { |
||
142 | } |
||
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) { |
||
152 | return \OC::$server->getL10N($application, $language); |
||
153 | } |
||
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) { |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * add a javascript file |
||
167 | * |
||
168 | * @param string $application |
||
169 | * @param string|null $file |
||
170 | * @param string $afterAppId |
||
171 | * @since 4.0.0 |
||
172 | */ |
||
173 | public static function addScript(string $application, string $file = null, string $afterAppId = 'core'): void { |
||
174 | if (!empty($application)) { |
||
175 | $path = "$application/js/$file"; |
||
176 | } else { |
||
177 | $path = "js/$file"; |
||
178 | } |
||
179 | |||
180 | // Inject js translations if we load a script for |
||
181 | // a specific app that is not core, as those js files |
||
182 | // need separate handling |
||
183 | if ($application !== 'core' |
||
184 | && $file !== null |
||
185 | && strpos($file, 'l10n') === false) { |
||
186 | self::addTranslations($application); |
||
187 | } |
||
188 | |||
189 | // store app in dependency list |
||
190 | if (!array_key_exists($application, self::$scriptDeps)) { |
||
191 | self::$scriptDeps[$application] = new AppScriptDependency($application, [$afterAppId]); |
||
192 | } else { |
||
193 | self::$scriptDeps[$application]->addDep($afterAppId); |
||
194 | } |
||
195 | |||
196 | self::$scripts[$application][] = $path; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Return the list of scripts injected to the page |
||
201 | * |
||
202 | * @return array |
||
203 | * @since 24.0.0 |
||
204 | */ |
||
205 | public static function getScripts(): array { |
||
206 | // Sort scriptDeps into sortedScriptDeps |
||
207 | $scriptSort = \OC::$server->get(AppScriptSort::class); |
||
208 | $sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps); |
||
209 | |||
210 | // Flatten array and remove duplicates |
||
211 | $sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : []; |
||
212 | |||
213 | // Override core-common and core-main order |
||
214 | array_unshift($sortedScripts, 'core/js/common', 'core/js/main'); |
||
215 | |||
216 | return array_unique($sortedScripts); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Add a translation JS file |
||
221 | * @param string $application application id |
||
222 | * @param string $languageCode language code, defaults to the current locale |
||
223 | * @since 8.0.0 |
||
224 | */ |
||
225 | public static function addTranslations($application, $languageCode = null) { |
||
226 | if (is_null($languageCode)) { |
||
227 | $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); |
||
228 | } |
||
229 | if (!empty($application)) { |
||
230 | $path = "$application/l10n/$languageCode"; |
||
231 | } else { |
||
232 | $path = "l10n/$languageCode"; |
||
233 | } |
||
234 | self::$scripts[$application][] = $path; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Add a custom element to the header |
||
239 | * If $text is null then the element will be written as empty element. |
||
240 | * So use "" to get a closing tag. |
||
241 | * @param string $tag tag name of the element |
||
242 | * @param array $attributes array of attributes for the element |
||
243 | * @param string $text the text content for the element |
||
244 | * @since 4.0.0 |
||
245 | */ |
||
246 | public static function addHeader($tag, $attributes, $text = null) { |
||
247 | \OC_Util::addHeader($tag, $attributes, $text); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Creates an absolute url to the given app and file. |
||
252 | * @param string $app app |
||
253 | * @param string $file file |
||
254 | * @param array $args array with param=>value, will be appended to the returned url |
||
255 | * The value of $args will be urlencoded |
||
256 | * @return string the url |
||
257 | * @since 4.0.0 - parameter $args was added in 4.5.0 |
||
258 | */ |
||
259 | public static function linkToAbsolute($app, $file, $args = []) { |
||
260 | $urlGenerator = \OC::$server->getURLGenerator(); |
||
261 | return $urlGenerator->getAbsoluteURL( |
||
262 | $urlGenerator->linkTo($app, $file, $args) |
||
263 | ); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Creates an absolute url for remote use. |
||
268 | * @param string $service id |
||
269 | * @return string the url |
||
270 | * @since 4.0.0 |
||
271 | */ |
||
272 | public static function linkToRemote($service) { |
||
277 | ); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Returns the server host name without an eventual port number |
||
282 | * @return string the server hostname |
||
283 | * @since 5.0.0 |
||
284 | */ |
||
285 | public static function getServerHostName() { |
||
286 | $host_name = \OC::$server->getRequest()->getServerHost(); |
||
287 | // strip away port number (if existing) |
||
288 | $colon_pos = strpos($host_name, ':'); |
||
289 | if ($colon_pos != false) { |
||
290 | $host_name = substr($host_name, 0, $colon_pos); |
||
291 | } |
||
292 | return $host_name; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Returns the default email address |
||
297 | * @param string $user_part the user part of the address |
||
298 | * @return string the default email address |
||
299 | * |
||
300 | * Assembles a default email address (using the server hostname |
||
301 | * and the given user part, and returns it |
||
302 | * Example: when given lostpassword-noreply as $user_part param, |
||
303 | * and is currently accessed via http(s)://example.com/, |
||
304 | * it would return '[email protected]' |
||
305 | * |
||
306 | * If the configuration value 'mail_from_address' is set in |
||
307 | * config.php, this value will override the $user_part that |
||
308 | * is passed to this function |
||
309 | * @since 5.0.0 |
||
310 | */ |
||
311 | public static function getDefaultEmailAddress(string $user_part): string { |
||
312 | $config = \OC::$server->getConfig(); |
||
313 | $user_part = $config->getSystemValueString('mail_from_address', $user_part); |
||
314 | $host_name = self::getServerHostName(); |
||
315 | $host_name = $config->getSystemValueString('mail_domain', $host_name); |
||
316 | $defaultEmailAddress = $user_part.'@'.$host_name; |
||
317 | |||
318 | $mailer = \OC::$server->getMailer(); |
||
319 | if ($mailer->validateMailAddress($defaultEmailAddress)) { |
||
320 | return $defaultEmailAddress; |
||
321 | } |
||
322 | |||
323 | // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' |
||
324 | return $user_part.'@localhost.localdomain'; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Converts string to int of float depending if it fits an int |
||
329 | * @param numeric-string|float|int $number numeric string |
||
330 | * @return int|float int if it fits, float if it is too big |
||
331 | * @since 26.0.0 |
||
332 | */ |
||
333 | public static function numericToNumber(string|float|int $number): int|float { |
||
334 | /* This is a hack to cast to (int|float) */ |
||
335 | return 0 + (string)$number; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Make a human file size (2048 to 2 kB) |
||
340 | * @param int|float $bytes file size in bytes |
||
341 | * @return string a human readable file size |
||
342 | * @since 4.0.0 |
||
343 | */ |
||
344 | public static function humanFileSize(int|float $bytes): string { |
||
345 | return \OC_Helper::humanFileSize($bytes); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Make a computer file size (2 kB to 2048) |
||
350 | * @param string $str file size in a fancy format |
||
351 | * @return false|int|float a file size in bytes |
||
352 | * |
||
353 | * Inspired by: https://www.php.net/manual/en/function.filesize.php#92418 |
||
354 | * @since 4.0.0 |
||
355 | */ |
||
356 | public static function computerFileSize(string $str): false|int|float { |
||
357 | return \OC_Helper::computerFileSize($str); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * connects a function to a hook |
||
362 | * |
||
363 | * @param string $signalClass class name of emitter |
||
364 | * @param string $signalName name of signal |
||
365 | * @param string|object $slotClass class name of slot |
||
366 | * @param string $slotName name of slot |
||
367 | * @return bool |
||
368 | * |
||
369 | * This function makes it very easy to connect to use hooks. |
||
370 | * |
||
371 | * TODO: write example |
||
372 | * @since 4.0.0 |
||
373 | * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::addListener |
||
374 | */ |
||
375 | public static function connectHook($signalClass, $signalName, $slotClass, $slotName) { |
||
376 | return \OC_Hook::connect($signalClass, $signalName, $slotClass, $slotName); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Emits a signal. To get data from the slot use references! |
||
381 | * @param string $signalclass class name of emitter |
||
382 | * @param string $signalname name of signal |
||
383 | * @param array $params default: array() array with additional data |
||
384 | * @return bool true if slots exists or false if not |
||
385 | * |
||
386 | * TODO: write example |
||
387 | * @since 4.0.0 |
||
388 | * @deprecated 21.0.0 use \OCP\EventDispatcher\IEventDispatcher::dispatchTypedEvent |
||
389 | */ |
||
390 | public static function emitHook($signalclass, $signalname, $params = []) { |
||
391 | return \OC_Hook::emit($signalclass, $signalname, $params); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Cached encrypted CSRF token. Some static unit-tests of ownCloud compare |
||
396 | * multiple OC_Template elements which invoke `callRegister`. If the value |
||
397 | * would not be cached these unit-tests would fail. |
||
398 | * @var string |
||
399 | */ |
||
400 | private static $token = ''; |
||
401 | |||
402 | /** |
||
403 | * Register an get/post call. This is important to prevent CSRF attacks |
||
404 | * @since 4.5.0 |
||
405 | */ |
||
406 | public static function callRegister() { |
||
407 | if (self::$token === '') { |
||
408 | self::$token = \OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue(); |
||
409 | } |
||
410 | return self::$token; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Used to sanitize HTML |
||
415 | * |
||
416 | * This function is used to sanitize HTML and should be applied on any |
||
417 | * string or array of strings before displaying it on a web page. |
||
418 | * |
||
419 | * @param string|string[] $value |
||
420 | * @return string|string[] an array of sanitized strings or a single sanitized string, depends on the input parameter. |
||
421 | * @since 4.5.0 |
||
422 | */ |
||
423 | public static function sanitizeHTML($value) { |
||
424 | return \OC_Util::sanitizeHTML($value); |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Public function to encode url parameters |
||
429 | * |
||
430 | * This function is used to encode path to file before output. |
||
431 | * Encoding is done according to RFC 3986 with one exception: |
||
432 | * Character '/' is preserved as is. |
||
433 | * |
||
434 | * @param string $component part of URI to encode |
||
435 | * @return string |
||
436 | * @since 6.0.0 |
||
437 | */ |
||
438 | public static function encodePath($component) { |
||
439 | return \OC_Util::encodePath($component); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
||
444 | * |
||
445 | * @param array $input The array to work on |
||
446 | * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
||
447 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
||
448 | * @return array |
||
449 | * @since 4.5.0 |
||
450 | */ |
||
451 | public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
||
452 | return \OC_Helper::mb_array_change_key_case($input, $case, $encoding); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * performs a search in a nested array |
||
457 | * |
||
458 | * @param array $haystack the array to be searched |
||
459 | * @param string $needle the search string |
||
460 | * @param mixed $index optional, only search this key name |
||
461 | * @return mixed the key of the matching field, otherwise false |
||
462 | * @since 4.5.0 |
||
463 | * @deprecated 15.0.0 |
||
464 | */ |
||
465 | public static function recursiveArraySearch($haystack, $needle, $index = null) { |
||
466 | return \OC_Helper::recursiveArraySearch($haystack, $needle, $index); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * calculates the maximum upload size respecting system settings, free space and user quota |
||
471 | * |
||
472 | * @param string $dir the current folder where the user currently operates |
||
473 | * @param int|float|null $free the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
||
474 | * @return int|float number of bytes representing |
||
475 | * @since 5.0.0 |
||
476 | */ |
||
477 | public static function maxUploadFilesize(string $dir, int|float|null $free = null): int|float { |
||
478 | return \OC_Helper::maxUploadFilesize($dir, $free); |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Calculate free space left within user quota |
||
483 | * @param string $dir the current folder where the user currently operates |
||
484 | * @return int|float number of bytes representing |
||
485 | * @since 7.0.0 |
||
486 | */ |
||
487 | public static function freeSpace(string $dir): int|float { |
||
488 | return \OC_Helper::freeSpace($dir); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Calculate PHP upload limit |
||
493 | * |
||
494 | * @return int|float number of bytes representing |
||
495 | * @since 7.0.0 |
||
496 | */ |
||
497 | public static function uploadLimit(): int|float { |
||
498 | return \OC_Helper::uploadLimit(); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Returns whether the given file name is valid |
||
503 | * @param string $file file name to check |
||
504 | * @return bool true if the file name is valid, false otherwise |
||
505 | * @deprecated 8.1.0 use \OC\Files\View::verifyPath() |
||
506 | * @since 7.0.0 |
||
507 | * @suppress PhanDeprecatedFunction |
||
508 | */ |
||
509 | public static function isValidFileName($file) { |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Compare two strings to provide a natural sort |
||
515 | * @param string $a first string to compare |
||
516 | * @param string $b second string to compare |
||
517 | * @return int -1 if $b comes before $a, 1 if $a comes before $b |
||
518 | * or 0 if the strings are identical |
||
519 | * @since 7.0.0 |
||
520 | */ |
||
521 | public static function naturalSortCompare($a, $b) { |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Check if a password is required for each public link |
||
527 | * |
||
528 | * @param bool $checkGroupMembership Check group membership exclusion |
||
529 | * @return boolean |
||
530 | * @since 7.0.0 |
||
531 | */ |
||
532 | public static function isPublicLinkPasswordRequired(bool $checkGroupMembership = true) { |
||
533 | return \OC_Util::isPublicLinkPasswordRequired($checkGroupMembership); |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * check if share API enforces a default expire date |
||
538 | * @return boolean |
||
539 | * @since 8.0.0 |
||
540 | */ |
||
541 | public static function isDefaultExpireDateEnforced() { |
||
542 | return \OC_Util::isDefaultExpireDateEnforced(); |
||
543 | } |
||
544 | |||
545 | protected static $needUpgradeCache = null; |
||
546 | |||
547 | /** |
||
548 | * Checks whether the current version needs upgrade. |
||
549 | * |
||
550 | * @return bool true if upgrade is needed, false otherwise |
||
551 | * @since 7.0.0 |
||
552 | */ |
||
553 | public static function needUpgrade() { |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * Sometimes a string has to be shortened to fit within a certain maximum |
||
562 | * data length in bytes. substr() you may break multibyte characters, |
||
563 | * because it operates on single byte level. mb_substr() operates on |
||
564 | * characters, so does not ensure that the shortened string satisfies the |
||
565 | * max length in bytes. |
||
566 | * |
||
567 | * For example, json_encode is messing with multibyte characters a lot, |
||
568 | * replacing them with something along "\u1234". |
||
569 | * |
||
570 | * This function shortens the string with by $accuracy (-5) from |
||
571 | * $dataLength characters, until it fits within $dataLength bytes. |
||
572 | * |
||
573 | * @since 23.0.0 |
||
574 | */ |
||
575 | public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string { |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Check if a function is enabled in the php configuration |
||
586 | * |
||
587 | * @since 25.0.0 |
||
588 | */ |
||
589 | public static function isFunctionEnabled(string $functionName): bool { |
||
602 |