| 1 | <?php |
||
| 13 | class Benchmark |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected static $markers = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected static $lastName; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Benchmark constructor. |
||
| 27 | * |
||
| 28 | * @param $markers |
||
| 29 | */ |
||
| 30 | 4 | public function __construct($markers = []) |
|
| 31 | { |
||
| 32 | 4 | self::$markers = $markers; |
|
| 33 | 4 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param bool|string $name |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | 6 | public static function start($name = false) |
|
| 40 | { |
||
| 41 | 6 | $name = self::getName($name); |
|
|
|
|||
| 42 | 6 | self::$markers[$name] = Measure::now(); |
|
| 43 | 6 | return $name; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param bool $name |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | 6 | public static function stop($name = null) |
|
| 51 | { |
||
| 52 | 6 | $name = $name ?: self::getLastName(); |
|
| 53 | 6 | self::$markers[$name] = array_merge(Measure::diff(self::$markers[$name]), Peak::now()); |
|
| 54 | 6 | return $name; |
|
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param bool $name |
||
| 59 | * @return array|mixed |
||
| 60 | */ |
||
| 61 | 3 | public static function getMarkers($name = false) |
|
| 62 | { |
||
| 63 | 3 | return $name ? [ $name => self::$markers[$name]] : self::$markers; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string|bool $name |
||
| 68 | * @return static |
||
| 69 | */ |
||
| 70 | 4 | public static function get($name = false) |
|
| 71 | { |
||
| 72 | 4 | return $name ? new self([$name => self::$markers[$name]]) : new static(self::$markers); |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 2 | private static function getLastName() |
|
| 79 | { |
||
| 80 | 2 | $name = self::$lastName; |
|
| 81 | 2 | self::$lastName = null; |
|
| 82 | 2 | return $name; |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param bool $name |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 6 | private static function getName($name = false) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | 6 | public static function reset() |
|
| 103 | } |
||
| 104 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.