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