Conditions | 10 |
Paths | 12 |
Total Lines | 47 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
136 | public function scan($dir = '') { |
||
137 | if (!Filesystem::isValidPath($dir)) { |
||
138 | throw new \InvalidArgumentException('Invalid path to scan'); |
||
139 | } |
||
140 | $mounts = $this->getMounts($dir); |
||
141 | foreach ($mounts as $mount) { |
||
142 | if (is_null($mount->getStorage())) { |
||
143 | continue; |
||
144 | } |
||
145 | $storage = $mount->getStorage(); |
||
146 | // if the home storage isn't writable then the scanner is run as the wrong user |
||
147 | if ($storage->instanceOfStorage('\OC\Files\Storage\Home') and |
||
148 | (!$storage->isCreatable('') or !$storage->isCreatable('files')) |
||
149 | ) { |
||
150 | throw new ForbiddenException(); |
||
151 | } |
||
152 | $relativePath = $mount->getInternalPath($dir); |
||
153 | $scanner = $storage->getScanner(); |
||
154 | $scanner->setUseTransactions(false); |
||
155 | $this->attachListener($mount); |
||
156 | $isDbLocking = \OC::$server->getLockingProvider() instanceof DBLockingProvider; |
||
157 | |||
158 | $scanner->listen('\OC\Files\Cache\Scanner', 'removeFromCache', function ($path) use ($storage) { |
||
159 | $this->triggerPropagator($storage, $path); |
||
160 | }); |
||
161 | $scanner->listen('\OC\Files\Cache\Scanner', 'updateCache', function ($path) use ($storage) { |
||
162 | $this->triggerPropagator($storage, $path); |
||
163 | }); |
||
164 | $scanner->listen('\OC\Files\Cache\Scanner', 'addToCache', function ($path) use ($storage) { |
||
165 | $this->triggerPropagator($storage, $path); |
||
166 | }); |
||
167 | |||
168 | if (!$isDbLocking) { |
||
169 | $this->db->beginTransaction(); |
||
170 | } |
||
171 | try { |
||
172 | $scanner->scan($relativePath, \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); |
||
173 | } catch (StorageNotAvailableException $e) { |
||
174 | $this->logger->error('Storage ' . $storage->getId() . ' not available'); |
||
175 | $this->logger->logException($e); |
||
176 | $this->emit('\OC\Files\Utils\Scanner', 'StorageNotAvailable', [$e]); |
||
177 | } |
||
178 | if (!$isDbLocking) { |
||
179 | $this->db->commit(); |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
189 |
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: