PhpIniOptions::check()   C
last analyzed

Complexity

Conditions 13
Paths 12

Size

Total Lines 60

Duplication

Lines 32
Ratio 53.33 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
nc 12
nop 1
dl 32
loc 60
ccs 0
cts 37
cp 0
crap 182
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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