| Conditions | 6 |
| Paths | 4 |
| Total Lines | 106 |
| Lines | 8 |
| Ratio | 7.55 % |
| Changes | 0 | ||
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 |
||
| 93 | public function createServer($baseUri, |
||
| 94 | $requestUri, |
||
| 95 | BackendInterface $authBackend, |
||
| 96 | callable $viewCallBack) { |
||
| 97 | // Fire up server |
||
| 98 | $objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree(); |
||
| 99 | $server = new \OCA\DAV\Connector\Sabre\Server($objectTree); |
||
| 100 | // Set URL explicitly due to reverse-proxy situations |
||
| 101 | $server->httpRequest->setUrl($requestUri); |
||
| 102 | $server->setBaseUri($baseUri); |
||
| 103 | |||
| 104 | // Load plugins |
||
| 105 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\CorsPlugin($this->userSession)); |
||
| 106 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config)); |
||
| 107 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\ValidateRequestPlugin('webdav')); |
||
| 108 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config)); |
||
| 109 | $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend)); |
||
| 110 | // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to / |
||
| 111 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin()); |
||
| 112 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger)); |
||
| 113 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
||
| 114 | // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
||
| 115 | // we do not provide locking we emulate it using a fake locking plugin. |
||
| 116 | View Code Duplication | if ($this->request->isUserAgent([ |
|
|
|
|||
| 117 | '/WebDAVFS/', |
||
| 118 | '/OneNote/', |
||
| 119 | '/Microsoft Office OneNote 2013/', |
||
| 120 | '/Microsoft-WebDAV-MiniRedir/', |
||
| 121 | ])) { |
||
| 122 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin()); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) { |
||
| 126 | $server->addPlugin(new BrowserErrorPagePlugin()); |
||
| 127 | } |
||
| 128 | |||
| 129 | // wait with registering these until auth is handled and the filesystem is setup |
||
| 130 | $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) { |
||
| 131 | // ensure the skeleton is copied |
||
| 132 | // Try to obtain User Folder |
||
| 133 | $userFolder = \OC::$server->getUserFolder(); |
||
| 134 | |||
| 135 | /** @var \OC\Files\View $view */ |
||
| 136 | $view = $viewCallBack($server); |
||
| 137 | if ($userFolder !== null) { |
||
| 138 | // User folder exists and user is active and not anonymous |
||
| 139 | $rootInfo = $userFolder->getFileInfo(); |
||
| 140 | } else { |
||
| 141 | // User is anonymous or inactive, we need to get root info |
||
| 142 | $rootInfo = $view->getFileInfo(''); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Create ownCloud Root |
||
| 146 | if ($rootInfo->getType() === 'dir') { |
||
| 147 | $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree); |
||
| 148 | } else { |
||
| 149 | $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo); |
||
| 150 | } |
||
| 151 | |||
| 152 | $objectTree->init($root, $view, $this->mountManager); |
||
| 153 | |||
| 154 | $server->addPlugin( |
||
| 155 | new \OCA\DAV\Connector\Sabre\FilesPlugin( |
||
| 156 | $objectTree, |
||
| 157 | $this->config, |
||
| 158 | $this->request, |
||
| 159 | false, |
||
| 160 | !$this->config->getSystemValue('debug', false) |
||
| 161 | ) |
||
| 162 | ); |
||
| 163 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view)); |
||
| 164 | |||
| 165 | if ($this->userSession->isLoggedIn()) { |
||
| 166 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager)); |
||
| 167 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin( |
||
| 168 | $objectTree, |
||
| 169 | $this->userSession, |
||
| 170 | \OC::$server->getShareManager() |
||
| 171 | )); |
||
| 172 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession)); |
||
| 173 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin( |
||
| 174 | $objectTree, |
||
| 175 | $view, |
||
| 176 | \OC::$server->getSystemTagManager(), |
||
| 177 | \OC::$server->getSystemTagObjectMapper(), |
||
| 178 | \OC::$server->getTagManager(), |
||
| 179 | $this->userSession, |
||
| 180 | \OC::$server->getGroupManager(), |
||
| 181 | $userFolder |
||
| 182 | )); |
||
| 183 | |||
| 184 | // custom properties plugin must be the last one |
||
| 185 | $server->addPlugin( |
||
| 186 | new FileCustomPropertiesPlugin( |
||
| 187 | new FileCustomPropertiesBackend( |
||
| 188 | $objectTree, |
||
| 189 | $this->databaseConnection, |
||
| 190 | $this->userSession->getUser() |
||
| 191 | ) |
||
| 192 | ) |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin()); |
||
| 196 | }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request |
||
| 197 | return $server; |
||
| 198 | } |
||
| 199 | } |
||
| 200 |
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.