Completed
Pull Request — master (#11)
by Davidson
02:50
created

RestrictionUtils::checkMaxLength()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
crap 2
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Utils;
3
4
use GoetasWebservices\XML\XSDReader\Exception\RestrictionException;
5
6
class RestrictionUtils
7
{
8
9
    /**
10
     * Defines a list of acceptable values.
11
     *  
12
     * @param mixed $value
13
     * @param array $enumeration
14
     * @throws RestrictionException
15
     * @return mixed
16
     */
17 2
    public static function checkEnumeration($value, $enumeration)
18
    {
19 2
        if (!in_array($value, $enumeration)) {
20 1
            $values = implode(', ', $enumeration);
21 1
            throw new RestrictionException(
22 1
                    "The restriction enumeration with '$values' is not true", 
23 1
                    RestrictionException::ERROR_CODE_ENUMARATION,
24 1
                    $value,
25 1
                    $enumeration);
0 ignored issues
show
Documentation introduced by
$enumeration is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
        }   
27 1
        return $value;
28
    }
29
    
30
    /**
31
     * Defines the exact sequence of characters that are acceptable.
32
     * 
33
     * @param mixed $value
34
     * @param string $pattern
35
     * @throws RestrictionException
36
     * @return mixed
37
     */
38 2
    public static function checkPattern($value, $pattern)
39
    {
40 2
        if (!preg_match("/^{$pattern}$/", $value)) {
41 1
            throw new RestrictionException(
42 1
                    "The restriction pattern with '$pattern' is not true", 
43 1
                    RestrictionException::ERROR_CODE_PATTERN,
44 1
                    $value,
45 1
                    $pattern);
46
        }
47 1
        return $value;
48
    }
49
    
50
    /**
51
     * Check is numeric valid
52
     * 
53
     * @param mixed $value
54
     * @return mixed
55
     * @throws RestrictionException
56
     */
57 31
    private static function getNumeric($value) 
58
    {
59 31
        if (!is_numeric($value)) {
60 2
            throw new RestrictionException(
61 2
                    "The '$value' is not a valid numeric", 
62 2
                    RestrictionException::ERROR_CODE_VALUE,
63 2
                    $value,
64 2
                    'is_numeric');
65
        }
66 29
        return $value + 0;
67
    }
68
    
69
    /**
70
     * Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero.
71
     * 
72
     * @param float $value
73
     * @param int $fractionDigits
74
     * @throws RestrictionException
75
     * @return float
76
     */
77 3
    public static function checkFractionDigits($value, $fractionDigits) 
78
    {
79 3
        $numeric = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
80 2
        if ($numeric < 0) {
81
            throw new RestrictionException(
82
                    "The '$numeric' must be equal to or greater than zero", 
83
                    RestrictionException::ERROR_CODE_GTE,
84
                    $numeric);
85
        }
86 2
        $count = 0;
87 2
        if ((int)$numeric != $numeric){
88 2
            $count = strlen($numeric) - strrpos($numeric, '.') - 1;
89 2
        }
90 2
        if ($count > $fractionDigits) {
91 1
            throw new RestrictionException(
92 1
                    "The restriction fraction digits with '$fractionDigits' is not true", 
93 1
                    RestrictionException::ERROR_CODE_FRACTION_DIGITS,
94 1
                    $value,
95 1
                    $fractionDigits);
96
        }
97 1
        return $numeric;
98
    }
99
    
100
    /**
101
     * Specifies the exact number of digits allowed. Must be greater than zero.
102
     * 
103
     * @param float $value
104
     * @param int $totalDigits
105
     * @throws RestrictionException
106
     * @return float
107
     */
108 4
    public static function checkTotalDigits($value, $totalDigits) 
109
    {
110 4
        $numeric = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
111 3
        if ($numeric < 0) {
112
            throw new RestrictionException(
113
                    "The '$numeric' must be equal to or greater than zero", 
114
                    RestrictionException::ERROR_CODE_GTE,
115
                    $numeric);
116
        }
117 3
        $count = 0;
0 ignored issues
show
Unused Code introduced by
$count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
118 3
        if ($numeric === 0) {
119
            $count = 1;
120
        } else {
121 3
            $count = floor(log10(floor($numeric))) + 1;
122
        }
123
        
124 3
        if ($count > $totalDigits) {
125 2
            throw new RestrictionException(
126 2
                    "The restriction total digits with '$totalDigits' is not true", 
127 2
                    RestrictionException::ERROR_CODE_TOTAL_DIGITS,
128 2
                    $value,
129 2
                    $totalDigits);
130
        }
131 1
        return $numeric;
132
    }
133
    
134
    /**
135
     * Specifies the upper bounds for numeric values (the value must be less than this value)
136
     * 
137
     * @param mixed $value
138
     * @param int $maxExclusive
139
     * @throws RestrictionException
140
     * @return mixed
141
     */
142 3 View Code Duplication
    public static function checkMaxExclusive($value, $maxExclusive) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
143
    {
144 3
        $numeric = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
145 3
        if ($numeric >= $maxExclusive) {
146 2
            throw new RestrictionException(
147 2
                    "The restriction max exclusive with '$maxExclusive' is not true", 
148 2
                    RestrictionException::ERROR_CODE_MAX_EXCLUSIVE,
149 2
                    $value,
150 2
                    $maxExclusive);
151
        }
152 1
        return $numeric;
153
    }
154
    
155
    /**
156
     * Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
157
     * 
158
     * @param mixed $value
159
     * @param int $maxInclusive
160
     * @throws RestrictionException
161
     * @return mixed
162
     */
163 3 View Code Duplication
    public static function checkMaxInclusive($value, $maxInclusive) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
164
    {
165 3
        $numeric = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
166 3
        if ($numeric > $maxInclusive) {
167 2
            throw new RestrictionException(
168 2
                    "The restriction max inclusive with '$maxInclusive' is not true", 
169 2
                    RestrictionException::ERROR_CODE_MAX_INCLUSIVE,
170 2
                    $value,
171 2
                    $maxInclusive);
172
        }
173 1
        return $numeric;
174
    }
175
    
176
    /**
177
     * Retrive the number of characters or list items allowed. 
178
     * 
179
     * @param mixed $value
180
     * @param string $nativeType
181
     * @throws RestrictionException
182
     * @return int
183
     */
184 21
    private static function getLength($value, $nativeType=null) 
185
    {
186 21
        $valueLength = 0;
0 ignored issues
show
Unused Code introduced by
$valueLength is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
187 21
        if (in_array($nativeType, ['int','float','integer']) && is_numeric($value)) {
188 12
            $valueLength = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
189 12
        } else
190 12
        if (is_array($value)) {
191 6
            $valueLength = count($value);
192 6
        } else {
193 9
            $valueLength = strlen($value);
194
        }
195 21
        if ($valueLength < 0) {
196
            throw new RestrictionException(
197
                    "The '$value' must be equal to or greater than zero", 
198
                    RestrictionException::ERROR_CODE_GTE,
199
                    $value);
200
        }
201 21
        return $valueLength;
202
    }    
203
    
204
    /**
205
     * Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero.
206
     * 
207
     * @param mixed $value
208
     * @param int $length
209
     * @param string $nativeType
210
     * @throws RestrictionException
211
     * @return mixed
212
     */
213 7 View Code Duplication
    public static function checkLength($value, $length, $nativeType=null) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
214
    {
215 7
        if (static::getLength($value, $nativeType) != $length) {
0 ignored issues
show
Bug introduced by
Since getLength() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLength() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
216 6
            throw new RestrictionException(
217 6
                    "The restriction length with '$length' is not true", 
218 6
                    RestrictionException::ERROR_CODE_LENGTH,
219 6
                    $value,
220 6
                    $length);
221
        }
222 1
        return $value;
223
    }    
224
    
225
    /**
226
     * Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
227
     * 
228
     * @param mixed $value
229
     * @param int $maxLength
230
     * @param string $nativeType
231
     * @throws RestrictionException
232
     * @return mixed
233
     */
234 7 View Code Duplication
    public static function checkMaxLength($value, $maxLength, $nativeType=null) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
235
    {
236 7
        if (static::getLength($value, $nativeType) > $maxLength) {
0 ignored issues
show
Bug introduced by
Since getLength() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLength() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
237 6
            throw new RestrictionException(
238 6
                    "The restriction max length with '$maxLength' is not true", 
239 6
                    RestrictionException::ERROR_CODE_MAX_LENGTH,
240 6
                    $value,
241 6
                    $maxLength);
242
        }
243 1
        return $value;
244
    }
245
    
246
    /**
247
     * Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
248
     * 
249
     * @param mixed $value
250
     * @param int $minLength
251
     * @param string $nativeType
252
     * @throws RestrictionException
253
     * @return mixed
254
     */
255 7 View Code Duplication
    public static function checkMinLength($value, $minLength, $nativeType=null) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
256
    {
257 7
        if (static::getLength($value, $nativeType) < $minLength) {
0 ignored issues
show
Bug introduced by
Since getLength() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLength() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
258 6
            throw new RestrictionException(
259 6
                    "The restriction min length with '$minLength' is not true", 
260 6
                    RestrictionException::ERROR_CODE_MIN_LENGTH,
261 6
                    $value,
262 6
                    $minLength);
263
        }
264 1
        return $value;
265
    }
266
    
267
    /**
268
     * Specifies the lower bounds for numeric values (the value must be greater than this value)
269
     * 
270
     * @param mixed $value
271
     * @param int $minExclusive
272
     * @throws RestrictionException
273
     * @return mixed
274
     */
275 3 View Code Duplication
    public static function checkMinExclusive($value, $minExclusive) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
276
    {
277 3
        $numeric = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
278 3
        if ($numeric <= $minExclusive) {
279 2
            throw new RestrictionException(
280 2
                    "The restriction min exclusive with '$minExclusive' is not true", 
281 2
                    RestrictionException::ERROR_CODE_MIN_EXCLUSIVE,
282 2
                    $value,
283 2
                    $minExclusive);
284
        }
285 1
        return $numeric;
286
    }
287
    
288
    /**
289
     * Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
290
     * 
291
     * @param mixed $value
292
     * @param int $minInclusive
293
     * @throws RestrictionException
294
     * @return mixed
295
     */
296 3 View Code Duplication
    public static function checkMinInclusive($value, $minInclusive) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
297
    {
298 3
        $numeric = static::getNumeric($value);
0 ignored issues
show
Bug introduced by
Since getNumeric() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getNumeric() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
299 3
        if ($numeric < $minInclusive) {
300 2
            throw new RestrictionException(
301 2
                    "The restriction min inclusive with '$minInclusive' is not true", 
302 2
                    RestrictionException::ERROR_CODE_MIN_INCLUSIVE,
303 2
                    $value,
304 2
                    $minInclusive);
305
        }
306 1
        return $numeric;
307
    }
308
309
    /**
310
     * Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
311
     * 
312
     * @param mixed $value
313
     * @param string $whiteSpace
314
     * @return mixed
315
     */
316 1
    public static function checkWhiteSpace($value, $whiteSpace) 
317
    {
318 1
        $output = $value;
319 1
        if ($whiteSpace == 'replace' || $whiteSpace == 'collapse') {
320 1
            $output = preg_replace('/\s/', ' ', $output);
321 1
        }
322 1
        if ($whiteSpace == 'collapse') {
323 1
            $output = preg_replace('/\s+/', ' ', $output);
324 1
        }
325 1
        return $output;
326
    }	
327
328
}
329