Completed
Pull Request — master (#75)
by
unknown
01:26
created

Strings   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 93
Duplicated Lines 6.45 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 1
dl 6
loc 93
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C filter() 3 42 11
A explode() 0 10 2

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
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