Passed
Push — dbal ( 259ca9...c97573 )
by Greg
12:35 queued 06:07
created

PhpService::memoryLimit()   A

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 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
/**
31
 * Access to the PHP environment - to facilitate mocking/testing.
32
 */
33
class PhpService
34
{
35
    public function extensionLoaded(string $extension): bool
36
    {
37
        return extension_loaded(extension: $extension);
38
    }
39
40
    public function functionExists(string $function): bool
41
    {
42
        return function_exists(function: $function);
43
    }
44
45
    public function sysGetTempDir(): string
46
    {
47
        return sys_get_temp_dir();
48
    }
49
50
    public function displayErrors(): bool
51
    {
52
        return (bool) $this->iniGet(option: 'display_errors');
53
    }
54
55
    public function maxExecutionTime(): int
56
    {
57
        return (int) $this->iniGet(option: 'max_execution_time');
58
    }
59
60
    public function memoryLimit(): int
61
    {
62
        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

62
        return /** @scrutinizer ignore-call */ ini_parse_quantity(shorthand: $this->iniGet(option: 'memory_limit'));
Loading history...
63
    }
64
65
    public function postMaxSize(): int
66
    {
67
        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

67
        return /** @scrutinizer ignore-call */ ini_parse_quantity(shorthand: $this->iniGet(option: 'post_max_size'));
Loading history...
68
    }
69
70
    public function uploadMaxFilesize(): int
71
    {
72
        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

72
        return /** @scrutinizer ignore-call */ ini_parse_quantity(shorthand: $this->iniGet(option: 'upload_max_filesize'));
Loading history...
73
    }
74
75
    public function iniGet(string $option): string
76
    {
77
        $value = ini_get(option: $option);
78
79
        if ($value === false) {
80
            throw new RuntimeException(message: 'Cannot read PHP configuration: ' . $option);
81
        }
82
83
        return $value;
84
    }
85
}
86