Failed Conditions
Pull Request — master (#75)
by
unknown
01:48
created

Strings::filter()   C

Complexity

Conditions 15
Paths 16

Size

Total Lines 46
Code Lines 25

Duplication

Lines 3
Ratio 6.52 %

Importance

Changes 0
Metric Value
dl 3
loc 46
rs 5.0381
c 0
b 0
f 0
cc 15
eloc 25
nc 16
nop 4

How to fix   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
namespace TraderInteractive\Filter;
4
5
/**
6
 * A collection of filters for strings.
7
 */
8
final class Strings
9
{
10
    /**
11
     * Filter a string.
12
     *
13
     * Verify that the passed in value  is a string.  By default, nulls are not allowed, and the length is restricted
14
     * between 1 and PHP_INT_MAX.  These parameters can be overwritten for custom behavior.
15
     *
16
     * The return value is the string, as expected by the \TraderInteractive\Filterer class.
17
     *
18
     * @param mixed $value The value to filter.
19
     * @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed.
20
     * @param int $minLength Minimum length to allow for $value.
21
     * @param int $maxLength Maximum length to allow for $value.
22
     * @return string|null The passed in $value.
23
     *
24
     * @throws Exception if the value did not pass validation.
25
     * @throws \InvalidArgumentException if one of the parameters was not correctly typed.
26
     */
27
    public static function filter($value, bool $allowNull = false, int $minLength = 1, int $maxLength = PHP_INT_MAX)
28
    {
29
        if ($minLength < 0) {
30
            throw new \InvalidArgumentException('$minLength was not a positive integer value');
31
        }
32
33
        if ($maxLength < 0) {
34
            throw new \InvalidArgumentException('$maxLength was not a positive integer value');
35
        }
36
37
        if ($allowNull === true && $value === null) {
38
            return null;
39
        }
40
41
        if (is_scalar($value)) {
42
            $value = "{$value}";
43
        }
44
45
        if (is_object($value) && method_exists($value, '__toString')) {
46
            $value = (string)$value;
47
        }
48
49 View Code Duplication
        if (!is_string($value)) {
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...
50
            throw new Exception("Value '" . var_export($value, true) . "' is not a string");
51
        }
52
53
        $valueLength = strlen($value);
54
55
        if ($valueLength < $minLength || $valueLength > $maxLength) {
56
            throw new Exception(
57
                sprintf(
58
                    "Value '%s' with length '%d' is less than '%d' or greater than '%d'",
59
                    $value,
60
                    $valueLength,
61
                    $minLength,
62
                    $maxLength
63
                )
64
            );
65
        }
66
67
        return $value;
68
    }
69
70
    /**
71
     * Explodes a string into an array using the given delimiter.
72
     *
73
     * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz'].
74
     *
75
     * @param string $value The string to explode.
76
     * @param string $delimiter The non-empty delimiter to explode on.
77
     * @return array The exploded values.
78
     *
79
     * @throws \InvalidArgumentException if the delimiter does not pass validation.
80
     */
81
    public static function explode(string $value, string $delimiter = ',')
82
    {
83
        if (empty($delimiter)) {
84
            throw new \InvalidArgumentException(
85
                "Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string"
86
            );
87
        }
88
89
        return explode($delimiter, $value);
90
    }
91
}
92