Password   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 10 2
A verify() 0 4 1
1
<?php
2
3
namespace Controlabs\Helper;
4
5
class Password
6
{
7
    protected const SHA_512 = 'sha512';
8
9 1
    public function encrypt(string $password, string $salt = null, string $algorithm = self::SHA_512): PasswordData
10
    {
11 1
        if (!$salt) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $salt of type null|string is loosely compared to false; 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...
12 1
            $salt = hash($algorithm, uniqid(strval(mt_rand(1, mt_getrandmax())), true));
13
        }
14
15 1
        $password = hash($algorithm, $password . $salt);
16
17 1
        return new PasswordData($password, $salt);
18
    }
19
20 1
    public function verify(string $encrypted, string $password, string $salt, string $algorithm = self::SHA_512): bool
21
    {
22 1
        return $encrypted === $this->encrypt($password, $salt, $algorithm)->password();
23
    }
24
}
25