Md5Hasher   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 16
ccs 2
cts 2
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A hash() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\ConsistentHash.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 * (c) Paul Annesley <[email protected]>
10
 *
11
 * This source file is subject to the MIT license. For the full copyright and
12
 * license information, please view the LICENSE file that was distributed with
13
 * this source code.
14
 */
15
16
namespace Esi\ConsistentHash\Hasher;
17
18
use Override;
19
20
use function hexdec;
21
use function md5;
22
use function substr;
23
24
/**
25
 * Uses MD5 to hash a value into a 32bit int.
26
 */
27
final class Md5Hasher implements HasherInterface
28
{
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * 8 hexits = 32bit, which also allows us to forego having to check whether
33
     * it is over PHP_INT_MAX.
34
     *
35
     * The substring is converted to an int since hex strings sometimes get
36
     * treated as ints if all digits are ints and this results in unexpected
37
     * sorting order.
38
     */
39 2
    #[Override]
40
    public function hash(string $string): int
41
    {
42 2
        return (int) hexdec(substr(md5($string), 0, 8));
43
    }
44
}
45