PhpIniOptions::name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
6
7
use BeyondCode\SelfDiagnosis\Checks\Check;
8
use BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException;
9
use Illuminate\Support\Arr;
10
use Longman\LaravelLodash\SelfDiagnosis\ParsesNumValues;
11
12
use function count;
13
use function implode;
14
use function ini_get;
15
use function preg_match;
16
17
use const PHP_EOL;
18
19
class PhpIniOptions implements Check
20
{
21
    use ParsesNumValues;
22
23
    /** @var array */
24
    private $options = [];
25
26
    /**
27
     * The name of the check.
28
     *
29
     * @param array $config
30
     * @return string
31
     */
32
    public function name(array $config): string
33
    {
34
        return trans('lodash::checks.php_ini_options.name');
35
    }
36
37
    public function check(array $config): bool
38
    {
39
        $options = Arr::get($config, 'options', []);
40
        foreach ($options as $option => $value) {
41
            $actualValue = ini_get($option);
42
43
            preg_match('#([><=]{0,2})(.*)#', $value, $match);
44
45
            if (! empty($match[1]) && $match[2] === '') {
46
                throw new InvalidConfigurationException('Value of option "' . $option . '" is invalid');
47
            }
48
49
            $prefix = $match[1] ?? null;
50
            switch ($prefix) {
51 View Code Duplication
                case '>':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                    $actualBytes = $this->toBytes($actualValue);
53
                    $bytes = $this->toBytes($match[2]);
54
55
                    if (! ($actualBytes > $bytes)) {
56
                        $this->options[$option] = $actualValue;
57
                    }
58
                    break;
59
60 View Code Duplication
                case '>=':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
                    $actualBytes = $this->toBytes($actualValue);
62
                    $bytes = $this->toBytes($match[2]);
63
64
                    if (! ($actualBytes >= $bytes)) {
65
                        $this->options[$option] = $actualValue;
66
                    }
67
                    break;
68
69 View Code Duplication
                case '<':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
                    $actualBytes = $this->toBytes($actualValue);
71
                    $bytes = $this->toBytes($match[2]);
72
73
                    if (! ($actualBytes < $bytes)) {
74
                        $this->options[$option] = $actualValue;
75
                    }
76
                    break;
77
78 View Code Duplication
                case '<=':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
                    $actualBytes = $this->toBytes($actualValue);
80
                    $bytes = $this->toBytes($match[2]);
81
82
                    if (! ($actualBytes <= $bytes)) {
83
                        $this->options[$option] = $actualValue;
84
                    }
85
                    break;
86
87
                default:
88
                    if ($value !== $actualValue) {
89
                        $this->options[$option] = $actualValue;
90
                    }
91
                    break;
92
            }
93
        }
94
95
        return count($this->options) === 0;
96
    }
97
98
    public function message(array $config): string
99
    {
100
        $options = [];
101
        foreach ($this->options as $option => $value) {
102
            $options[] = $option . ': ' . $value;
103
        }
104
105
        return trans('lodash::checks.php_ini_options.message', [
106
            'options' => implode(PHP_EOL, $options),
107
        ]);
108
    }
109
}
110