| Conditions | 9 |
| Paths | 8 |
| Total Lines | 167 |
| Code Lines | 108 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 58 | public function __construct(IRequest $request, $baseUri) { |
||
| 59 | $this->request = $request; |
||
| 60 | $this->baseUri = $baseUri; |
||
|
|
|||
| 61 | $logger = \OC::$server->getLogger(); |
||
| 62 | $mailer = \OC::$server->getMailer(); |
||
| 63 | $dispatcher = \OC::$server->getEventDispatcher(); |
||
| 64 | |||
| 65 | $root = new RootCollection(); |
||
| 66 | $this->server = new \OCA\DAV\Connector\Sabre\Server($root); |
||
| 67 | |||
| 68 | // Backends |
||
| 69 | $authBackend = new Auth( |
||
| 70 | \OC::$server->getSession(), |
||
| 71 | \OC::$server->getUserSession(), |
||
| 72 | \OC::$server->getRequest(), |
||
| 73 | \OC::$server->getTwoFactorAuthManager() |
||
| 74 | ); |
||
| 75 | |||
| 76 | // Set URL explicitly due to reverse-proxy situations |
||
| 77 | $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
||
| 78 | $this->server->setBaseUri($this->baseUri); |
||
| 79 | |||
| 80 | $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
||
| 81 | $authPlugin = new Plugin(); |
||
| 82 | $authPlugin->addBackend(new PublicAuth()); |
||
| 83 | $this->server->addPlugin($authPlugin); |
||
| 84 | |||
| 85 | // allow setup of additional auth backends |
||
| 86 | $event = new SabrePluginEvent($this->server); |
||
| 87 | $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
||
| 88 | |||
| 89 | // because we are throwing exceptions this plugin has to be the last one |
||
| 90 | $authPlugin->addBackend($authBackend); |
||
| 91 | |||
| 92 | // debugging |
||
| 93 | if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
||
| 94 | $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
||
| 95 | } else { |
||
| 96 | $this->server->addPlugin(new DummyGetResponsePlugin()); |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
||
| 100 | $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
||
| 101 | $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
||
| 102 | |||
| 103 | // acl |
||
| 104 | $acl = new DavAclPlugin(); |
||
| 105 | $acl->principalCollectionSet = [ |
||
| 106 | 'principals/users', 'principals/groups' |
||
| 107 | ]; |
||
| 108 | $acl->defaultUsernamePath = 'principals/users'; |
||
| 109 | $this->server->addPlugin($acl); |
||
| 110 | |||
| 111 | // calendar plugins |
||
| 112 | $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
||
| 113 | $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
||
| 114 | $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin()); |
||
| 115 | $this->server->addPlugin(new IMipPlugin($mailer, $logger)); |
||
| 116 | $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
||
| 117 | $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
||
| 118 | $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
||
| 119 | $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
||
| 120 | \OC::$server->getConfig(), |
||
| 121 | \OC::$server->getURLGenerator() |
||
| 122 | )); |
||
| 123 | |||
| 124 | // addressbook plugins |
||
| 125 | $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
||
| 126 | $this->server->addPlugin(new VCFExportPlugin()); |
||
| 127 | $this->server->addPlugin(new ImageExportPlugin(\OC::$server->getLogger())); |
||
| 128 | |||
| 129 | // system tags plugins |
||
| 130 | $this->server->addPlugin(new SystemTagPlugin( |
||
| 131 | \OC::$server->getSystemTagManager(), |
||
| 132 | \OC::$server->getGroupManager(), |
||
| 133 | \OC::$server->getUserSession() |
||
| 134 | )); |
||
| 135 | |||
| 136 | $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
||
| 137 | |||
| 138 | // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
||
| 139 | // we do not provide locking we emulate it using a fake locking plugin. |
||
| 140 | if($request->isUserAgent([ |
||
| 141 | '/WebDAVFS/', |
||
| 142 | '/Microsoft Office OneNote 2013/', |
||
| 143 | ])) { |
||
| 144 | $this->server->addPlugin(new FakeLockerPlugin()); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
||
| 148 | $this->server->addPlugin(new BrowserErrorPagePlugin()); |
||
| 149 | } |
||
| 150 | |||
| 151 | // wait with registering these until auth is handled and the filesystem is setup |
||
| 152 | $this->server->on('beforeMethod', function () use ($root) { |
||
| 153 | // custom properties plugin must be the last one |
||
| 154 | $userSession = \OC::$server->getUserSession(); |
||
| 155 | $user = $userSession->getUser(); |
||
| 156 | if (!is_null($user)) { |
||
| 157 | $view = \OC\Files\Filesystem::getView(); |
||
| 158 | $this->server->addPlugin( |
||
| 159 | new FilesPlugin( |
||
| 160 | $this->server->tree, |
||
| 161 | \OC::$server->getConfig(), |
||
| 162 | $this->request, |
||
| 163 | false, |
||
| 164 | !\OC::$server->getConfig()->getSystemValue('debug', false) |
||
| 165 | ) |
||
| 166 | ); |
||
| 167 | |||
| 168 | $this->server->addPlugin( |
||
| 169 | new \Sabre\DAV\PropertyStorage\Plugin( |
||
| 170 | new CustomPropertiesBackend( |
||
| 171 | $this->server->tree, |
||
| 172 | \OC::$server->getDatabaseConnection(), |
||
| 173 | \OC::$server->getUserSession()->getUser() |
||
| 174 | ) |
||
| 175 | ) |
||
| 176 | ); |
||
| 177 | if (!is_null($view)) { |
||
| 178 | $this->server->addPlugin( |
||
| 179 | new QuotaPlugin($view)); |
||
| 180 | } |
||
| 181 | $this->server->addPlugin( |
||
| 182 | new TagsPlugin( |
||
| 183 | $this->server->tree, \OC::$server->getTagManager() |
||
| 184 | ) |
||
| 185 | ); |
||
| 186 | // TODO: switch to LazyUserFolder |
||
| 187 | $userFolder = \OC::$server->getUserFolder(); |
||
| 188 | $this->server->addPlugin(new SharesPlugin( |
||
| 189 | $this->server->tree, |
||
| 190 | $userSession, |
||
| 191 | $userFolder, |
||
| 192 | \OC::$server->getShareManager() |
||
| 193 | )); |
||
| 194 | $this->server->addPlugin(new CommentPropertiesPlugin( |
||
| 195 | \OC::$server->getCommentsManager(), |
||
| 196 | $userSession |
||
| 197 | )); |
||
| 198 | if (!is_null($view)) { |
||
| 199 | $this->server->addPlugin(new FilesReportPlugin( |
||
| 200 | $this->server->tree, |
||
| 201 | $view, |
||
| 202 | \OC::$server->getSystemTagManager(), |
||
| 203 | \OC::$server->getSystemTagObjectMapper(), |
||
| 204 | \OC::$server->getTagManager(), |
||
| 205 | $userSession, |
||
| 206 | \OC::$server->getGroupManager(), |
||
| 207 | $userFolder |
||
| 208 | )); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | // register plugins from apps |
||
| 213 | $pluginManager = new PluginManager( |
||
| 214 | \OC::$server, |
||
| 215 | \OC::$server->getAppManager() |
||
| 216 | ); |
||
| 217 | foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
||
| 218 | $this->server->addPlugin($appPlugin); |
||
| 219 | } |
||
| 220 | foreach ($pluginManager->getAppCollections() as $appCollection) { |
||
| 221 | $root->addChild($appCollection); |
||
| 222 | } |
||
| 223 | }); |
||
| 224 | } |
||
| 225 | |||
| 230 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: