Kint_Parsers_Microtime::_parse()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 45
Code Lines 29

Duplication

Lines 11
Ratio 24.44 %

Code Coverage

Tests 6
CRAP Score 28.5098

Importance

Changes 0
Metric Value
cc 7
dl 11
loc 45
ccs 6
cts 25
cp 0.24
crap 28.5098
rs 6.7272
c 0
b 0
f 0
eloc 29
nc 4
nop 1
1
<?php
2
3
namespace kint\parsers\custom;
4
5
use kint\inc\KintParser;
6
7
/**
8
 * Class Kint_Parsers_Microtime
9
 */
10
class Kint_Parsers_Microtime extends KintParser
11
{
12
  private static $_times = array();
13
  private static $_laps  = array();
14
15
  /**
16
   * @param mixed $variable
17
   *
18
   * @return bool
19
   */
20 1
  protected function _parse(&$variable)
21
  {
22 View Code Duplication
    if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
23 1
        is_object($variable)
24
        ||
25 1
        is_array($variable)
26
        ||
27 1
        (string)$variable !== $variable
28
        ||
29 1
        !preg_match('[0\.[0-9]{8} [0-9]{10}]', $variable)
30
    ) {
31 1
      return false;
32
    }
33
34
    list($usec, $sec) = explode(' ', $variable);
35
36
    $time = (float)$usec + (float)$sec;
37
    $size = memory_get_usage(true);
38
39
    # '@' is used to prevent the dreaded timezone not set error
40
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
41
    $this->value = @date('Y-m-d H:i:s', $sec) . '.' . substr($usec, 2, 4);
42
43
    $numberOfCalls = count(self::$_times);
44
    if ($numberOfCalls > 0) { # meh, faster than count($times) > 1
45
      $lap = $time - end(self::$_times);
46
      self::$_laps[] = $lap;
47
48
      $this->value .= "\n<b>SINCE LAST CALL:</b> <b class=\"kint-microtime\">" . round($lap, 4) . '</b>s.';
49
      if ($numberOfCalls > 1) {
50
        $this->value .= "\n<b>SINCE START:</b> " . round($time - self::$_times[0], 4) . 's.';
51
        $this->value .= "\n<b>AVERAGE DURATION:</b> "
52
                        . round(array_sum(self::$_laps) / $numberOfCalls, 4) . 's.';
53
      }
54
    }
55
56
    $unit = array('B', 'KB', 'MB', 'GB', 'TB');
57
    $memTmp = round($size / pow(1024, $i = (int)floor(log($size, 1024))), 3);
58
    $this->value .= "\n<b>MEMORY USAGE:</b> " . $size . ' bytes (' . $memTmp . ' ' . $unit[$i] . ')';
59
60
    self::$_times[] = $time;
61
    $this->type = 'Stats';
62
63
    return true;
64
  }
65
}
66