Conditions | 17 |
Paths | 35 |
Total Lines | 69 |
Code Lines | 41 |
Lines | 14 |
Ratio | 20.29 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
58 | function removeSharedFolder($mkdirs = true, $chunkSize = 99) { |
||
59 | $query = OCP\DB::prepare('SELECT * FROM `*PREFIX*share`'); |
||
60 | $result = $query->execute(); |
||
61 | $view = new \OC\Files\View('/'); |
||
62 | $users = array(); |
||
63 | $shares = array(); |
||
64 | //we need to set up user backends |
||
65 | OC_User::useBackend(new OC_User_Database()); |
||
66 | OC_Group::useBackend(new OC_Group_Database()); |
||
67 | OC_App::loadApps(array('authentication')); |
||
68 | //we need to set up user backends, otherwise creating the shares will fail with "because user does not exist" |
||
69 | while ($row = $result->fetchRow()) { |
||
70 | //collect all user shares |
||
71 | if ((int)$row['share_type'] === 0 && ($row['item_type'] === 'file' || $row['item_type'] === 'folder')) { |
||
72 | $users[] = $row['share_with']; |
||
73 | $shares[$row['id']] = $row['file_target']; |
||
74 | } else if ((int)$row['share_type'] === 1 && ($row['item_type'] === 'file' || $row['item_type'] === 'folder')) { |
||
75 | //collect all group shares |
||
76 | $users = array_merge($users, \OC_group::usersInGroup($row['share_with'])); |
||
77 | $shares[$row['id']] = $row['file_target']; |
||
78 | } else if ((int)$row['share_type'] === 2) { |
||
79 | $shares[$row['id']] = $row['file_target']; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | $unique_users = array_unique($users); |
||
84 | |||
85 | if (!empty($unique_users) && !empty($shares)) { |
||
86 | |||
87 | // create folder Shared for each user |
||
88 | |||
89 | if ($mkdirs) { |
||
90 | $logger = \OC::$server->getLogger(); |
||
91 | foreach ($unique_users as $user) { |
||
92 | try { |
||
93 | \OC\Files\Filesystem::initMountPoints($user); |
||
94 | } catch(\OC\User\NoUserException $e) { |
||
95 | $logger->warning("Update: removeSharedFolder - user '$user' is not present anymore" , array('app' => 'files_sharing')); |
||
96 | continue; |
||
97 | } |
||
98 | if (!$view->file_exists('/' . $user . '/files/Shared')) { |
||
99 | $view->mkdir('/' . $user . '/files/Shared'); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | $chunkedShareList = array_chunk($shares, $chunkSize, true); |
||
105 | $connection = \OC_DB::getConnection(); |
||
106 | |||
107 | View Code Duplication | foreach ($chunkedShareList as $subList) { |
|
108 | |||
109 | $statement = "UPDATE `*PREFIX*share` SET `file_target` = CASE `id` "; |
||
110 | //update share table |
||
111 | $ids = implode(',', array_keys($subList)); |
||
112 | foreach ($subList as $id => $target) { |
||
113 | $statement .= "WHEN " . $connection->quote($id, \PDO::PARAM_INT) . " THEN " . $connection->quote('/Shared' . $target, \PDO::PARAM_STR); |
||
114 | } |
||
115 | $statement .= ' END WHERE `id` IN (' . $ids . ')'; |
||
116 | |||
117 | $query = OCP\DB::prepare($statement); |
||
118 | |||
119 | $query->execute(array()); |
||
120 | } |
||
121 | |||
122 | // set config to keep the Shared folder as the default location for new shares |
||
123 | \OCA\Files_Sharing\Helper::setShareFolder('/Shared'); |
||
124 | |||
125 | } |
||
126 | } |
||
127 |
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.