Completed
Push — master ( 900799...05921b )
by Bret R.
02:04 queued 17s
created

PhpIniCheck   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 163
Duplicated Lines 8.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 2
dl 14
loc 163
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A stringToMegabyte() 0 14 4
B check() 0 20 6
A checkBoolean() 0 22 5
A checkMemory() 0 9 2
A checkNumber() 14 19 5
A checkRegex() 0 6 1
A checkString() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
class PhpIniCheck extends AbstractCheck
7
{
8
9
    const TypeBoolean = 'boolean';
10
    const TypeMemory  = 'memory';
11
    const TypeNumber  = 'number';
12
    const TypeRegex   = 'regex';
13
    const TypeString  = 'string';
14
15
    protected $varName;
16
    protected $varType;
17
    protected $varValue;
18
    protected $maxValue;
19
    protected $iniValue;
20
21
    /**
22
     * PhpIniCheck constructor.
23
     *
24
     * @param string $label
25
     * @param string $varName <b>Name</b> of ini value - used for ini_get(...)
26
     * @param string $varType <b>Type</b> of ini value - see class constant
27
     * @param mixed $varValue <b>Value</b> which is expected
28
     * @param null $maxValue <b>maximum</b> value is optional used for numbers
29
     *
30
     * @return PhpIniCheck
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
31
     */
32
    public function __construct(string $label, string $varName, string $varType, $varValue, $maxValue=null)
33
    {
34
        parent::__construct($label);
35
        $this->varName = $varName;
36
        $this->varType = $varType;
37
        $this->varValue = $varValue;
38
        $this->maxValue = $maxValue;
39
        $this->iniValue = ini_get($varName);
40
    }
41
42
    /**
43
     * @param string $size
44
     * @return int|bool(false)
0 ignored issues
show
Documentation introduced by
The doc-type int|bool(false) could not be parsed: Expected "|" or "end of type", but got "(" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
45
     */
46
    protected function stringToMegabyte(string $size)
47
    {
48
        $value = preg_replace('~[^0-9]*~','', $size);
49
        switch (substr(strtolower($size), -1)) {
50
            case 'm':
51
                return (int)$value;
52
            case 'k':
53
                return round((int)$value / 1024);
54
            case 'g':
55
                return ((int)$value * 1024);
56
            default:
57
                return false;
58
        }
59
    }
60
61
62
    /**
63
     * @return Result
64
     */
65
    public function check(): Result
66
    {
67
        switch ($this->varType) {
68
            case self::TypeBoolean:
69
                return $this->checkBoolean();
70
            case self::TypeMemory:
71
                return $this->checkMemory();
72
            case self::TypeNumber:
73
                return $this->checkNumber();
74
            case self::TypeRegex:
75
                return $this->checkRegex();
76
            case self::TypeString:
77
                return $this->checkString();
78
            default:
79
                $result = new Result($this->label);
80
                $result->setSuccess(false);
81
                $result->setError("Invalid Type: " . $this->varType);
82
                return $result;
83
        }
84
    }
85
86
    /**
87
     * @return Result
88
     */
89
    protected function checkBoolean() {
90
        $result = new Result($this->label);
91
        // some boolval advance
92
        switch (strtolower($this->iniValue)) {
93
            case 'on':
94
                $this->iniValue = true;
95
                break;
96
            case 'off':
97
                $this->iniValue = false;
98
                break;
99
            case 'yes':
100
                $this->iniValue = true;
101
                break;
102
            case 'no':
103
                $this->iniValue = false;
104
                break;
105
        }
106
107
        $result->setSuccess( boolval($this->iniValue) === boolval($this->varValue) );
108
        $result->setError("php.ini value of '".$this->varName."' is set to '".strval(boolval($this->iniValue))."' instead of expected '".strval(boolval($this->varValue))."'");
109
        return $result;
110
    }
111
112
    /**
113
     * @return Result
114
     */
115
    protected function checkMemory() {
116
        $result = new Result($this->label);
117
        if ($this->iniValue != -1) {
118
            $value = $this->stringToMegabyte($this->iniValue);
0 ignored issues
show
Bug introduced by
It seems like $this->iniValue can also be of type boolean; however, BretRZaun\StatusPage\Che...eck::stringToMegabyte() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
119
            $result->setSuccess( $value >= $this->varValue );
120
            $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', minimum expected is '".strval($this->varValue)."'");
121
        }
122
        return $result;
123
    }
124
125
    /**
126
     * @return Result
127
     */
128
    protected function checkNumber() {
129
        $result = new Result($this->label);
130 View Code Duplication
        if (!is_null($this->varValue)) {
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...
131
            if ($this->iniValue < $this->varValue) {
132
                $result->setSuccess(false);
133
                $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', minimum expected is '".strval($this->varValue)."'");
134
                return $result;
135
            }
136
        }
137 View Code Duplication
        if (!is_null($this->maxValue)) {
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...
138
            if ($this->iniValue > $this->maxValue) {
139
                $result->setSuccess(false);
140
                $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', maximum expected is '".strval($this->maxValue)."'");
141
                return $result;
142
            }
143
        }
144
        $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue));
145
        return $result;
146
    }
147
148
    /**
149
     * @return Result
150
     */
151
    protected function checkRegex() {
152
        $result = new Result($this->label);
153
        $result->setSuccess(preg_match('~'.$this->varValue.'~', $this->iniValue));
154
        $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', expected is '".strval($this->varValue)."'");
155
        return $result;
156
    }
157
158
    /**
159
     * @return Result
160
     */
161
    protected function checkString() {
162
        $result = new Result($this->label);
163
        $result->setSuccess( strval($this->iniValue) == strval($this->varValue) );
164
        $result->setError("php.ini value of '".$this->varName."' is set to '".strval($this->iniValue)."', expected is '".strval($this->varValue)."'");
165
        return $result;
166
    }
167
168
}
169