Lines 100.00% 13 / 13
Methods 100.00% 8 / 8
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 init 100.00% 3 / 3 100.00% 1 / 1 2
 add 100.00% 1 / 1 100.00% 1 / 1 1
 addMemoryPeak 100.00% 2 / 2 100.00% 1 / 1 1
 getList 100.00% 1 / 1 100.00% 1 / 1 1
 __construct 100.00% 3 / 3 100.00% 1 / 1 1
 getSeconds 100.00% 1 / 1 100.00% 1 / 1 1
 getMilliSeconds 100.00% 1 / 1 100.00% 1 / 1 1
 __toString 100.00% 1 / 1 100.00% 1 / 1 1
5class Profiler
6{
7    public static ?float $startupMicrotime = null;
8
9    /** @var list<self> */
10    public static array $profileList = [];
11
12    protected string $message = '';
13
14    protected float $instanceMicrotime;
15
16    protected int $includedFiles = 0;
17
18    /**
19     * @SuppressWarnings(Superglobal)
20     */
21    public static function init(): void
22    {
23        static::$startupMicrotime = microtime(true);
24        if (isset($_SERVER["REQUEST_TIME_FLOAT"])) {
25            static::$startupMicrotime = $_SERVER["REQUEST_TIME_FLOAT"];
26        }
27    }
28
29    public static function add(string $message): void
30    {
31        static::$profileList[] = new self($message);
32    }
33
34    public static function addMemoryPeak(string $message = 'Mem'): void
35    {
36        $memoryKb = round(memory_get_peak_usage() / 1024, 0);
37        static::add("$message " . $memoryKb . "kb");
38    }
39
40    public static function getList(): string
41    {
42        return implode(";", static::$profileList);
43    }
44
45    public function __construct(string $message)
46    {
47        $this->message = $message;
48        $this->instanceMicrotime = microtime(true);
49        $this->includedFiles = count(get_included_files());
50    }
51
52    public function getSeconds(): float
53    {
54        return round(($this->instanceMicrotime - (static::$startupMicrotime ?? $this->instanceMicrotime)), 3);
55    }
56
57    public function getMilliSeconds(): float
58    {
59        return $this->getSeconds() * 1000;
60    }
61
62    public function __toString(): string
63    {
64        return $this->message . "=" . $this->getMilliSeconds() . "ms/#" . $this->includedFiles;
65    }
66}