Total Complexity | 41 |
Total Lines | 273 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 2 | Features | 0 |
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 boolean |
||
27 | */ |
||
28 | protected static $findSource = true; |
||
29 | |||
30 | /** |
||
31 | * @param ProxyGenerator $proxy |
||
32 | * @return void |
||
33 | */ |
||
34 | public function updateProxy(ProxyGenerator &$proxy) |
||
35 | { |
||
36 | self::$findSource = DebugBar::config()->get('find_source'); |
||
37 | |||
38 | // In the closure, $this is the proxied database |
||
39 | $callback = function ($args, $next) { |
||
40 | |||
41 | // The first argument is always the sql query |
||
42 | $sql = $args[0]; |
||
43 | $parameters = isset($args[2]) ? $args[2] : []; |
||
44 | |||
45 | // Sql can be an array |
||
46 | if (is_array($sql)) { |
||
47 | $parameters = $sql[1]; |
||
48 | $sql = $sql[0]; |
||
49 | } |
||
50 | |||
51 | // Inline sql |
||
52 | $sql = DB::inline_parameters($sql, $parameters); |
||
53 | |||
54 | // Get time and memory for the request |
||
55 | $startTime = microtime(true); |
||
56 | $startMemory = memory_get_usage(true); |
||
57 | |||
58 | // Execute all middleware |
||
59 | $handle = $next(...$args); |
||
60 | |||
61 | // Get time and memory after the request |
||
62 | $endTime = microtime(true); |
||
63 | $endMemory = memory_get_usage(true); |
||
64 | |||
65 | // Show query on screen |
||
66 | if (DebugBar::getShowQueries()) { |
||
67 | $formattedSql = SqlFormatter::format($sql); |
||
68 | $rows = $handle->numRecords(); |
||
69 | |||
70 | echo '<pre>The following query took <b>' . round($endTime - $startTime, 4) . '</b>s an returned <b>' . $rows . "</b> row(s) \n"; |
||
71 | echo 'Triggered by: <i>' . self::findSource() . '</i></pre>'; |
||
72 | echo $formattedSql; |
||
73 | |||
74 | // Preview results |
||
75 | $results = iterator_to_array($handle); |
||
76 | if ($rows > 0) { |
||
77 | if ($rows == 1) { |
||
78 | dump($results[0]); |
||
79 | } else { |
||
80 | $linearValues = count($results[0]); |
||
81 | if ($linearValues) { |
||
82 | dump(implode( |
||
83 | ',', |
||
84 | array_map( |
||
85 | function ($item) { |
||
86 | return $item[key($item)]; |
||
87 | }, |
||
88 | $results |
||
89 | ) |
||
90 | )); |
||
91 | } else { |
||
92 | if ($rows < 20) { |
||
93 | dump($results); |
||
94 | } else { |
||
95 | dump("Too many results to display"); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | echo '<hr/>'; |
||
101 | |||
102 | $handle->rewind(); // Rewind the results |
||
103 | } |
||
104 | |||
105 | // Sometimes, ugly spaces are there |
||
106 | $sql = preg_replace('/[[:blank:]]+/', ' ', trim($sql)); |
||
107 | |||
108 | // Sometimes, the select statement can be very long and unreadable |
||
109 | $shortsql = $sql; |
||
110 | $matches = null; |
||
111 | preg_match_all('/SELECT(.+?) FROM/is', $sql, $matches); |
||
112 | $select = empty($matches[1]) ? null : trim($matches[1][0]); |
||
113 | if ($select !== null) { |
||
114 | if (strlen($select) > 100) { |
||
115 | $shortsql = str_replace($select, '"ClickToShowFields"', $sql); |
||
116 | } else { |
||
117 | $select = null; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // null on the first query, since it's the select statement itself |
||
122 | $db = DB::get_conn()->getSelectedDatabase(); |
||
123 | |||
124 | self::$queries[] = array( |
||
125 | 'short_query' => $shortsql, |
||
126 | 'select' => $select, |
||
127 | 'query' => $sql, |
||
128 | 'start_time' => $startTime, |
||
129 | 'end_time' => $endTime, |
||
130 | 'duration' => $endTime - $startTime, |
||
131 | 'memory' => $endMemory - $startMemory, |
||
132 | 'rows' => $handle ? $handle->numRecords() : null, |
||
133 | 'success' => $handle ? true : false, |
||
134 | 'database' => $db, |
||
135 | 'source' => self::$findSource ? self::findSource() : null |
||
136 | ); |
||
137 | |||
138 | return $handle; |
||
139 | }; |
||
140 | |||
141 | // Attach to benchmarkQuery to fire on both query and preparedQuery |
||
142 | $proxy = $proxy->addMethod('benchmarkQuery', $callback); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Reset queries array |
||
147 | * |
||
148 | * Helpful for long running process and avoid accumulating queries |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | public static function resetQueries() |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return array<array<mixed>> |
||
159 | */ |
||
160 | public static function getQueries() |
||
161 | { |
||
162 | return self::$queries; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | protected static function findSource() |
||
285 | } |
||
286 | } |
||
287 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths