Conditions | 18 |
Paths | 4097 |
Total Lines | 57 |
Lines | 15 |
Ratio | 26.32 % |
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 |
||
31 | private function buildConditions(array $search): array |
||
32 | { |
||
33 | if (!$search) { |
||
|
|||
34 | return []; |
||
35 | } |
||
36 | |||
37 | View Code Duplication | if (!empty($search['limit_custom']) && $search['limit_custom'][0] === 'P') { |
|
38 | $search['limit'] = $search['limit_custom']; |
||
39 | } |
||
40 | $hasLimit = (!empty($search['limit']) && $search['limit'] != -1); |
||
41 | |||
42 | $conditions = []; |
||
43 | View Code Duplication | if (!empty($search['date_start']) && !$hasLimit) { |
|
44 | $conditions['meta.request_date']['$gte'] = (string)$search['date_start']; |
||
45 | } |
||
46 | View Code Duplication | if (!empty($search['date_end']) && !$hasLimit) { |
|
47 | $conditions['meta.request_date']['$lte'] = (string)$search['date_end']; |
||
48 | } |
||
49 | if (isset($search['simple_url'])) { |
||
50 | $conditions['meta.simple_url'] = (string)$search['simple_url']; |
||
51 | } |
||
52 | View Code Duplication | if (!empty($search['request_start'])) { |
|
53 | $conditions['meta.SERVER.REQUEST_TIME']['$gte'] = $this->convertDate($search['request_start']); |
||
54 | } |
||
55 | View Code Duplication | if (!empty($search['request_end'])) { |
|
56 | $conditions['meta.SERVER.REQUEST_TIME']['$lte'] = $this->convertDate($search['request_end']); |
||
57 | } |
||
58 | |||
59 | if (!empty($search['remote_addr'])) { |
||
60 | $conditions['meta.SERVER.REMOTE_ADDR'] = (string)$search['remote_addr']; |
||
61 | } |
||
62 | if (isset($search['cookie'])) { |
||
63 | $conditions['meta.SERVER.HTTP_COOKIE'] = (string)$search['cookie']; |
||
64 | } |
||
65 | |||
66 | if ($hasLimit && $search['limit'][0] === 'P') { |
||
67 | $date = new DateTime(); |
||
68 | try { |
||
69 | $date->sub(new DateInterval($search['limit'])); |
||
70 | $conditions['meta.request_ts']['$gte'] = new MongoDate($date->getTimestamp()); |
||
71 | } catch (\Exception $e) { |
||
72 | // Match a day in the future so we match nothing, as it's likely an invalid format |
||
73 | $conditions['meta.request_ts']['$gte'] = new MongoDate(time() + 86400); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if (isset($search['url'])) { |
||
78 | // Not sure if letting people use regex here |
||
79 | // is a good idea. Only one way to find out. |
||
80 | $conditions['meta.url'] = [ |
||
81 | '$regex' => (string)$search['url'], |
||
82 | '$options' => 'i', |
||
83 | ]; |
||
84 | } |
||
85 | |||
86 | return $conditions; |
||
87 | } |
||
88 | |||
148 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.