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

Length::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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