Passed
Push — master ( ad2baf...e1d221 )
by Florian
01:09
created

AbstractPasswordHasher   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 52
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setSalt() 0 13 2
A needsRehash() 0 3 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://cakephp.org CakePHP(tm) Project
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
namespace Phauthentic\PasswordHasher;
16
17
use InvalidArgumentException;
18
19
/**
20
 * Abstract password hashing class
21
 */
22
abstract class AbstractPasswordHasher implements PasswordHasherInterface
23
{
24
    const SALT_BEFORE = 'before';
25
    const SALT_AFTER = 'after';
26
27
    /**
28
     * Salt
29
     *
30
     * @return void
31
     */
32
    protected $salt = '';
33
34
    /**
35
     * Position of the salt
36
     *
37
     * @var string
38
     */
39
    protected $saltPosition;
40
41
    /**
42
     * Sets the salt if any was used
43
     *
44
     * @return $this
45
     */
46
    public function setSalt(string $salt, string $position = self::SALT_AFTER): self
47
    {
48
        if (!in_array($position, [self::SALT_BEFORE, self::SALT_AFTER])) {
49
            throw new InvalidArgumentException(sprintf(
50
                '`%s` is an invalud argument, it has to be `` or ``',
51
                $position,
52
                self::SALT_BEFORE,
53
                self::SALT_AFTER
54
            ));
55
        }
56
57
        $this->salt = $salt;
58
        $this->saltPosition = $position;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Phauthentic\PasswordHasher\AbstractPasswordHasher. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
59
    }
60
61
    /**
62
     * Returns true if the password need to be rehashed, due to the password being
63
     * created with anything else than the passwords generated by this class.
64
     *
65
     * Returns true by default since the only implementation users should rely
66
     * on is the one provided by default in php 5.5+ or any compatible library
67
     *
68
     * @param string $password The password to verify
69
     * @return bool
70
     */
71
    public function needsRehash(string $password): bool
72
    {
73
        return password_needs_rehash($password, PASSWORD_DEFAULT);
74
    }
75
}
76