Completed
Push — master ( 9458ed...7d322b )
by Henry
10:04
created

Password::getFormPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript\Validator;
3
4
use Redaxscript\Hash;
5
use function preg_match;
6
7
/**
8
 * children class to validate password
9
 *
10
 * @since 4.3.0
11
 *
12
 * @package Redaxscript
13
 * @category Validator
14
 * @author Henry Ruhs
15
 */
16
17
class Password implements ValidatorInterface
18
{
19
	/**
20
	 * pattern for password
21
	 *
22
	 * @var string
23
	 */
24
25
	protected $_pattern = '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])\S{10,100}$';
26
27
	/**
28
	 * get the pattern
29
	 *
30
	 * @since 4.3.0
31
	 *
32
	 * @return string
33
	 */
34
35
	public function getPattern() : string
36
	{
37
		return $this->_pattern;
38
	}
39
40
	/**
41
	 * validate the password
42
	 *
43
	 * @since 4.3.0
44
	 *
45
	 * @param string $password password to be validated
46
	 *
47
	 * @return bool
48 1
	 */
49
50 1
	public function validate(string $password = null) : bool
51
	{
52
		return preg_match('/' . $this->_pattern . '/', $password);
53
	}
54
55
	/**
56
	 * match password hash
57
	 *
58
	 * @since 4.3.0
59
	 *
60
	 * @param string $password password to be validated
61
	 * @param string $hash hash to be validated
62
	 *
63 8
	 * @return bool
64
	 */
65 8
66 8
	public function matchHash(string $password = null, string $hash = null) : bool
67
	{
68
		$passwordHash = new Hash();
69
		return $password && $passwordHash->validate($password, $hash);
0 ignored issues
show
Bug Best Practice introduced by
The expression $password of type null|string is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
70
	}
71
}
72