Xxh32Hasher   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 15
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 15
ccs 2
cts 2
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A hash() 0 3 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
/**
19
 * Uses XXH32 to hash a value into a 32bit int.
20
 *
21
 * @todo Look into xxh64 (64-bit hash), xxh3 (64-bit hash), and xxh128 (128-bit hash)
22
 *
23
 * @see https://xxhash.com/
24
 */
25
class Xxh32Hasher implements HasherInterface
26
{
27
    /**
28
     * {@inheritDoc}
29
     *
30
     * 8 hexits = 32bit, which also allows us to forego having to check whether
31
     * it is over PHP_INT_MAX.
32
     *
33
     * The substring is converted to an int since hex strings sometimes get
34
     * treated as ints if all digits are ints and this results in unexpected
35
     * sorting order.
36
     */
37 2
    public function hash(string $string): int
38
    {
39 2
        return (int) hexdec(substr(hash('xxh32', $string), 0, 8));
40
    }
41
}
42