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

StrlenMin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
use Mbright\Validation\Rule\AbstractStringCase;
6
7
class StrlenMin extends AbstractStringCase implements SanitizeRuleInterface
8
{
9
    /** @var int */
10
    protected $min;
11
12
    /** @var string */
13
    protected $padString;
14
15
    /** @var int */
16
    protected $padType;
17
18
19
    /**
20
     * @param int $min The minimum length.
21
     * @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...
22
     * @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...
23
     */
24 21
    public function __construct(int $min, string $padString = ' ', int $padType = STR_PAD_RIGHT)
25
    {
26 21
        $this->min = $min;
27 21
        $this->padString = $padString;
28 21
        $this->padType = $padType;
29 21
    }
30
31
    /**
32
     * Sanitizes a string to a minimum length by padding it.
33
     *
34
     * @param object $subject The subject to be filtered.
35
     * @param string $field The subject field name.
36
     *
37
     * @return bool True if the value was sanitized, false if not.
38
     */
39 21
    public function __invoke($subject, string $field): bool
40
    {
41 21
        $value = $subject->$field;
42 21
        if (!is_scalar($value)) {
43 3
            return false;
44
        }
45
46 18
        if ($this->strlen($value) < $this->min) {
47 6
            $subject->$field = $this->strpad($value, $this->min, $this->padString, $this->padType);
48
        }
49
50 18
        return true;
51
    }
52
}
53