Passed
Push — master ( ff74ae...3481b7 )
by Rick
01:43
created

Ranges::floatBetween()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 1
nop 2
crap 3
1
<?php
2
/**
3
 * Copyright 2017 NanoSector
4
 *
5
 * You should have received a copy of the MIT license with the project.
6
 * See the LICENSE file for more information.
7
 */
8
9
namespace ValidationClosures;
10
11
12
class Ranges
13
{
14
	/**
15
	 * @param int $minimumLength
16
	 * @param int $maximumLength
17
	 *
18
	 * @return \Closure
19
	 */
20 4
	public static function stringWithLengthBetween(int $minimumLength, int $maximumLength): \Closure
21
	{
22 4
		if ($maximumLength < 0 || $maximumLength < 1)
23 1
			throw new \InvalidArgumentException('Minimum length cannot be below 0, maximum length cannot be below 1');
24
25
		return function ($value) use ($minimumLength, $maximumLength)
26
		{
27 3
			return Types::string()($value) && static::intBetween($minimumLength, $maximumLength)(strlen($value));
28 3
		};
29
	}
30
31
	/**
32
	 * @param int $minimum
33
	 * @param int $maximum
34
	 *
35
	 * @return \Closure
36
	 */
37 4 View Code Duplication
	public static function intBetween(int $minimum, int $maximum): \Closure
1 ignored issue
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...
38
	{
39
		return function ($value) use ($minimum, $maximum)
40
		{
41 4
			return Types::int()($value) && ($value >= $minimum && $value <= $maximum);
42 4
		};
43
	}
44
45
	/**
46
	 * @param float $minimum
47
	 * @param float $maximum
48
	 *
49
	 * @return \Closure
50
	 */
51 1 View Code Duplication
	public static function floatBetween(float $minimum, float $maximum): \Closure
1 ignored issue
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...
52
	{
53
		return function ($value) use ($minimum, $maximum)
54
		{
55 1
			return Types::float()($value) && ($value >= $minimum && $value <= $maximum);
56 1
		};
57
	}
58
59
	/**
60
	 * @param array ...$allowedValues
61
	 *
62
	 * @return \Closure
63
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedValues a bit more specific; maybe use array[].
Loading history...
64 1
	public static function enum(...$allowedValues): \Closure
65
	{
66
		return function ($value) use ($allowedValues)
67
		{
68 1
			return in_array($value, $allowedValues, true);
69 1
		};
70
	}
71
72
	/**
73
	 * @param array ...$allowedTypes
74
	 *
75
	 * @return \Closure
76
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedTypes a bit more specific; maybe use array[].
Loading history...
77 1
	public static function typeEnum(...$allowedTypes): \Closure
78
	{
79
		return function ($value) use ($allowedTypes)
80
		{
81 1
			return in_array(gettype($value), $allowedTypes, true);
82 1
		};
83
	}
84
85
	/**
86
	 * @param array ...$allowedValues
87
	 *
88
	 * @return \Closure
89
	 */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $allowedValues a bit more specific; maybe use array[].
Loading history...
90 3
	public static function stringOneOf(...$allowedValues): \Closure
91
	{
92 3
		if (!Utils::validateArray(Types::string(), $allowedValues))
93 1
			throw new \InvalidArgumentException('Ranges::stringOneOf expects arguments of type string only');
94
95 2
		return function ($value) use ($allowedValues)
96
		{
97 2
			return Types::string() && in_array($value, $allowedValues);
98 2
		};
99
	}
100
}