Conditions | 19 |
Paths | 648 |
Total Lines | 117 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
90 | protected function collectData(?TimeDataCollector $timeCollector = null) |
||
91 | { |
||
92 | $stmts = []; |
||
93 | |||
94 | $total_duration = 0; |
||
95 | $total_mem = 0; |
||
96 | |||
97 | $failed = 0; |
||
98 | |||
99 | $i = 0; |
||
100 | |||
101 | // Get queries gathered by proxy |
||
102 | $queries = ProxyDBExtension::getQueries(); |
||
103 | |||
104 | $limit = DebugBar::config()->get('query_limit'); |
||
105 | $warnDurationThreshold = DebugBar::config()->get('warn_dbqueries_threshold_seconds'); |
||
106 | |||
107 | // only show db if there is more than one database in use |
||
108 | $showDb = count(array_filter(array_unique(array_map(function ($stmt) { |
||
|
|||
109 | return $stmt['database']; |
||
110 | }, $queries)))) > 1; |
||
111 | |||
112 | $showDb = false; |
||
113 | $hasUnclosedTransaction = false; |
||
114 | foreach ($queries as $stmt) { |
||
115 | $i++; |
||
116 | |||
117 | $total_duration += $stmt['duration']; |
||
118 | $total_mem += $stmt['memory']; |
||
119 | |||
120 | if (!$stmt['success']) { |
||
121 | $failed++; |
||
122 | } |
||
123 | |||
124 | if (str_starts_with((string) $stmt['short_query'], 'SHOW DATABASES LIKE')) { |
||
125 | $showDb = true; |
||
126 | } |
||
127 | |||
128 | if (str_contains((string) $stmt['short_query'], 'START TRANSACTION')) { |
||
129 | $hasUnclosedTransaction = true; |
||
130 | } |
||
131 | if (str_contains((string) $stmt['short_query'], 'END TRANSACTION')) { |
||
132 | $hasUnclosedTransaction = false; |
||
133 | } |
||
134 | |||
135 | if ($limit && $i > $limit) { |
||
136 | $stmts[] = [ |
||
137 | 'sql' => "Only the first $limit queries are shown" |
||
138 | ]; |
||
139 | break; |
||
140 | } |
||
141 | |||
142 | $stmts[] = [ |
||
143 | 'sql' => $stmt['short_query'], |
||
144 | 'row_count' => $stmt['rows'], |
||
145 | 'params' => $stmt['select'] ? $stmt['select'] : null, |
||
146 | 'duration' => $stmt['duration'], |
||
147 | 'duration_str' => $this->getDataFormatter()->formatDuration($stmt['duration']), |
||
148 | 'memory' => $stmt['memory'], |
||
149 | 'memory_str' => $this->getDataFormatter()->formatBytes($stmt['memory']), |
||
150 | 'is_success' => $stmt['success'], |
||
151 | 'database' => $showDb ? $stmt['database'] : null, |
||
152 | 'source' => $stmt['source'], |
||
153 | 'warn' => $stmt['duration'] > $warnDurationThreshold |
||
154 | ]; |
||
155 | |||
156 | if ($timeCollector !== null) { |
||
157 | $timeCollector->addMeasure( |
||
158 | $stmt['short_query'], |
||
159 | $stmt['start_time'], |
||
160 | $stmt['end_time'] |
||
161 | ); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // Save as CSV |
||
166 | $db_save_csv = DebugBar::config()->get('db_save_csv'); |
||
167 | if ($db_save_csv && !empty($queries)) { |
||
168 | $filename = date('Ymd_His') . '_' . count($queries) . '_' . uniqid() . '.csv'; |
||
169 | $isOutput = false; |
||
170 | if (isset($_REQUEST['downloadqueries']) && Director::isDev()) { |
||
171 | $isOutput = true; |
||
172 | if (headers_sent()) { |
||
173 | die('Cannot download queries, headers are already sent'); |
||
174 | } |
||
175 | header('Content-Type: text/csv'); |
||
176 | header('Content-Disposition: attachment;filename=' . $filename); |
||
177 | $fp = fopen('php://output', 'w'); |
||
178 | } else { |
||
179 | $tempFolder = TEMP_FOLDER . '/debugbar/db'; |
||
180 | if (!is_dir($tempFolder)) { |
||
181 | mkdir($tempFolder, 0755, true); |
||
182 | } |
||
183 | $fp = fopen($tempFolder . '/' . $filename, 'w'); |
||
184 | } |
||
185 | $headers = array_keys($queries[0]); |
||
186 | fputcsv($fp, $headers); |
||
187 | foreach ($queries as $query) { |
||
188 | fputcsv($fp, $query); |
||
189 | } |
||
190 | fclose($fp); |
||
191 | |||
192 | if ($isOutput) { |
||
193 | die(); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | return [ |
||
198 | 'nb_statements' => count($queries), |
||
199 | 'nb_failed_statements' => $failed, |
||
200 | 'show_db' => $showDb, |
||
201 | 'has_unclosed_transaction' => $hasUnclosedTransaction, |
||
202 | 'statements' => $stmts, |
||
203 | 'accumulated_duration' => $total_duration, |
||
204 | 'accumulated_duration_str' => $this->getDataFormatter()->formatDuration($total_duration), |
||
205 | 'memory_usage' => $total_mem, |
||
206 | 'memory_usage_str' => $this->getDataFormatter()->formatBytes($total_mem), |
||
207 | ]; |
||
250 |