| Conditions | 8 |
| Paths | 13 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 39 | public function search($query): array { |
||
| 40 | $cm = \OC::$server->getCommentsManager(); |
||
| 41 | $us = \OC::$server->getUserSession(); |
||
| 42 | |||
| 43 | $user = $us->getUser(); |
||
| 44 | if (!$user instanceof IUser) { |
||
| 45 | return []; |
||
| 46 | } |
||
| 47 | $uf = \OC::$server->getUserFolder($user->getUID()); |
||
| 48 | |||
| 49 | if ($uf === null) { |
||
| 50 | return []; |
||
| 51 | } |
||
| 52 | |||
| 53 | $result = []; |
||
| 54 | $numComments = 50; |
||
| 55 | $offset = 0; |
||
| 56 | |||
| 57 | while (\count($result) < $numComments) { |
||
| 58 | /** @var IComment[] $comments */ |
||
| 59 | $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments); |
||
| 60 | |||
| 61 | foreach ($comments as $comment) { |
||
| 62 | if ($comment->getActorType() !== 'users') { |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | $displayName = $cm->resolveDisplayName('user', $comment->getActorId()); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $file = $this->getFileForComment($uf, $comment); |
||
| 70 | $result[] = new Result($query, |
||
| 71 | $comment, |
||
| 72 | $displayName, |
||
| 73 | $file->getPath() |
||
| 74 | ); |
||
| 75 | } catch (NotFoundException $e) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if (\count($comments) < $numComments) { |
||
| 81 | // Didn't find more comments when we tried to get, so there are no more comments. |
||
| 82 | return $result; |
||
| 83 | } |
||
| 84 | |||
| 85 | $offset += $numComments; |
||
| 86 | $numComments = 50 - \count($result); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $result; |
||
| 90 | } |
||
| 91 | |||
| 107 |