Conditions | 53 |
Paths | 3288 |
Total Lines | 148 |
Code Lines | 110 |
Lines | 41 |
Ratio | 27.7 % |
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 |
||
58 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
59 | { |
||
60 | $this->data['queries'] = array(); |
||
61 | $this->data['nb_queries'] = 0; |
||
62 | |||
63 | $grouped = array(); |
||
64 | $ordered = array(); |
||
65 | foreach ($this->queries as $query) { |
||
66 | if (!isset($query['query']) || !isset($query['fields'])) { |
||
67 | // no grouping necessary |
||
68 | $ordered[] = array($query); |
||
69 | continue; |
||
70 | } |
||
71 | |||
72 | $cursor = serialize($query['query']).serialize($query['fields']); |
||
73 | |||
74 | // append if issued from cursor (currently just "sort") |
||
75 | if (isset($query['sort'])) { |
||
76 | unset($query['query'], $query['fields']); |
||
77 | $grouped[$cursor][count($grouped[$cursor]) - 1][] = $query; |
||
78 | } else { |
||
79 | $grouped[$cursor][] = array($query); |
||
80 | $ordered[] =& $grouped[$cursor][count($grouped[$cursor]) - 1]; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $i = 0; |
||
85 | $db = ''; |
||
86 | $query = ''; |
||
87 | foreach ($ordered as $logs) { |
||
88 | foreach ($logs as $log) { |
||
89 | if (isset($log['db']) && $db != $log['db']) { |
||
90 | // for readability |
||
91 | $this->data['queries'][$i++] = 'use '.$log['db'].';'; |
||
92 | $db = $log['db']; |
||
93 | } |
||
94 | |||
95 | if (isset($log['collection'])) { |
||
96 | // flush the previous and start a new query |
||
97 | View Code Duplication | if (!empty($query)) { |
|
98 | if ('.' == $query[0]) { |
||
99 | $query = 'db'.$query; |
||
100 | } |
||
101 | |||
102 | $this->data['queries'][$i++] = $query.';'; |
||
103 | ++$this->data['nb_queries']; |
||
104 | } |
||
105 | |||
106 | $query = 'db.'.$log['collection']; |
||
107 | } |
||
108 | |||
109 | // format the method call |
||
110 | if (isset($log['authenticate'])) { |
||
111 | $query .= '.authenticate()'; |
||
112 | } elseif (isset($log['batchInsert'])) { |
||
113 | if (1 === $log['num']) { |
||
114 | $query .= '.insert('.$this->bsonEncode($log['data']).')'; |
||
115 | } elseif (null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $log['num']) { |
||
116 | $query .= '.batchInsert(**'.$log['num'].' items**)'; |
||
117 | } else { |
||
118 | $query .= '.batchInsert('.$this->bsonEncode($log['data']).')'; |
||
119 | } |
||
120 | } elseif (isset($log['command'])) { |
||
121 | $query .= '.runCommand('.$this->bsonEncode($log['data']).')'; |
||
122 | } elseif (isset($log['count'])) { |
||
123 | $query .= '.count('; |
||
124 | if ($log['query'] || $log['limit'] || $log['skip']) { |
||
125 | $query .= $this->bsonEncode($log['query']); |
||
126 | View Code Duplication | if ($log['limit'] || $log['skip']) { |
|
127 | $query .= ', '.$this->bsonEncode($log['limit']); |
||
128 | if ($log['skip']) { |
||
129 | $query .= ', '.$this->bsonEncode($log['skip']); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | $query .= ')'; |
||
134 | } elseif (isset($log['skip'])) { |
||
135 | $query .= '.skip('.$log['skipNum'].')'; |
||
136 | } elseif (isset($log['limit'])) { |
||
137 | $query .= '.limit('.$log['limitNum'].')'; |
||
138 | } elseif (isset($log['createCollection'])) { |
||
139 | $query .= '.createCollection()'; |
||
140 | } elseif (isset($log['createDBRef'])) { |
||
141 | $query .= '.createDBRef()'; |
||
142 | } elseif (isset($log['deleteIndex'])) { |
||
143 | $query .= '.dropIndex('.$this->bsonEncode($log['keys']).')'; |
||
144 | } elseif (isset($log['deleteIndexes'])) { |
||
145 | $query .= '.dropIndexes()'; |
||
146 | } elseif (isset($log['drop'])) { |
||
147 | $query .= '.drop()'; |
||
148 | } elseif (isset($log['dropDatabase'])) { |
||
149 | $query .= '.dropDatabase()'; |
||
150 | View Code Duplication | } elseif (isset($log['ensureIndex'])) { |
|
151 | $query .= '.ensureIndex('.$this->bsonEncode($log['keys']).', '.$this->bsonEncode($log['options']).')'; |
||
152 | } elseif (isset($log['execute'])) { |
||
153 | $query .= '.execute()'; |
||
154 | } elseif (isset($log['find'])) { |
||
155 | $query .= '.find('; |
||
156 | View Code Duplication | if ($log['query'] || $log['fields']) { |
|
157 | $query .= $this->bsonEncode($log['query']); |
||
158 | if ($log['fields']) { |
||
159 | $query .= ', '.$this->bsonEncode($log['fields']); |
||
160 | } |
||
161 | } |
||
162 | $query .= ')'; |
||
163 | } elseif (isset($log['findOne'])) { |
||
164 | $query .= '.findOne('; |
||
165 | View Code Duplication | if ($log['query'] || $log['fields']) { |
|
166 | $query .= $this->bsonEncode($log['query']); |
||
167 | if ($log['fields']) { |
||
168 | $query .= ', '.$this->bsonEncode($log['fields']); |
||
169 | } |
||
170 | } |
||
171 | $query .= ')'; |
||
172 | } elseif (isset($log['getDBRef'])) { |
||
173 | $query .= '.getDBRef()'; |
||
174 | } elseif (isset($log['group'])) { |
||
175 | $query .= '.group('.$this->bsonEncode(array( |
||
176 | 'key' => $log['keys'], |
||
177 | 'initial' => $log['initial'], |
||
178 | 'reduce' => $log['reduce'], |
||
179 | )).')'; |
||
180 | } elseif (isset($log['insert'])) { |
||
181 | $query .= '.insert('.$this->bsonEncode($log['document']).')'; |
||
182 | } elseif (isset($log['remove'])) { |
||
183 | $query .= '.remove('.$this->bsonEncode($log['query']).')'; |
||
184 | } elseif (isset($log['save'])) { |
||
185 | $query .= '.save('.$this->bsonEncode($log['document']).')'; |
||
186 | } elseif (isset($log['sort'])) { |
||
187 | $query .= '.sort('.$this->bsonEncode($log['sortFields']).')'; |
||
188 | View Code Duplication | } elseif (isset($log['update'])) { |
|
189 | // todo: include $log['options'] |
||
190 | $query .= '.update('.$this->bsonEncode($log['query']).', '.$this->bsonEncode($log['newObj']).')'; |
||
191 | } elseif (isset($log['validate'])) { |
||
192 | $query .= '.validate()'; |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | View Code Duplication | if (!empty($query)) { |
|
198 | if ('.' == $query[0]) { |
||
199 | $query = 'db'.$query; |
||
200 | } |
||
201 | |||
202 | $this->data['queries'][$i++] = $query.';'; |
||
203 | ++$this->data['nb_queries']; |
||
204 | } |
||
205 | } |
||
206 | |||
275 |