PHP::getServer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Hyperized\Benchmark\Modules;
5
6
use Hyperized\Benchmark\Config\Config;
7
use Hyperized\Benchmark\Generic\Size;
8
use Hyperized\Benchmark\Generic\Visual;
9
10
11
/**
12
 * Class PHP
13
 * @package Hyperized\Benchmark\Modules
14
 */
15
class PHP
16
{
17
    /**
18
     * @var array
19
     */
20
    private $extensions;
21
22
    /**
23
     * @var int
24
     */
25
    private $maxUploadBytes;
26
27
    /**
28
     * @var
29
     */
30
    private $maxMemoryBytes;
31
32
    /**
33
     * @var
34
     */
35
    private $server;
36
37
    /**
38
     * PHP constructor.
39
     *
40
     * @param \Hyperized\Benchmark\Config\Config $config
41
     */
42
    public function __construct(Config $config)
43
    {
44
        if ($config->get('benchmark.php.enabled')) {
45
            $this->run();
46
            $this->render();
47
        }
48
    }
49
50
    /**
51
     * Run!
52
     */
53
    private function run(): void
54
    {
55
        $this->extensions = $this->getExtensions();
56
        $this->maxUploadBytes = $this->getMaxUploadBytes();
57
        $this->maxMemoryBytes = $this->getMaxMemoryBytes();
58
        $this->server = $this->getServer();
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    private function getExtensions(): array
65
    {
66
        return \get_loaded_extensions();
67
    }
68
69
    /**
70
     * @return int
71
     *
72
     * https://stackoverflow.com/a/25370978/1757763
73
     */
74
    private function getMaxUploadBytes(): int
75
    {
76
        static $max_size = -1;
77
78
        if ($max_size < 0) {
79
            // Start with post_max_size.
80
            $post_max_size = Size::formatToBytes(\ini_get('post_max_size'));
81
            if ($post_max_size > 0) {
82
                $max_size = $post_max_size;
83
            }
84
85
            // If upload_max_size is less, then reduce. Except if upload_max_size is
86
            // zero, which indicates no limit.
87
            $upload_max = Size::formatToBytes(\ini_get('upload_max_filesize'));
88
            if ($upload_max > 0 && $upload_max < $max_size) {
89
                $max_size = $upload_max;
90
            }
91
        }
92
        return $max_size;
93
    }
94
95
    /**
96
     * @return int
97
     */
98
    private function getMaxMemoryBytes(): int
99
    {
100
        return (int)Size::formatToBytes(\ini_get('memory_limit'));
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    private function getServer(): string
107
    {
108
        if (isset($_SERVER['SERVER_SOFTWARE'])) {
109
            return $_SERVER['SERVER_SOFTWARE'];
110
        }
111
        return 'Probably CLI';
112
    }
113
114
    /**
115
     * Gives output
116
     */
117
    private function render(): void
118
    {
119
        Visual::print('== Generic PHP information');
120
        Visual::print('PHP version: ' . $this->getVersion(), "\n");
121
        Visual::print('Server: ' . $this->server, "\n");
122
        Visual::print('Maximum execution time: ' . $this->getMaximumExecutionTime() . ' seconds');
123
        Visual::print('Maximum memory size: ' . Size::bytesToFormat($this->maxMemoryBytes) . ' (' . $this->maxMemoryBytes . ' bytes)', "\n");
124
        Visual::print('Maximum upload size: ' . Size::bytesToFormat($this->maxUploadBytes) . ' (' . $this->maxUploadBytes . ' bytes)');
125
        Visual::print('Modules loaded:', "\n");
126
        foreach ($this->extensions as $extension) {
127
            Visual::print(' ' . $extension . ' (' . $this->getVersion($extension) . ')', "\n");
128
        }
129
        Visual::print(' ', "\n");
130
    }
131
132
    /**
133
     * @param null $extension
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $extension is correct as it would always require null to be passed?
Loading history...
134
     *
135
     * @return string
136
     */
137
    private function getVersion($extension = null): string
138
    {
139
        if ($extension !== null) {
0 ignored issues
show
introduced by
The condition $extension !== null is always false.
Loading history...
140
            return \phpversion($extension);
141
        }
142
        return PHP_VERSION;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    private function getMaximumExecutionTime(): string
149
    {
150
        return \ini_get('max_execution_time');
151
    }
152
}