| Conditions | 13 |
| Paths | 1 |
| Total Lines | 105 |
| Code Lines | 62 |
| 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 |
||
| 30 | public function updateProxy(ProxyGenerator &$proxy) |
||
| 31 | { |
||
| 32 | self::$findSource = DebugBar::config()->get('find_source'); |
||
| 33 | |||
| 34 | // In the closure, $this is the proxied database |
||
| 35 | $callback = function ($args, $next) { |
||
| 36 | |||
| 37 | // The first argument is always the sql query |
||
| 38 | $sql = $args[0]; |
||
| 39 | $parameters = isset($args[2]) ? $args[2] : array(); |
||
| 40 | |||
| 41 | // Sql can be an array |
||
| 42 | // TODO: verify if it's still the case in SS4 |
||
| 43 | if (is_array($sql)) { |
||
| 44 | $parameters = $sql[1]; |
||
| 45 | $sql = $sql[0]; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Inline sql |
||
| 49 | $sql = DB::inline_parameters($sql, $parameters); |
||
| 50 | |||
| 51 | // Get time and memory for the request |
||
| 52 | $startTime = microtime(true); |
||
| 53 | $startMemory = memory_get_usage(true); |
||
| 54 | |||
| 55 | // Execute all middleware |
||
| 56 | $handle = $next(...$args); |
||
| 57 | |||
| 58 | // Get time and memory after the request |
||
| 59 | $endTime = microtime(true); |
||
| 60 | $endMemory = memory_get_usage(true); |
||
| 61 | |||
| 62 | // Show query on screen |
||
| 63 | if (DebugBar::getShowQueries()) { |
||
| 64 | $formattedSql = SqlFormatter::format($sql); |
||
| 65 | $rows = $handle->numRecords(); |
||
| 66 | |||
| 67 | echo '<pre>The following query took <b>' . round($endTime - $startTime, 4) . '</b>s an returned <b>' . $rows . "</b> row(s) \n"; |
||
| 68 | echo 'Triggered by: <i>' . self::findSource() . '</i></pre>'; |
||
| 69 | echo $formattedSql; |
||
| 70 | |||
| 71 | // Preview results |
||
| 72 | $results = iterator_to_array($handle); |
||
| 73 | if ($rows > 0) { |
||
| 74 | if ($rows == 1) { |
||
| 75 | dump($results[0]); |
||
| 76 | } else { |
||
| 77 | $linearValues = count($results[0]); |
||
| 78 | if ($linearValues) { |
||
| 79 | dump(implode( |
||
| 80 | ',', |
||
| 81 | array_map( |
||
| 82 | function ($item) { |
||
| 83 | return $item[key($item)]; |
||
| 84 | }, |
||
| 85 | $results |
||
| 86 | ) |
||
| 87 | )); |
||
| 88 | } else { |
||
| 89 | if ($rows < 20) { |
||
| 90 | dump($results); |
||
| 91 | } else { |
||
| 92 | dump("Too many results to display"); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | echo '<hr/>'; |
||
| 98 | |||
| 99 | $handle->rewind(); // Rewind the results |
||
| 100 | } |
||
| 101 | |||
| 102 | // Sometimes, ugly spaces are there |
||
| 103 | $sql = preg_replace('/[[:blank:]]+/', ' ', trim($sql)); |
||
| 104 | |||
| 105 | // Sometimes, the select statement can be very long and unreadable |
||
| 106 | $shortsql = $sql; |
||
| 107 | $matches = null; |
||
| 108 | preg_match_all('/SELECT(.+?) FROM/is', $sql, $matches); |
||
| 109 | $select = empty($matches[1]) ? null : trim($matches[1][0]); |
||
| 110 | if (strlen($select) > 100) { |
||
|
|
|||
| 111 | $shortsql = str_replace($select, '"ClickToShowFields"', $sql); |
||
| 112 | } else { |
||
| 113 | $select = null; |
||
| 114 | } |
||
| 115 | |||
| 116 | self::$queries[] = array( |
||
| 117 | 'short_query' => $shortsql, |
||
| 118 | 'select' => $select, |
||
| 119 | 'query' => $sql, |
||
| 120 | 'start_time' => $startTime, |
||
| 121 | 'end_time' => $endTime, |
||
| 122 | 'duration' => $endTime - $startTime, |
||
| 123 | 'memory' => $endMemory - $startMemory, |
||
| 124 | 'rows' => $handle ? $handle->numRecords() : null, |
||
| 125 | 'success' => $handle ? true : false, |
||
| 126 | 'database' => $this->getSelectedDatabase(), |
||
| 127 | 'source' => self::$findSource ? self::findSource() : null |
||
| 128 | ); |
||
| 129 | |||
| 130 | return $handle; |
||
| 131 | }; |
||
| 132 | |||
| 133 | // Attach to benchmarkQuery to fire on both query and preparedQuery |
||
| 134 | $proxy = $proxy->addMethod('benchmarkQuery', $callback); |
||
| 135 | } |
||
| 262 |