Password   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 55
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPattern() 0 4 1
A matchHash() 0 5 2
A validate() 0 4 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 1
	public function getPattern() : string
36
	{
37 1
		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
	 */
49
50 10
	public function validate(string $password = null) : bool
51
	{
52 10
		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
	 * @return bool
64
	 */
65
66 4
	public function matchHash(string $password = null, string $hash = null) : bool
67
	{
68 4
		$passwordHash = new Hash();
69 4
		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