Completed
Push — master ( bcf360...98d48e )
by Abdala
03:37
created

Length   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 10 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CustomerGauge\Password\Rule;
6
7
use CustomerGauge\Password\Exception\InvalidLength;
8
use CustomerGauge\Password\Rule;
9
use function mb_strlen;
10
11
final class Length implements Rule
12
{
13
    /** @var int */
14
    private $min;
15
16
    /** @var int|null */
17
    private $max;
18
19
    /** @var string */
20
    private $encoding;
21
22 2
    public function __construct(int $min, ?int $max = null, string $encoding = 'utf8')
23
    {
24 2
        $this->min      = $min;
25 2
        $this->max      = $max;
26 2
        $this->encoding = $encoding;
27 2
    }
28
29 2
    public function __invoke(string $password) : void
30
    {
31 2
        $length = (int) mb_strlen($password, $this->encoding);
32
33 2
        if ($length < $this->min) {
34 1
            throw InvalidLength::requires('min', $this->min, $length);
35
        }
36
37 1
        if ($this->max && $length > $this->max) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
38 1
            throw InvalidLength::requires('max', $this->max, $length);
39
        }
40
    }
41
}
42