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