Conditions | 8 |
Paths | 13 |
Total Lines | 51 |
Code Lines | 30 |
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 |
||
46 | public function search($query): array { |
||
47 | $cm = \OC::$server->getCommentsManager(); |
||
48 | $us = \OC::$server->getUserSession(); |
||
49 | |||
50 | $user = $us->getUser(); |
||
51 | if (!$user instanceof IUser) { |
||
52 | return []; |
||
53 | } |
||
54 | $uf = \OC::$server->getUserFolder($user->getUID()); |
||
55 | |||
56 | if ($uf === null) { |
||
57 | return []; |
||
58 | } |
||
59 | |||
60 | $result = []; |
||
61 | $numComments = 50; |
||
62 | $offset = 0; |
||
63 | |||
64 | while (count($result) < $numComments) { |
||
65 | /** @var IComment[] $comments */ |
||
66 | $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments); |
||
67 | |||
68 | foreach ($comments as $comment) { |
||
69 | if ($comment->getActorType() !== 'users') { |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | $displayName = $cm->resolveDisplayName('user', $comment->getActorId()); |
||
74 | |||
75 | try { |
||
76 | $file = $this->getFileForComment($uf, $comment); |
||
77 | $result[] = new Result($query, |
||
78 | $comment, |
||
79 | $displayName, |
||
80 | $file->getPath() |
||
81 | ); |
||
82 | } catch (NotFoundException $e) { |
||
83 | continue; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | if (count($comments) < $numComments) { |
||
88 | // Didn't find more comments when we tried to get, so there are no more comments. |
||
89 | return $result; |
||
90 | } |
||
91 | |||
92 | $offset += $numComments; |
||
93 | $numComments = 50 - count($result); |
||
94 | } |
||
95 | |||
96 | return $result; |
||
97 | } |
||
114 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: