Passed
Push — main ( 9feb73...15f4e9 )
by Greg
07:34
created

PhpService::iniGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Services;
21
22
use RuntimeException;
23
24
use function extension_loaded;
25
use function function_exists;
26
use function ini_get;
27
use function ini_parse_quantity;
0 ignored issues
show
introduced by
The function ini_parse_quantity was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
28
use function sys_get_temp_dir;
29
30
use const PHP_OS_FAMILY;
31
32
/**
33
 * Access to the PHP environment - to facilitate mocking/testing.
34
 */
35
class PhpService
36
{
37
    public function extensionLoaded(string $extension): bool
38
    {
39
        return extension_loaded(extension: $extension);
40
    }
41
42
    public function functionExists(string $function): bool
43
    {
44
        return function_exists(function: $function);
45
    }
46
47
    public function sysGetTempDir(): string
48
    {
49
        return sys_get_temp_dir();
50
    }
51
52
    public function displayErrors(): bool
53
    {
54
        return (bool) $this->iniGet(option: 'display_errors');
55
    }
56
57
    public function maxExecutionTime(): int
58
    {
59
        return (int) $this->iniGet(option: 'max_execution_time');
60
    }
61
62
    public function memoryLimit(): int
63
    {
64
        return ini_parse_quantity(shorthand: $this->iniGet(option: 'memory_limit'));
0 ignored issues
show
Bug introduced by
The function ini_parse_quantity was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        return /** @scrutinizer ignore-call */ ini_parse_quantity(shorthand: $this->iniGet(option: 'memory_limit'));
Loading history...
65
    }
66
67
    public function pdoMysqlDefaultSocket(): string
68
    {
69
        if (PHP_OS_FAMILY === 'Windows') {
70
            return '';
71
        }
72
73
        return $this->iniGet(option: 'pdo_mysql.default_socket');
74
    }
75
76
    public function postMaxSize(): int
77
    {
78
        return ini_parse_quantity(shorthand: $this->iniGet(option: 'post_max_size'));
0 ignored issues
show
Bug introduced by
The function ini_parse_quantity was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        return /** @scrutinizer ignore-call */ ini_parse_quantity(shorthand: $this->iniGet(option: 'post_max_size'));
Loading history...
79
    }
80
81
    public function uploadMaxFilesize(): int
82
    {
83
        return ini_parse_quantity(shorthand: $this->iniGet(option: 'upload_max_filesize'));
0 ignored issues
show
Bug introduced by
The function ini_parse_quantity was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        return /** @scrutinizer ignore-call */ ini_parse_quantity(shorthand: $this->iniGet(option: 'upload_max_filesize'));
Loading history...
84
    }
85
86
    public function iniGet(string $option): string
87
    {
88
        $value = ini_get(option: $option);
89
90
        if ($value === false) {
91
            throw new RuntimeException(message: 'Cannot read PHP configuration: ' . $option);
92
        }
93
94
        return $value;
95
    }
96
}
97