Conditions | 17 |
Paths | 4096 |
Total Lines | 53 |
Lines | 15 |
Ratio | 28.3 % |
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 |
||
42 | protected function _conditions($search) |
||
43 | { |
||
44 | View Code Duplication | if (!empty($search['limit_custom']) && $search['limit_custom'][0] == "P") { |
|
|
|||
45 | $search['limit'] = $search['limit_custom']; |
||
46 | } |
||
47 | $hasLimit = (!empty($search['limit']) && $search['limit'] != -1); |
||
48 | |||
49 | $conditions = array(); |
||
50 | View Code Duplication | if (!empty($search['date_start']) && !$hasLimit) { |
|
51 | $conditions['meta.request_date']['$gte'] = (string)$search['date_start']; |
||
52 | } |
||
53 | View Code Duplication | if (!empty($search['date_end']) && !$hasLimit) { |
|
54 | $conditions['meta.request_date']['$lte'] = (string)$search['date_end']; |
||
55 | } |
||
56 | if (isset($search['simple_url'])) { |
||
57 | $conditions['meta.simple_url'] = (string)$search['simple_url']; |
||
58 | } |
||
59 | View Code Duplication | if (!empty($search['request_start'])) { |
|
60 | $conditions['meta.SERVER.REQUEST_TIME']['$gte'] = $this->_convertDate($search['request_start']); |
||
61 | } |
||
62 | View Code Duplication | if (!empty($search['request_end'])) { |
|
63 | $conditions['meta.SERVER.REQUEST_TIME']['$lte'] = $this->_convertDate($search['request_end']); |
||
64 | } |
||
65 | |||
66 | if (!empty($search['remote_addr'])) { |
||
67 | $conditions['meta.SERVER.REMOTE_ADDR'] = (string)$search['remote_addr']; |
||
68 | } |
||
69 | if (isset($search['cookie'])) { |
||
70 | $conditions['meta.SERVER.HTTP_COOKIE'] = (string)$search['cookie']; |
||
71 | } |
||
72 | |||
73 | if ($hasLimit && $search['limit'][0] == "P") { |
||
74 | $date = new DateTime(); |
||
75 | try { |
||
76 | $date->sub(new DateInterval($search['limit'])); |
||
77 | $conditions['meta.request_ts']['$gte'] = new MongoDate($date->getTimestamp()); |
||
78 | } catch (\Exception $e) { |
||
79 | // Match a day in the future so we match nothing, as it's likely an invalid format |
||
80 | $conditions['meta.request_ts']['$gte'] = new MongoDate(time() + 86400); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | if (isset($search['url'])) { |
||
85 | // Not sure if letting people use regex here |
||
86 | // is a good idea. Only one way to find out. |
||
87 | $conditions['meta.url'] = array( |
||
88 | '$regex' => (string)$search['url'], |
||
89 | '$options' => 'i', |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | return $conditions; |
||
94 | } |
||
95 | |||
153 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.