| Conditions | 25 |
| Paths | 32 |
| Total Lines | 323 |
| Code Lines | 133 |
| Lines | 48 |
| Ratio | 14.86 % |
| Changes | 1 | ||
| 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 __invoke($output) |
||
| 31 | { |
||
| 32 | |||
| 33 | $css = file_get_contents(__DIR__ . '/../css/pQp.css'); |
||
| 34 | |||
| 35 | echo <<<JAVASCRIPT |
||
| 36 | <!-- JavaScript --> |
||
| 37 | <script type="text/javascript"> |
||
| 38 | var PQP_DETAILS = true; |
||
| 39 | var PQP_HEIGHT = "short"; |
||
| 40 | |||
| 41 | function changeTab(tab) { |
||
| 42 | var pQp = document.getElementById('pQp'); |
||
| 43 | hideAllTabs(); |
||
| 44 | addClassName(pQp, tab, true); |
||
| 45 | } |
||
| 46 | |||
| 47 | function hideAllTabs() { |
||
| 48 | var pQp = document.getElementById('pQp'); |
||
| 49 | removeClassName(pQp, 'console'); |
||
| 50 | removeClassName(pQp, 'speed'); |
||
| 51 | removeClassName(pQp, 'queries'); |
||
| 52 | removeClassName(pQp, 'memory'); |
||
| 53 | removeClassName(pQp, 'files'); |
||
| 54 | } |
||
| 55 | |||
| 56 | function toggleDetails(){ |
||
| 57 | var container = document.getElementById('pqp-container'); |
||
| 58 | |||
| 59 | if(PQP_DETAILS){ |
||
| 60 | addClassName(container, 'hideDetails', true); |
||
| 61 | PQP_DETAILS = false; |
||
| 62 | } |
||
| 63 | else{ |
||
| 64 | removeClassName(container, 'hideDetails'); |
||
| 65 | PQP_DETAILS = true; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | function toggleHeight(){ |
||
| 69 | var container = document.getElementById('pqp-container'); |
||
| 70 | |||
| 71 | if(PQP_HEIGHT == "short"){ |
||
| 72 | addClassName(container, 'tallDetails', true); |
||
| 73 | PQP_HEIGHT = "tall"; |
||
| 74 | } |
||
| 75 | else{ |
||
| 76 | removeClassName(container, 'tallDetails'); |
||
| 77 | PQP_HEIGHT = "short"; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | //http://www.bigbold.com/snippets/posts/show/2630 |
||
| 82 | function addClassName(objElement, strClass, blnMayAlreadyExist){ |
||
| 83 | if ( objElement.className ){ |
||
| 84 | var arrList = objElement.className.split(' '); |
||
| 85 | if ( blnMayAlreadyExist ){ |
||
| 86 | var strClassUpper = strClass.toUpperCase(); |
||
| 87 | for ( var i = 0; i < arrList.length; i++ ){ |
||
| 88 | if ( arrList[i].toUpperCase() == strClassUpper ){ |
||
| 89 | arrList.splice(i, 1); |
||
| 90 | i--; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | arrList[arrList.length] = strClass; |
||
| 95 | objElement.className = arrList.join(' '); |
||
| 96 | } |
||
| 97 | else{ |
||
| 98 | objElement.className = strClass; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | //http://www.bigbold.com/snippets/posts/show/2630 |
||
| 103 | function removeClassName(objElement, strClass){ |
||
| 104 | if ( objElement.className ){ |
||
| 105 | var arrList = objElement.className.split(' '); |
||
| 106 | var strClassUpper = strClass.toUpperCase(); |
||
| 107 | for ( var i = 0; i < arrList.length; i++ ){ |
||
| 108 | if ( arrList[i].toUpperCase() == strClassUpper ){ |
||
| 109 | arrList.splice(i, 1); |
||
| 110 | i--; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | objElement.className = arrList.join(' '); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | setTimeout(function(){document.getElementById("pqp-container").style.display = "block"}, 100); |
||
| 117 | </script> |
||
| 118 | JAVASCRIPT; |
||
| 119 | |||
| 120 | echo <<<STYLESHEETS |
||
| 121 | <style type="text/css"> |
||
| 122 | $css |
||
| 123 | </style> |
||
| 124 | STYLESHEETS; |
||
| 125 | |||
| 126 | echo '<div id="pqp-container" class="pQp" style="display:none">'; |
||
| 127 | |||
| 128 | $logCount = array_sum($this->console_data['count']); |
||
| 129 | $fileCount = count($output['files']); |
||
| 130 | $memoryUsed = $output['memoryTotals']['used']; |
||
| 131 | $queryCount = $output['queryTotals']['count']; |
||
| 132 | $speedTotal = $output['speedTotals']['total']; |
||
| 133 | |||
| 134 | echo <<<PQPTABS |
||
| 135 | <div id="pQp" class="console"> |
||
| 136 | <table id="pqp-metrics" cellspacing="0"> |
||
| 137 | <tr> |
||
| 138 | <td class="green" onclick="changeTab('console');"> |
||
| 139 | <var>$logCount</var> |
||
| 140 | <h4>Console</h4> |
||
| 141 | </td> |
||
| 142 | <td class="blue" onclick="changeTab('speed');"> |
||
| 143 | <var>$speedTotal</var> |
||
| 144 | <h4>Load Time</h4> |
||
| 145 | </td> |
||
| 146 | <td class="purple" onclick="changeTab('queries');"> |
||
| 147 | <var>$queryCount Queries</var> |
||
| 148 | <h4>Database</h4> |
||
| 149 | </td> |
||
| 150 | <td class="orange" onclick="changeTab('memory');"> |
||
| 151 | <var>$memoryUsed</var> |
||
| 152 | <h4>Memory Used</h4> |
||
| 153 | </td> |
||
| 154 | <td class="red" onclick="changeTab('files');"> |
||
| 155 | <var>{$fileCount} Files</var> |
||
| 156 | <h4>Included</h4> |
||
| 157 | </td> |
||
| 158 | </tr> |
||
| 159 | </table> |
||
| 160 | PQPTABS; |
||
| 161 | |||
| 162 | echo '<div id="pqp-console" class="pqp-box">'; |
||
| 163 | |||
| 164 | if($logCount == 0) { |
||
| 165 | echo '<h3>This panel has no log items.</h3>'; |
||
| 166 | } |
||
| 167 | else { |
||
| 168 | echo '<table class="side" cellspacing="0"> |
||
| 169 | <tr> |
||
| 170 | <td class="alt1"><var>'.$this->console_data['count']['log'].'</var><h4>Logs</h4></td> |
||
| 171 | <td class="alt2"><var>'.$this->console_data['count']['error'].'</var> <h4>Errors</h4></td> |
||
| 172 | </tr> |
||
| 173 | <tr> |
||
| 174 | <td class="alt3"><var>'.$this->console_data['count']['memory'].'</var> <h4>Memory</h4></td> |
||
| 175 | <td class="alt4"><var>'.$this->console_data['count']['speed'].'</var> <h4>Speed</h4></td> |
||
| 176 | </tr> |
||
| 177 | </table> |
||
| 178 | <table class="main" cellspacing="0">'; |
||
| 179 | |||
| 180 | $class = ''; |
||
| 181 | foreach($this->console_data['messages'] as $log) { |
||
| 182 | echo '<tr class="log-'.$log['type'].'"> |
||
| 183 | <td class="type">'.$log['type'].'</td> |
||
| 184 | <td class="'.$class.'">'; |
||
| 185 | if($log['type'] == 'log') { |
||
| 186 | echo '<div><pre>'.$log['data'].'</pre></div>'; |
||
| 187 | } |
||
| 188 | elseif($log['type'] == 'memory') { |
||
| 189 | echo '<div><pre>'.$log['data'].'</pre>'; |
||
| 190 | if (!empty($log['data_type'])) { |
||
| 191 | echo '<em>'.$log['data_type'].'</em>: '; |
||
| 192 | } |
||
| 193 | echo $log['name'].' </div>'; |
||
| 194 | } |
||
| 195 | elseif($log['type'] == 'speed') { |
||
| 196 | echo '<div><pre>'.$log['data'].'</pre> <em>'.$log['name'].'</em></div>'; |
||
| 197 | } |
||
| 198 | elseif($log['type'] == 'error') { |
||
| 199 | echo '<div><em>Line '.$log['line'].'</em> : '.$log['data'].' <pre>'.$log['file'].'</pre></div>'; |
||
| 200 | } |
||
| 201 | |||
| 202 | echo '</td></tr>'; |
||
| 203 | if($class == '') $class = 'alt'; |
||
| 204 | else $class = ''; |
||
| 205 | } |
||
| 206 | |||
| 207 | echo '</table>'; |
||
| 208 | } |
||
| 209 | |||
| 210 | echo '</div>'; |
||
| 211 | |||
| 212 | echo '<div id="pqp-speed" class="pqp-box">'; |
||
| 213 | |||
| 214 | View Code Duplication | if($this->console_data['count']['speed'] == 0) { |
|
|
|
|||
| 215 | echo '<h3>This panel has no log items.</h3>'; |
||
| 216 | } |
||
| 217 | else { |
||
| 218 | echo '<table class="side" cellspacing="0"> |
||
| 219 | <tr><td><var>'.$output['speedTotals']['total'].'</var><h4>Load Time</h4></td></tr> |
||
| 220 | <tr><td class="alt"><var>'.$output['speedTotals']['allowed'].'</var> <h4>Max Execution Time</h4></td></tr> |
||
| 221 | </table> |
||
| 222 | <table class="main" cellspacing="0">'; |
||
| 223 | |||
| 224 | $class = ''; |
||
| 225 | foreach($this->console_data['messages'] as $log) { |
||
| 226 | if($log['type'] == 'speed') { |
||
| 227 | echo '<tr class="log-'.$log['type'].'"> |
||
| 228 | <td class="'.$class.'">'; |
||
| 229 | echo '<div><pre>'.$log['data'].'</pre> <em>'.$log['name'].'</em></div>'; |
||
| 230 | echo '</td></tr>'; |
||
| 231 | if($class == '') $class = 'alt'; |
||
| 232 | else $class = ''; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | echo '</table>'; |
||
| 237 | } |
||
| 238 | |||
| 239 | echo '</div>'; |
||
| 240 | |||
| 241 | echo '<div id="pqp-queries" class="pqp-box">'; |
||
| 242 | |||
| 243 | if($output['queryTotals']['count'] == 0) { |
||
| 244 | echo '<h3>This panel has no log items.</h3>'; |
||
| 245 | } |
||
| 246 | else { |
||
| 247 | echo '<table class="side" cellspacing="0"> |
||
| 248 | <tr><td><var>'.$output['queryTotals']['count'].'</var><h4>Total Queries</h4></td></tr> |
||
| 249 | <tr><td class="alt"><var>'.$output['queryTotals']['time'].'</var> <h4>Total Time</h4></td></tr> |
||
| 250 | <tr><td><var>0</var> <h4>Duplicates</h4></td></tr> |
||
| 251 | </table> |
||
| 252 | <table class="main" cellspacing="0">'; |
||
| 253 | |||
| 254 | $class = ''; |
||
| 255 | foreach($output['queries'] as $query) { |
||
| 256 | echo '<tr> |
||
| 257 | <td class="'.$class.'">'.$query['sql']; |
||
| 258 | if($query['explain']) { |
||
| 259 | echo '<em> |
||
| 260 | Possible keys: <b>'.$query['explain']['possible_keys'].'</b> · |
||
| 261 | Key Used: <b>'.$query['explain']['key'].'</b> · |
||
| 262 | Type: <b>'.$query['explain']['type'].'</b> · |
||
| 263 | Rows: <b>'.$query['explain']['rows'].'</b> · |
||
| 264 | Speed: <b>'.$query['time'].'</b> |
||
| 265 | </em>'; |
||
| 266 | } |
||
| 267 | echo '</td></tr>'; |
||
| 268 | if($class == '') $class = 'alt'; |
||
| 269 | else $class = ''; |
||
| 270 | } |
||
| 271 | |||
| 272 | echo '</table>'; |
||
| 273 | } |
||
| 274 | |||
| 275 | echo '</div>'; |
||
| 276 | |||
| 277 | echo '<div id="pqp-memory" class="pqp-box">'; |
||
| 278 | |||
| 279 | if($this->console_data['count']['memory'] == 0) { |
||
| 280 | echo '<h3>This panel has no log items.</h3>'; |
||
| 281 | } |
||
| 282 | View Code Duplication | else { |
|
| 283 | echo '<table class="side" cellspacing="0"> |
||
| 284 | <tr><td><var>'.$output['memoryTotals']['used'].'</var><h4>Used Memory</h4></td></tr> |
||
| 285 | <tr><td class="alt"><var>'.$output['memoryTotals']['total'].'</var> <h4>Total Available</h4></td></tr> |
||
| 286 | </table> |
||
| 287 | <table class="main" cellspacing="0">'; |
||
| 288 | |||
| 289 | $class = ''; |
||
| 290 | foreach($this->console_data['messages'] as $log) { |
||
| 291 | if($log['type'] == 'memory') { |
||
| 292 | echo '<tr class="log-'.$log['type'].'">'; |
||
| 293 | echo '<td class="'.$class.'"><b>'.$log['data'].'</b>'; |
||
| 294 | if ($log['data_type']) { |
||
| 295 | echo '<em>'.$log['data_type'].'</em>: '; |
||
| 296 | } |
||
| 297 | echo $log['name'].'</td>'; |
||
| 298 | echo '</tr>'; |
||
| 299 | if($class == '') $class = 'alt'; |
||
| 300 | else $class = ''; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | echo '</table>'; |
||
| 305 | } |
||
| 306 | |||
| 307 | echo '</div>'; |
||
| 308 | |||
| 309 | echo '<div id="pqp-files" class="pqp-box">'; |
||
| 310 | |||
| 311 | if($output['fileTotals']['count'] == 0) { |
||
| 312 | echo '<h3>This panel has no log items.</h3>'; |
||
| 313 | } |
||
| 314 | else { |
||
| 315 | echo '<table class="side" cellspacing="0"> |
||
| 316 | <tr><td><var>'.$output['fileTotals']['count'].'</var><h4>Total Files</h4></td></tr> |
||
| 317 | <tr><td class="alt"><var>'.$output['fileTotals']['size'].'</var> <h4>Total Size</h4></td></tr> |
||
| 318 | <tr><td><var>'.$output['fileTotals']['largest'].'</var> <h4>Largest</h4></td></tr> |
||
| 319 | </table> |
||
| 320 | <table class="main" cellspacing="0">'; |
||
| 321 | |||
| 322 | $class =''; |
||
| 323 | foreach($output['files'] as $file) { |
||
| 324 | echo '<tr><td class="'.$class.'"><b>'.$file['size'].'</b> '.$file['name'].'</td></tr>'; |
||
| 325 | if($class == '') $class = 'alt'; |
||
| 326 | else $class = ''; |
||
| 327 | } |
||
| 328 | |||
| 329 | echo '</table>'; |
||
| 330 | } |
||
| 331 | |||
| 332 | echo '</div>'; |
||
| 333 | |||
| 334 | echo <<<FOOTER |
||
| 335 | <table id="pqp-footer" cellspacing="0"> |
||
| 336 | <tr> |
||
| 337 | <td class="credit"> |
||
| 338 | <a href="http://particletree.com" target="_blank"> |
||
| 339 | <strong>PHP</strong> |
||
| 340 | <b class="green">Q</b><b class="blue">u</b><b class="purple">i</b><b class="orange">c</b><b class="red">k</b> |
||
| 341 | Profiler</a></td> |
||
| 342 | <td class="actions"> |
||
| 343 | <a href="#" onclick="toggleDetails();return false">Details</a> |
||
| 344 | <a class="heightToggle" href="#" onclick="toggleHeight();return false">Height</a> |
||
| 345 | </td> |
||
| 346 | </tr> |
||
| 347 | </table> |
||
| 348 | FOOTER; |
||
| 349 | |||
| 350 | echo '</div></div>'; |
||
| 351 | |||
| 352 | } |
||
| 353 | } |
||
| 354 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.