| Conditions | 16 | 
| Paths | 3 | 
| Total Lines | 114 | 
| Code Lines | 67 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 6 | ||
| Bugs | 2 | 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  | 
            ||
| 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 | }  | 
            ||
| 313 |