ConditionalRule   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 44
ccs 12
cts 12
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 7 2
A test() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\Rule;
6
7
use Stadly\PasswordPolice\Password;
8
use Stadly\PasswordPolice\Rule;
9
use Stadly\PasswordPolice\ValidationError;
10
use Symfony\Contracts\Translation\TranslatorInterface;
11
12
final class ConditionalRule implements Rule
13
{
14
    /**
15
     * @var Rule Rule to test conditionally.
16
     */
17
    private $rule;
18
19
    /**
20
     * @var callable(Password|string): bool Condition function.
21
     */
22
    private $condition;
23
24
    /**
25
     * @param Rule $rule Rule to test if the condition is true.
26
     * @param callable(Password|string): bool $condition Condition function.
27
     */
28 4
    public function __construct(Rule $rule, callable $condition)
29
    {
30 4
        $this->rule = $rule;
31 4
        $this->condition = $condition;
32 4
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37 2
    public function test($password, ?int $weight = null): bool
38
    {
39 2
        if (($this->condition)($password)) {
40 1
            return $this->rule->test($password, $weight);
41
        }
42
43 1
        return true;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 2
    public function validate($password, TranslatorInterface $translator): ?ValidationError
50
    {
51 2
        if (($this->condition)($password)) {
52 1
            return $this->rule->validate($password, $translator);
53
        }
54
55 1
        return null;
56
    }
57
}
58