Completed
Push — master ( 7b686b...b265e2 )
by Avtandil
09:58
created

PhpIniOptions::name()   A

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
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
13
14
use BeyondCode\SelfDiagnosis\Checks\Check;
15
use BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException;
16
use Illuminate\Support\Arr;
17
use Longman\LaravelLodash\SelfDiagnosis\ParsesNumValues;
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
            $actual_value = 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
                default:
52
                    if ($value !== $actual_value) {
53
                        $this->options[$option] = $actual_value;
54
                    }
55
                    break;
56
57 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...
58
                    $actual_bytes = $this->toBytes($actual_value);
59
                    $bytes = $this->toBytes($match[2]);
60
61
                    if (! ($actual_bytes > $bytes)) {
62
                        $this->options[$option] = $actual_value;
63
                    }
64
                    break;
65
66 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...
67
                    $actual_bytes = $this->toBytes($actual_value);
68
                    $bytes = $this->toBytes($match[2]);
69
70
                    if (! ($actual_bytes >= $bytes)) {
71
                        $this->options[$option] = $actual_value;
72
                    }
73
                    break;
74
75 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...
76
                    $actual_bytes = $this->toBytes($actual_value);
77
                    $bytes = $this->toBytes($match[2]);
78
79
                    if (! ($actual_bytes < $bytes)) {
80
                        $this->options[$option] = $actual_value;
81
                    }
82
                    break;
83
84 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...
85
                    $actual_bytes = $this->toBytes($actual_value);
86
                    $bytes = $this->toBytes($match[2]);
87
88
                    if (! ($actual_bytes <= $bytes)) {
89
                        $this->options[$option] = $actual_value;
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