Conditions | 11 |
Paths | 30 |
Total Lines | 47 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
29 | public function search(SearchQuery $search) |
||
30 | { |
||
31 | $result = []; |
||
32 | $words = $search->getWords(); |
||
33 | $start = strlen($this->getBasePath()) + 1; |
||
34 | $length = -strlen($this->extension) - 1; |
||
35 | |||
36 | foreach ($this->getIterator() as $file) { |
||
37 | if (!$file->isFile() || $file->getExtension() !== $this->extension) { |
||
38 | continue; |
||
39 | } |
||
40 | |||
41 | $id = substr($file->getPathname(), $start, $length); |
||
42 | |||
43 | foreach ($words as $word) { |
||
44 | if (strpos($id, $word) === false) { |
||
45 | continue; |
||
46 | } |
||
47 | } |
||
48 | |||
49 | $result[$id] = $this->parse(file_get_contents($file->getPathname())); |
||
50 | } |
||
51 | |||
52 | $sort = $search->getSort(); |
||
53 | |||
54 | if ($sort) { |
||
|
|||
55 | uasort($result, function ($a, $b) use ($sort) { |
||
56 | if ($a[$sort] === $b[$sort]) { |
||
57 | return 0; |
||
58 | } |
||
59 | |||
60 | return ($a[$sort] < $b[$sort]) ? -1 : 1; |
||
61 | }); |
||
62 | |||
63 | if ($search->getDirection() === 'DESC') { |
||
64 | $result = array_reverse($result, true); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | if ($search->getPage() !== null) { |
||
69 | $offset = ($search->getPage() * 50) - 50; |
||
70 | |||
71 | $result = array_slice($result, $offset, 50, true); |
||
72 | } |
||
73 | |||
74 | return $result; |
||
75 | } |
||
76 | |||
166 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: