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:
Complex classes like OC_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 OC_Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class OC_Util { |
||
63 | public static $scripts = array(); |
||
64 | public static $styles = array(); |
||
65 | public static $headers = array(); |
||
66 | private static $rootMounted = false; |
||
67 | private static $fsSetup = false; |
||
68 | |||
69 | protected static function getAppManager() { |
||
72 | |||
73 | private static function initLocalStorageRootFS() { |
||
83 | |||
84 | /** |
||
85 | * mounting an object storage as the root fs will in essence remove the |
||
86 | * necessity of a data folder being present. |
||
87 | * TODO make home storage aware of this and use the object storage instead of local disk access |
||
88 | * |
||
89 | * @param array $config containing 'class' and optional 'arguments' |
||
90 | */ |
||
91 | private static function initObjectStoreRootFS($config) { |
||
112 | |||
113 | /** |
||
114 | * Can be set up |
||
115 | * |
||
116 | * @param string $user |
||
117 | * @return boolean |
||
118 | * @description configure the initial filesystem based on the configuration |
||
119 | */ |
||
120 | public static function setupFS($user = '') { |
||
121 | //setting up the filesystem twice can only lead to trouble |
||
122 | if (self::$fsSetup) { |
||
123 | return false; |
||
124 | } |
||
125 | |||
126 | \OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem'); |
||
127 | |||
128 | // If we are not forced to load a specific user we load the one that is logged in |
||
129 | if ($user === null) { |
||
130 | $user = ''; |
||
131 | } else if ($user == "" && OC_User::isLoggedIn()) { |
||
|
|||
132 | $user = OC_User::getUser(); |
||
133 | } |
||
134 | |||
135 | // load all filesystem apps before, so no setup-hook gets lost |
||
136 | OC_App::loadApps(array('filesystem')); |
||
137 | |||
138 | // the filesystem will finish when $user is not empty, |
||
139 | // mark fs setup here to avoid doing the setup from loading |
||
140 | // OC_Filesystem |
||
141 | if ($user != '') { |
||
142 | self::$fsSetup = true; |
||
143 | } |
||
144 | |||
145 | \OC\Files\Filesystem::initMountManager(); |
||
146 | |||
147 | \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
||
148 | \OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
||
149 | if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) { |
||
150 | /** @var \OC\Files\Storage\Common $storage */ |
||
151 | $storage->setMountOptions($mount->getOptions()); |
||
152 | } |
||
153 | return $storage; |
||
154 | }); |
||
155 | |||
156 | \OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
||
157 | if (!$mount->getOption('enable_sharing', true)) { |
||
158 | return new \OC\Files\Storage\Wrapper\PermissionsMask([ |
||
159 | 'storage' => $storage, |
||
160 | 'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE |
||
161 | ]); |
||
162 | } |
||
163 | return $storage; |
||
164 | }); |
||
165 | |||
166 | // install storage availability wrapper, before most other wrappers |
||
167 | \OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, $storage) { |
||
168 | /** @var \OCP\Files\Storage $storage */ |
||
169 | if (!$storage->instanceOfStorage('\OC\Files\Storage\Shared') && !$storage->isLocal()) { |
||
170 | return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]); |
||
171 | } |
||
172 | return $storage; |
||
173 | }); |
||
174 | |||
175 | \OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { |
||
176 | if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OC\Files\Storage\Shared') && !$storage->isLocal()) { |
||
177 | return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]); |
||
178 | } |
||
179 | return $storage; |
||
180 | }); |
||
181 | |||
182 | \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { |
||
183 | // set up quota for home storages, even for other users |
||
184 | // which can happen when using sharing |
||
185 | |||
186 | /** |
||
187 | * @var \OC\Files\Storage\Storage $storage |
||
188 | */ |
||
189 | if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
||
190 | || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
||
191 | ) { |
||
192 | /** @var \OC\Files\Storage\Home $storage */ |
||
193 | if (is_object($storage->getUser())) { |
||
194 | $user = $storage->getUser()->getUID(); |
||
195 | $quota = OC_Util::getUserQuota($user); |
||
196 | if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
||
197 | return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return $storage; |
||
203 | }); |
||
204 | |||
205 | OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); |
||
206 | \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true); |
||
207 | |||
208 | //check if we are using an object storage |
||
209 | $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null); |
||
210 | if (isset($objectStore)) { |
||
211 | self::initObjectStoreRootFS($objectStore); |
||
212 | } else { |
||
213 | self::initLocalStorageRootFS(); |
||
214 | } |
||
215 | |||
216 | if ($user != '' && !OCP\User::userExists($user)) { |
||
217 | \OC::$server->getEventLogger()->end('setup_fs'); |
||
218 | return false; |
||
219 | } |
||
220 | |||
221 | //if we aren't logged in, there is no use to set up the filesystem |
||
222 | if ($user != "") { |
||
223 | |||
224 | $userDir = '/' . $user . '/files'; |
||
225 | |||
226 | //jail the user into his "home" directory |
||
227 | \OC\Files\Filesystem::init($user, $userDir); |
||
228 | |||
229 | OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir)); |
||
230 | } |
||
231 | \OC::$server->getEventLogger()->end('setup_fs'); |
||
232 | return true; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * check if a password is required for each public link |
||
237 | * |
||
238 | * @return boolean |
||
239 | */ |
||
240 | public static function isPublicLinkPasswordRequired() { |
||
245 | |||
246 | /** |
||
247 | * check if sharing is disabled for the current user |
||
248 | * @param IConfig $config |
||
249 | * @param IGroupManager $groupManager |
||
250 | * @param IUser|null $user |
||
251 | * @return bool |
||
252 | */ |
||
253 | public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { |
||
274 | |||
275 | /** |
||
276 | * check if share API enforces a default expire date |
||
277 | * |
||
278 | * @return boolean |
||
279 | */ |
||
280 | public static function isDefaultExpireDateEnforced() { |
||
290 | |||
291 | /** |
||
292 | * Get the quota of a user |
||
293 | * |
||
294 | * @param string $userId |
||
295 | * @return int Quota bytes |
||
296 | */ |
||
297 | public static function getUserQuota($userId) { |
||
298 | $user = \OC::$server->getUserManager()->get($userId); |
||
299 | if (is_null($user)) { |
||
300 | return \OCP\Files\FileInfo::SPACE_UNLIMITED; |
||
301 | } |
||
302 | $userQuota = $user->getQuota(); |
||
303 | if($userQuota === 'none') { |
||
304 | return \OCP\Files\FileInfo::SPACE_UNLIMITED; |
||
305 | } |
||
306 | return OC_Helper::computerFileSize($userQuota); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * copies the skeleton to the users /files |
||
311 | * |
||
312 | * @param String $userId |
||
313 | * @param \OCP\Files\Folder $userDirectory |
||
314 | */ |
||
315 | public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { |
||
330 | |||
331 | /** |
||
332 | * copies a directory recursively by using streams |
||
333 | * |
||
334 | * @param string $source |
||
335 | * @param \OCP\Files\Folder $target |
||
336 | * @return void |
||
337 | */ |
||
338 | public static function copyr($source, \OCP\Files\Folder $target) { |
||
368 | |||
369 | /** |
||
370 | * @return void |
||
371 | */ |
||
372 | public static function tearDownFS() { |
||
377 | |||
378 | /** |
||
379 | * get the current installed version of ownCloud |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | public static function getVersion() { |
||
387 | |||
388 | /** |
||
389 | * get the current installed version string of ownCloud |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | public static function getVersionString() { |
||
397 | |||
398 | /** |
||
399 | * @description get the current installed edition of ownCloud. There is the community |
||
400 | * edition that just returns an empty string and the enterprise edition |
||
401 | * that returns "Enterprise". |
||
402 | * @return string |
||
403 | */ |
||
404 | public static function getEditionString() { |
||
412 | |||
413 | /** |
||
414 | * @description get the update channel of the current installed of ownCloud. |
||
415 | * @return string |
||
416 | */ |
||
417 | public static function getChannel() { |
||
421 | |||
422 | /** |
||
423 | * @description get the build number of the current installed of ownCloud. |
||
424 | * @return string |
||
425 | */ |
||
426 | public static function getBuild() { |
||
430 | |||
431 | /** |
||
432 | * @description load the version.php into the session as cache |
||
433 | */ |
||
434 | private static function loadVersion() { |
||
465 | |||
466 | /** |
||
467 | * generates a path for JS/CSS files. If no application is provided it will create the path for core. |
||
468 | * |
||
469 | * @param string $application application to get the files from |
||
470 | * @param string $directory directory within this application (css, js, vendor, etc) |
||
471 | * @param string $file the file inside of the above folder |
||
472 | * @return string the path |
||
473 | */ |
||
474 | private static function generatePath($application, $directory, $file) { |
||
485 | |||
486 | /** |
||
487 | * add a javascript file |
||
488 | * |
||
489 | * @param string $application application id |
||
490 | * @param string|null $file filename |
||
491 | * @param bool $prepend prepend the Script to the beginning of the list |
||
492 | * @return void |
||
493 | */ |
||
494 | public static function addScript($application, $file = null, $prepend = false) { |
||
503 | |||
504 | /** |
||
505 | * add a javascript file from the vendor sub folder |
||
506 | * |
||
507 | * @param string $application application id |
||
508 | * @param string|null $file filename |
||
509 | * @param bool $prepend prepend the Script to the beginning of the list |
||
510 | * @return void |
||
511 | */ |
||
512 | public static function addVendorScript($application, $file = null, $prepend = false) { |
||
516 | |||
517 | /** |
||
518 | * add a translation JS file |
||
519 | * |
||
520 | * @param string $application application id |
||
521 | * @param string $languageCode language code, defaults to the current language |
||
522 | * @param bool $prepend prepend the Script to the beginning of the list |
||
523 | */ |
||
524 | public static function addTranslations($application, $languageCode = null, $prepend = false) { |
||
535 | |||
536 | /** |
||
537 | * add a css file |
||
538 | * |
||
539 | * @param string $application application id |
||
540 | * @param string|null $file filename |
||
541 | * @param bool $prepend prepend the Style to the beginning of the list |
||
542 | * @return void |
||
543 | */ |
||
544 | public static function addStyle($application, $file = null, $prepend = false) { |
||
548 | |||
549 | /** |
||
550 | * add a css file from the vendor sub folder |
||
551 | * |
||
552 | * @param string $application application id |
||
553 | * @param string|null $file filename |
||
554 | * @param bool $prepend prepend the Style to the beginning of the list |
||
555 | * @return void |
||
556 | */ |
||
557 | public static function addVendorStyle($application, $file = null, $prepend = false) { |
||
561 | |||
562 | /** |
||
563 | * add an external resource css/js file |
||
564 | * |
||
565 | * @param string $application application id |
||
566 | * @param bool $prepend prepend the file to the beginning of the list |
||
567 | * @param string $path |
||
568 | * @param string $type (script or style) |
||
569 | * @return void |
||
570 | */ |
||
571 | private static function addExternalResource($application, $prepend, $path, $type = "script") { |
||
591 | |||
592 | /** |
||
593 | * Add a custom element to the header |
||
594 | * If $text is null then the element will be written as empty element. |
||
595 | * So use "" to get a closing tag. |
||
596 | * @param string $tag tag name of the element |
||
597 | * @param array $attributes array of attributes for the element |
||
598 | * @param string $text the text content for the element |
||
599 | */ |
||
600 | View Code Duplication | public static function addHeader($tag, $attributes, $text=null) { |
|
607 | |||
608 | /** |
||
609 | * formats a timestamp in the "right" way |
||
610 | * |
||
611 | * @param int $timestamp |
||
612 | * @param bool $dateOnly option to omit time from the result |
||
613 | * @param DateTimeZone|string $timeZone where the given timestamp shall be converted to |
||
614 | * @return string timestamp |
||
615 | * |
||
616 | * @deprecated Use \OC::$server->query('DateTimeFormatter') instead |
||
617 | */ |
||
618 | public static function formatDate($timestamp, $dateOnly = false, $timeZone = null) { |
||
630 | |||
631 | /** |
||
632 | * check if the current server configuration is suitable for ownCloud |
||
633 | * |
||
634 | * @param \OCP\IConfig $config |
||
635 | * @return array arrays with error messages and hints |
||
636 | */ |
||
637 | public static function checkServer(\OCP\IConfig $config) { |
||
889 | |||
890 | /** |
||
891 | * Check the database version |
||
892 | * |
||
893 | * @return array errors array |
||
894 | */ |
||
895 | public static function checkDatabaseVersion() { |
||
921 | |||
922 | /** |
||
923 | * Check for correct file permissions of data directory |
||
924 | * |
||
925 | * @param string $dataDirectory |
||
926 | * @return array arrays with error messages and hints |
||
927 | */ |
||
928 | public static function checkDataDirectoryPermissions($dataDirectory) { |
||
947 | |||
948 | /** |
||
949 | * Check that the data directory exists and is valid by |
||
950 | * checking the existence of the ".ocdata" file. |
||
951 | * |
||
952 | * @param string $dataDirectory data directory path |
||
953 | * @return array errors found |
||
954 | */ |
||
955 | public static function checkDataDirectoryValidity($dataDirectory) { |
||
973 | |||
974 | /** |
||
975 | * Check if the user is logged in, redirects to home if not. With |
||
976 | * redirect URL parameter to the request URI. |
||
977 | * |
||
978 | * @return void |
||
979 | */ |
||
980 | public static function checkLoggedIn() { |
||
998 | |||
999 | /** |
||
1000 | * Check if the user is a admin, redirects to home if not |
||
1001 | * |
||
1002 | * @return void |
||
1003 | */ |
||
1004 | public static function checkAdminUser() { |
||
1011 | |||
1012 | /** |
||
1013 | * Check if it is allowed to remember login. |
||
1014 | * |
||
1015 | * @note Every app can set 'rememberlogin' to 'false' to disable the remember login feature |
||
1016 | * |
||
1017 | * @return bool |
||
1018 | */ |
||
1019 | public static function rememberLoginAllowed() { |
||
1032 | |||
1033 | /** |
||
1034 | * Check if the user is a subadmin, redirects to home if not |
||
1035 | * |
||
1036 | * @return null|boolean $groups where the current user is subadmin |
||
1037 | */ |
||
1038 | public static function checkSubAdminUser() { |
||
1052 | |||
1053 | /** |
||
1054 | * Returns the URL of the default page |
||
1055 | * based on the system configuration and |
||
1056 | * the apps visible for the current user |
||
1057 | * |
||
1058 | * @return string URL |
||
1059 | */ |
||
1060 | public static function getDefaultPageUrl() { |
||
1091 | |||
1092 | /** |
||
1093 | * Redirect to the user default page |
||
1094 | * |
||
1095 | * @return void |
||
1096 | */ |
||
1097 | public static function redirectToDefaultPage() { |
||
1102 | |||
1103 | /** |
||
1104 | * get an id unique for this instance |
||
1105 | * |
||
1106 | * @return string |
||
1107 | */ |
||
1108 | public static function getInstanceId() { |
||
1117 | |||
1118 | /** |
||
1119 | * Public function to sanitize HTML |
||
1120 | * |
||
1121 | * This function is used to sanitize HTML and should be applied on any |
||
1122 | * string or array of strings before displaying it on a web page. |
||
1123 | * |
||
1124 | * @param string|array $value |
||
1125 | * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. |
||
1126 | */ |
||
1127 | public static function sanitizeHTML($value) { |
||
1138 | |||
1139 | /** |
||
1140 | * Public function to encode url parameters |
||
1141 | * |
||
1142 | * This function is used to encode path to file before output. |
||
1143 | * Encoding is done according to RFC 3986 with one exception: |
||
1144 | * Character '/' is preserved as is. |
||
1145 | * |
||
1146 | * @param string $component part of URI to encode |
||
1147 | * @return string |
||
1148 | */ |
||
1149 | public static function encodePath($component) { |
||
1154 | |||
1155 | |||
1156 | public function createHtaccessTestFile(\OCP\IConfig $config) { |
||
1181 | |||
1182 | /** |
||
1183 | * Check if the .htaccess file is working |
||
1184 | * @param \OCP\IConfig $config |
||
1185 | * @return bool |
||
1186 | * @throws Exception |
||
1187 | * @throws \OC\HintException If the test file can't get written. |
||
1188 | */ |
||
1189 | public function isHtaccessWorking(\OCP\IConfig $config) { |
||
1220 | |||
1221 | /** |
||
1222 | * Check if the setlocal call does not work. This can happen if the right |
||
1223 | * local packages are not available on the server. |
||
1224 | * |
||
1225 | * @return bool |
||
1226 | */ |
||
1227 | public static function isSetLocaleWorking() { |
||
1234 | |||
1235 | /** |
||
1236 | * Check if it's possible to get the inline annotations |
||
1237 | * |
||
1238 | * @return bool |
||
1239 | */ |
||
1240 | public static function isAnnotationsWorking() { |
||
1246 | |||
1247 | /** |
||
1248 | * Check if the PHP module fileinfo is loaded. |
||
1249 | * |
||
1250 | * @return bool |
||
1251 | */ |
||
1252 | public static function fileInfoLoaded() { |
||
1255 | |||
1256 | /** |
||
1257 | * clear all levels of output buffering |
||
1258 | * |
||
1259 | * @return void |
||
1260 | */ |
||
1261 | public static function obEnd() { |
||
1266 | |||
1267 | /** |
||
1268 | * Checks whether the server is running on Windows |
||
1269 | * |
||
1270 | * @return bool true if running on Windows, false otherwise |
||
1271 | */ |
||
1272 | public static function runningOnWindows() { |
||
1275 | |||
1276 | /** |
||
1277 | * Checks whether the server is running on Mac OS X |
||
1278 | * |
||
1279 | * @return bool true if running on Mac OS X, false otherwise |
||
1280 | */ |
||
1281 | public static function runningOnMac() { |
||
1284 | |||
1285 | /** |
||
1286 | * Checks whether server is running on HHVM |
||
1287 | * |
||
1288 | * @return bool True if running on HHVM, false otherwise |
||
1289 | */ |
||
1290 | public static function runningOnHhvm() { |
||
1293 | |||
1294 | /** |
||
1295 | * Handles the case that there may not be a theme, then check if a "default" |
||
1296 | * theme exists and take that one |
||
1297 | * |
||
1298 | * @return string the theme |
||
1299 | */ |
||
1300 | public static function getTheme() { |
||
1311 | |||
1312 | /** |
||
1313 | * Clear a single file from the opcode cache |
||
1314 | * This is useful for writing to the config file |
||
1315 | * in case the opcode cache does not re-validate files |
||
1316 | * Returns true if successful, false if unsuccessful: |
||
1317 | * caller should fall back on clearing the entire cache |
||
1318 | * with clearOpcodeCache() if unsuccessful |
||
1319 | * |
||
1320 | * @param string $path the path of the file to clear from the cache |
||
1321 | * @return bool true if underlying function returns true, otherwise false |
||
1322 | */ |
||
1323 | public static function deleteFromOpcodeCache($path) { |
||
1337 | |||
1338 | /** |
||
1339 | * Clear the opcode cache if one exists |
||
1340 | * This is necessary for writing to the config file |
||
1341 | * in case the opcode cache does not re-validate files |
||
1342 | * |
||
1343 | * @return void |
||
1344 | */ |
||
1345 | public static function clearOpcodeCache() { |
||
1367 | |||
1368 | /** |
||
1369 | * Normalize a unicode string |
||
1370 | * |
||
1371 | * @param string $value a not normalized string |
||
1372 | * @return bool|string |
||
1373 | */ |
||
1374 | public static function normalizeUnicode($value) { |
||
1387 | |||
1388 | /** |
||
1389 | * @param boolean|string $file |
||
1390 | * @return string |
||
1391 | */ |
||
1392 | public static function basename($file) { |
||
1397 | |||
1398 | /** |
||
1399 | * A human readable string is generated based on version, channel and build number |
||
1400 | * |
||
1401 | * @return string |
||
1402 | */ |
||
1403 | public static function getHumanVersion() { |
||
1411 | |||
1412 | /** |
||
1413 | * Returns whether the given file name is valid |
||
1414 | * |
||
1415 | * @param string $file file name to check |
||
1416 | * @return bool true if the file name is valid, false otherwise |
||
1417 | * @deprecated use \OC\Files\View::verifyPath() |
||
1418 | */ |
||
1419 | public static function isValidFileName($file) { |
||
1434 | |||
1435 | /** |
||
1436 | * Check whether the instance needs to perform an upgrade, |
||
1437 | * either when the core version is higher or any app requires |
||
1438 | * an upgrade. |
||
1439 | * |
||
1440 | * @param \OCP\IConfig $config |
||
1441 | * @return bool whether the core or any app needs an upgrade |
||
1442 | * @throws \OC\HintException When the upgrade from the given version is not allowed |
||
1443 | */ |
||
1444 | public static function needUpgrade(\OCP\IConfig $config) { |
||
1483 | |||
1484 | } |
||
1485 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.