Passed
Push — main ( bfc7cf...f88b94 )
by Eric
02:00
created

Xxh32Hasher::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 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
 * @see https://xxhash.com/
22
 */
23
class Xxh32Hasher implements HasherInterface
24
{
25
    /**
26
     * {@inheritDoc}
27
     *
28
     * 8 hexits = 32bit, which also allows us to forego having to check whether
29
     * it is over PHP_INT_MAX.
30
     *
31
     * The substring is converted to an int since hex strings sometimes get
32
     * treated as ints if all digits are ints and this results in unexpected
33
     * sorting order.
34
     */
35 2
    public function hash(string $string): int
36
    {
37 2
        return (int) hexdec(substr(hash('xxh32', $string), 0, 8));
38
    }
39
}
40