Completed
Push — master ( b6d955...929034 )
by Marcus
02:03
created

StrBetween::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
use Mbright\Validation\Rule\AbstractStringCase;
6
7 View Code Duplication
class StrBetween extends AbstractStringCase implements SanitizeRuleInterface
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
    /** @var int */
10
    protected $min;
11
12
    /** @var int */
13
    protected $max;
14
15
    /** @var string */
16
    protected $padString;
17
18
    /** @var int */
19
    protected $padType;
20
21
    /**
22
     * @param int $min The minimum length.
23
     * @param int $max The maximum length.
24
     * @param string $pad_string Pad using this string.
0 ignored issues
show
Bug introduced by
There is no parameter named $pad_string. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     * @param int $pad_type A `STR_PAD_*` constant.
0 ignored issues
show
Bug introduced by
There is no parameter named $pad_type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     *
27
     */
28 39
    public function __construct(int $min, int $max, string $padString = ' ', int $padType = STR_PAD_RIGHT)
29
    {
30 39
        $this->min = $min;
31 39
        $this->max = $max;
32 39
        $this->padString = $padString;
33 39
        $this->padType = $padType;
34 39
    }
35
36
    /**
37
     * Sanitizes a string to a length range by padding or chopping it.
38
     *
39
     * @param object $subject The subject to be filtered.
40
     * @param string $field The subject field name.
41
     *
42
     * @return bool True if the value was sanitized, false if not.
43
     */
44 39
    public function __invoke($subject, string $field): bool
45
    {
46 39
        $value = $subject->$field;
47 39
        if (!is_scalar($value)) {
48 3
            return false;
49
        }
50 36
        if ($this->strlen($value) < $this->min) {
51 6
            $subject->$field = $this->strpad($value, $this->min, $this->padString, $this->padType);
52
        }
53 36
        if ($this->strlen($value) > $this->max) {
54 12
            $subject->$field = $this->substr($value, 0, $this->max);
55
        }
56
57 36
        return true;
58
    }
59
}
60