Completed
Push — master ( d69335...e1b0e6 )
by Kai
02:08
created

BcryptCalculator::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace SimpleHash\Calculator;
2
3
/**
4
 * Bcrypt hash calculator class
5
 *
6
 * @package    SimpleHash
7
 * @author     Kai Hempel <[email protected]>
8
 * @copyright  2016 Kai Hempel <[email protected]>
9
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
10
 * @link       https://www.kuweh.de/
11
 * @since      Class available since Release 1.0.0
12
 */
13
class BcryptCalculator implements HashCalculatorInterface
14
{
15
    const DEFAULT_COST = '10';
16
    const DEFAULT_SALT = 'Af13GgKoL503sCvf42dJ18';
17
18
    /**
19
     * Costs
20
     *
21
     * @var string
22
     */
23
    private $cost = '';
24
25
    /**
26
     * Salt
27
     *
28
     * @var string
29
     */
30
    private $salt = '';
31
32
    /**
33
     * Constructor
34
     *
35
     * @param array $params
36
     */
37 4
    public function __construct(array $params = array())
38
    {
39 4
        $this->setCostFromParams($params);
40 4
        $this->setSaltFromParams($params);
41 4
    }
42
43
    /**
44
     * Returns the Bcrypt hash
45
     *
46
     * @param string $data
47
     * @return string
48
     */
49 3
    public function getHash($data)
50
    {
51 3
        return crypt($data, $this->buildSaltString());
52
    }
53
54
    /**
55
     * Builds the final salt string for PHP crypt.
56
     * The blowfish type "$2y$" is used.
57
     *
58
     * @return string
59
     */
60 3
    private function buildSaltString()
61
    {
62 3
        return '$2y$' . $this->cost . '$' . $this->salt . '$';
63
    }
64
65
    /**
66
     * Sets the salt string from parameter array.
67
     * If no salt is given, the default value will be used!
68
     *
69
     * @return void
70
     */
71 4
    private function setSaltFromParams(array $params)
72
    {
73 4
        $salt = $this->getMatchingData($params, '/^[\.\\A-Za-z0-9]{22}$/');
74 4
        $this->salt = (! empty($salt)) ? $salt : self::DEFAULT_SALT;
75 4
    }
76
77
    /**
78
     * Sets the cost string from parameter array.
79
     * If no cost is given, the default value will be used!
80
     *
81
     * @return void
82
     */
83 4
    private function setCostFromParams(array $params)
84
    {
85 4
        $cost = (int)$this->getMatchingData($params, '/^[0-9]{1,2}$/');
86 4
        $this->cost = (string)(! empty($cost)) ? $cost : self::DEFAULT_COST;
1 ignored issue
show
Documentation Bug introduced by
It seems like (string) (!empty($cost))...st : self::DEFAULT_COST can also be of type integer. However, the property $cost is declared as type string. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
87 4
    }
88
89
    /**
90
     * Returns the first match from parameter array
91
     *
92
     * @param array $params
93
     * @param string $regex
94
     * @return string
95
     */
96 4
    private function getMatchingData(array $params, $regex)
97
    {
98 4
        foreach ($params as $data) {
99 1
            if (preg_match($regex, $data)) {
100 1
                return $data;
101
            }
102 4
        }
103
104 3
        return '';
105
    }
106
}
107