Murmur3Hasher   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
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 hash;
21
use function hexdec;
22
use function substr;
23
24
/**
25
 * Uses MurmurHash3 to hash a value into a 32bit int.
26
 *
27
 * Note: specifically uses the 'murmur3a' algo in PHP's hash().
28
 *
29
 * @todo Look into murmur3c (128-bit hash on x86 architecture) and murmur3f (128-bit hash on x64 architecture)
30
 *
31
 * @see https://github.com/aappleby/smhasher
32
 */
33
final class Murmur3Hasher implements HasherInterface
34
{
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * 8 hexits = 32bit, which also allows us to forego having to check whether
39
     * it is over PHP_INT_MAX.
40
     *
41
     * The substring is converted to an int since hex strings sometimes get
42
     * treated as ints if all digits are ints and this results in unexpected
43
     * sorting order.
44
     */
45 2
    #[Override]
46
    public function hash(string $string): int
47
    {
48 2
        return (int) hexdec(substr(hash('murmur3a', $string), 0, 8));
49
    }
50
}
51