Total Complexity | 44 |
Total Lines | 299 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 2 | Features | 1 |
Complex classes like ProxyDBExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProxyDBExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ProxyDBExtension extends Extension |
||
13 | { |
||
14 | const MAX_FIND_SOURCE_LEVEL = 3; |
||
15 | |||
16 | /** |
||
17 | * Store queries |
||
18 | * |
||
19 | * @var array<array<mixed>> |
||
20 | */ |
||
21 | protected static $queries = []; |
||
22 | |||
23 | /** |
||
24 | * Find source toggle (set by config find_source) |
||
25 | * |
||
26 | * @var ?bool |
||
27 | */ |
||
28 | protected static $findSource = null; |
||
29 | |||
30 | /** |
||
31 | * @param ProxyGenerator $proxy |
||
32 | * @return void |
||
33 | */ |
||
34 | public function updateProxy(ProxyGenerator &$proxy) |
||
35 | { |
||
36 | if (DebugBar::getDebugBar() === false) { |
||
|
|||
37 | return; |
||
38 | } |
||
39 | if (self::$findSource === null) { |
||
40 | self::$findSource = DebugBar::config()->get('find_source'); |
||
41 | } |
||
42 | |||
43 | // In the closure, $this is the proxied database |
||
44 | $callback = function ($args, $next) { |
||
45 | |||
46 | // The first argument is always the sql query |
||
47 | $sql = $args[0]; |
||
48 | $parameters = isset($args[2]) ? $args[2] : []; |
||
49 | |||
50 | // Sql can be an array |
||
51 | if (is_array($sql)) { |
||
52 | $parameters = $sql[1]; |
||
53 | $sql = $sql[0]; |
||
54 | } |
||
55 | |||
56 | // Inline sql |
||
57 | $sql = DB::inline_parameters($sql, $parameters); |
||
58 | |||
59 | // Get time and memory for the request |
||
60 | $startTime = microtime(true); |
||
61 | $startMemory = memory_get_usage(true); |
||
62 | |||
63 | // Execute all middleware |
||
64 | $handle = $next(...$args); |
||
65 | |||
66 | // Get time and memory after the request |
||
67 | $endTime = microtime(true); |
||
68 | $endMemory = memory_get_usage(true); |
||
69 | |||
70 | // Show query on screen |
||
71 | if (DebugBar::getShowQueries()) { |
||
72 | $formattedSql = DebugBarUtils::formatSql($sql); |
||
73 | $rows = $handle->numRecords(); |
||
74 | |||
75 | echo '<pre>The following query took <b>' . round($endTime - $startTime, 4) . '</b>s an returned <b>' . $rows . "</b> row(s) \n"; |
||
76 | echo 'Triggered by: <i>' . self::findSource() . '</i></pre>'; |
||
77 | echo $formattedSql; |
||
78 | |||
79 | // Preview results |
||
80 | $results = iterator_to_array($handle); |
||
81 | if ($rows > 0) { |
||
82 | if ($rows == 1) { |
||
83 | dump($results[0]); |
||
84 | } else { |
||
85 | $linearValues = count($results[0]); |
||
86 | if ($linearValues) { |
||
87 | dump(implode( |
||
88 | ',', |
||
89 | array_map( |
||
90 | function ($item) { |
||
91 | return $item[key($item)]; |
||
92 | }, |
||
93 | $results |
||
94 | ) |
||
95 | )); |
||
96 | } else { |
||
97 | if ($rows < 20) { |
||
98 | dump($results); |
||
99 | } else { |
||
100 | dump("Too many results to display"); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | echo '<hr/>'; |
||
106 | |||
107 | $handle->rewind(); // Rewind the results |
||
108 | } |
||
109 | |||
110 | // Sometimes, ugly spaces are there |
||
111 | $sql = preg_replace('/[[:blank:]]+/', ' ', trim($sql)); |
||
112 | |||
113 | // Sometimes, the select statement can be very long and unreadable |
||
114 | $shortsql = $sql; |
||
115 | $matches = null; |
||
116 | preg_match_all('/SELECT(.+?) FROM/is', $sql, $matches); |
||
117 | $select = empty($matches[1]) ? null : trim($matches[1][0]); |
||
118 | if ($select !== null) { |
||
119 | if (strlen($select) > 100) { |
||
120 | $shortsql = str_replace($select, '"ClickToShowFields"', $sql); |
||
121 | } else { |
||
122 | $select = null; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // null on the first query, since it's the select statement itself |
||
127 | $db = DB::get_conn()->getSelectedDatabase(); |
||
128 | |||
129 | self::$queries[] = [ |
||
130 | 'short_query' => $shortsql, |
||
131 | 'select' => $select, |
||
132 | 'query' => $sql, |
||
133 | 'start_time' => $startTime, |
||
134 | 'end_time' => $endTime, |
||
135 | 'duration' => $endTime - $startTime, |
||
136 | 'memory' => $endMemory - $startMemory, |
||
137 | 'rows' => $handle ? $handle->numRecords() : null, |
||
138 | 'success' => $handle ? true : false, |
||
139 | 'database' => $db, |
||
140 | 'source' => self::$findSource ? self::findSource() : null |
||
141 | ]; |
||
142 | |||
143 | return $handle; |
||
144 | }; |
||
145 | |||
146 | // Attach to benchmarkQuery to fire on both query and preparedQuery |
||
147 | $proxy = $proxy->addMethod('benchmarkQuery', $callback); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Reset queries array |
||
152 | * |
||
153 | * Helpful for long running process and avoid accumulating queries |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public static function resetQueries() |
||
158 | { |
||
159 | self::$queries = []; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return array<array<mixed>> |
||
164 | */ |
||
165 | public static function getQueries() |
||
166 | { |
||
167 | return self::$queries; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $str |
||
172 | * @return void |
||
173 | */ |
||
174 | public static function addCustomQuery(string $str) |
||
188 | ]; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | protected static function findSource() |
||
311 | } |
||
312 | } |
||
313 |