| Conditions | 15 |
| Paths | 43 |
| Total Lines | 71 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 165 | public function scan($dir = '') { |
||
| 166 | if (!Filesystem::isValidPath($dir)) { |
||
| 167 | throw new \InvalidArgumentException('Invalid path to scan'); |
||
| 168 | } |
||
| 169 | $mounts = $this->getMounts($dir); |
||
| 170 | foreach ($mounts as $mount) { |
||
| 171 | $storage = $mount->getStorage(); |
||
| 172 | if (is_null($storage)) { |
||
| 173 | continue; |
||
| 174 | } |
||
| 175 | |||
| 176 | // don't bother scanning failed storages (shortcut for same result) |
||
| 177 | if ($storage->instanceOfStorage('OC\Files\Storage\FailedStorage')) { |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | |||
| 181 | // if the home storage isn't writable then the scanner is run as the wrong user |
||
| 182 | if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and |
||
| 183 | (!$storage->isCreatable('') or !$storage->isCreatable('files')) |
||
| 184 | ) { |
||
| 185 | if ($storage->file_exists('') or $storage->getCache()->inCache('')) { |
||
| 186 | throw new ForbiddenException(); |
||
| 187 | } else {// if the root exists in neither the cache nor the storage the user isn't setup yet |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | // don't scan received local shares, these can be scanned when scanning the owner's storage |
||
| 194 | if ($storage->instanceOfStorage(SharedStorage::class)) { |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | $relativePath = $mount->getInternalPath($dir); |
||
| 198 | $scanner = $storage->getScanner(); |
||
| 199 | $scanner->setUseTransactions(false); |
||
| 200 | $this->attachListener($mount); |
||
| 201 | $isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider; |
||
| 202 | |||
| 203 | $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) { |
||
| 204 | $this->triggerPropagator($storage, $path); |
||
| 205 | }); |
||
| 206 | $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) { |
||
| 207 | $this->triggerPropagator($storage, $path); |
||
| 208 | }); |
||
| 209 | $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) { |
||
| 210 | $this->triggerPropagator($storage, $path); |
||
| 211 | }); |
||
| 212 | |||
| 213 | if (!$isDbLocking) { |
||
| 214 | $this->db->beginTransaction(); |
||
| 215 | } |
||
| 216 | try { |
||
| 217 | $propagator = $storage->getPropagator(); |
||
| 218 | $propagator->beginBatch(); |
||
| 219 | $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); |
||
| 220 | $cache = $storage->getCache(); |
||
| 221 | if ($cache instanceof Cache) { |
||
| 222 | // only re-calculate for the root folder we scanned, anything below that is taken care of by the scanner |
||
| 223 | $cache->correctFolderSize($relativePath); |
||
| 224 | } |
||
| 225 | $propagator->commitBatch(); |
||
| 226 | } catch (StorageNotAvailableException $e) { |
||
| 227 | $this->logger->error('Storage ' . $storage->getId() . ' not available'); |
||
| 228 | $this->logger->logException($e); |
||
| 229 | $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]); |
||
| 230 | } |
||
| 231 | if (!$isDbLocking) { |
||
| 232 | $this->db->commit(); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 242 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: