Module::id()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\BlackBar\Modules;
4
5
use GeminiLabs\BlackBar\Application;
6
7
abstract class Module
8
{
9
    /**
10
     * @var Application
11
     */
12
    protected $app;
13
    /**
14
     * @var array
15
     */
16
    protected $entries;
17
    /**
18
     * @var array
19
     */
20
    public $highlighted;
21
22
    public function __construct(Application $app)
23
    {
24
        $this->app = $app;
25
        $this->entries = [];
26
        $this->highlighted = $this->highlighted();
27
    }
28
29
    public function classes(): string
30
    {
31
        return $this->id();
32
    }
33
34
    abstract public function entries(): array;
35
36
    public function hasEntries(): bool
37
    {
38
        return !empty($this->entries);
39
    }
40
41
    public function highlighted(): array
42
    {
43
        return [];
44
    }
45
46
    public function id(): string
47
    {
48
        return sprintf('glbb-%s', $this->slug());
49
    }
50
51
    public function info(): string
52
    {
53
        return '';
54
    }
55
56
    public function isVisible(): bool
57
    {
58
        return true;
59
    }
60
61
    abstract public function label(): string;
62
63
    public function render(): void
64
    {
65
        $this->app->render('panels/'.$this->slug(), ['module' => $this]);
66
    }
67
68
    public function slug(): string
69
    {
70
        return strtolower((new \ReflectionClass($this))->getShortName());
71
    }
72
73
    protected function formatTime(int $nanoseconds): string
74
    {
75
        if ($nanoseconds >= 1e9) {
76
            return sprintf('%s s', $this->toDecimal(round($nanoseconds / 1e9, 2)));
77
        }
78
        if ($nanoseconds >= 1e6) {
79
            return sprintf('%s ms', $this->toDecimal(round($nanoseconds / 1e6, 2)));
80
        }
81
        if ($nanoseconds >= 1e3) {
82
            return sprintf('%s µs', round($nanoseconds / 1e3));
83
        }
84
        return sprintf('%s ns', $nanoseconds);
85
    }
86
87
    protected function toDecimal(float $number): string
88
    {
89
        $number = (string) $number;
90
        if (false !== strpos($number, '.')) {
91
            $number = rtrim(rtrim($number, '0'), '.');
92
        }
93
        return $number;
94
    }
95
}
96