1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice; |
6
|
|
|
|
7
|
|
|
use Stadly\PasswordPolice\Rule\CouldNotUseRuleException; |
8
|
|
|
use Symfony\Component\Translation\Loader\MoFileLoader; |
9
|
|
|
use Symfony\Component\Translation\Translator; |
10
|
|
|
use Symfony\Contracts\Translation\LocaleAwareInterface; |
11
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
12
|
|
|
|
13
|
|
|
final class Policy |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array<Rule> Policy rules. |
17
|
|
|
*/ |
18
|
|
|
private $rules = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var (TranslatorInterface&LocaleAwareInterface)|null Translator for translating messages. |
22
|
|
|
*/ |
23
|
|
|
private $translator = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Rule ...$rules Policy rules. |
27
|
|
|
*/ |
28
|
13 |
|
public function __construct(Rule ...$rules) |
29
|
|
|
{ |
30
|
13 |
|
$this->addRules(...$rules); |
31
|
13 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Rule ...$rules Policy rules. |
35
|
|
|
*/ |
36
|
3 |
|
public function addRules(Rule ...$rules): void |
37
|
|
|
{ |
38
|
3 |
|
foreach ($rules as $rule) { |
39
|
3 |
|
$this->rules[] = $rule; |
40
|
|
|
} |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check whether a password is in compliance with the policy. |
45
|
|
|
* |
46
|
|
|
* @param Password|string $password Password to check. |
47
|
|
|
* @param int|null $weight Don't consider rule constraints with lower weights. |
48
|
|
|
* @return bool Whether the password is in compliance with the policy. |
49
|
|
|
* @throws CouldNotUseRuleException If an error occurred. |
50
|
|
|
*/ |
51
|
6 |
|
public function test($password, ?int $weight = null): bool |
52
|
|
|
{ |
53
|
6 |
|
foreach ($this->rules as $rule) { |
54
|
5 |
|
if (!$rule->test($password, $weight)) { |
55
|
5 |
|
return false; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Validate that a password is in compliance with the policy. |
64
|
|
|
* |
65
|
|
|
* @param Password|string $password Password to validate. |
66
|
|
|
* @return array<ValidationError> Validation errors describing why the password isn't in compliance with the policy. |
67
|
|
|
* @throws CouldNotUseRuleException If an error occurred. |
68
|
|
|
*/ |
69
|
2 |
|
public function validate($password): array |
70
|
|
|
{ |
71
|
2 |
|
$validationErrors = []; |
72
|
|
|
|
73
|
2 |
|
foreach ($this->rules as $rule) { |
74
|
2 |
|
$validationError = $rule->validate($password, $this->getTranslator()); |
75
|
|
|
|
76
|
2 |
|
if ($validationError !== null) { |
77
|
2 |
|
$validationErrors[] = $validationError; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
return $validationErrors; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param (TranslatorInterface&LocaleAwareInterface)|null $translator Translator for translating messages. |
86
|
|
|
*/ |
87
|
2 |
|
public function setTranslator(?TranslatorInterface $translator): void |
88
|
|
|
{ |
89
|
2 |
|
$this->translator = $translator; |
|
|
|
|
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return TranslatorInterface&LocaleAwareInterface Translator to use for the current instance. |
94
|
|
|
*/ |
95
|
2 |
|
public function getTranslator(): TranslatorInterface |
96
|
|
|
{ |
97
|
2 |
|
if ($this->translator === null) { |
98
|
1 |
|
$this->translator = new Translator('en_US'); |
|
|
|
|
99
|
1 |
|
$this->translator->addLoader('mo', new MoFileLoader()); |
100
|
1 |
|
$this->translator->addResource('mo', __DIR__ . '/../translations/messages.nn_NO.mo', 'nn_NO'); |
101
|
1 |
|
$this->translator->addResource('mo', __DIR__ . '/../translations/messages.nb_NO.mo', 'nb_NO'); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
return $this->translator; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.