• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / lock / 17475241346

04 Sep 2025 07:59PM UTC coverage: 5.5%. Remained the same
17475241346

push

github

BenMorel
Apply coding standard

11 of 200 relevant lines covered (5.5%)

3.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/Internal/PostgresHasher.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Lock\Internal;
6

7
use function sha1;
8
use function substr;
9
use function unpack;
10

11
use const PHP_INT_SIZE;
12

13
final class PostgresHasher
14
{
15
    /**
16
     * Returns a pair of 32-bit integers that can be used as PostgreSQL advisory lock keys.
17
     *
18
     * Although Postgres supports using a single 64-bit key, we use 32-bit keys for portability on 32-bit systems.
19
     *
20
     * @return array{int, int}
21
     */
22
    public static function hashLockName(string $lockName): array
23
    {
24
        $hash = sha1($lockName, true);
30✔
25

26
        return [
30✔
27
            self::unpackSigned32bit(substr($hash, 0, 4)),
30✔
28
            self::unpackSigned32bit(substr($hash, 4, 4)),
30✔
29
        ];
30✔
30
    }
31

32
    public static function unpackSigned32bit(string $binary): int
33
    {
34
        /** @var array{1: int} $parts */
35
        $parts = unpack('N', $binary); // unsigned long (always 32 bit, big endian byte order)
110✔
36

37
        $value = $parts[1];
110✔
38

39
        if (PHP_INT_SIZE === 4) {
110✔
40
            // already signed on 32-bit systems, even though it's documented as unsigned!
41
            return $value;
×
42
        }
43

44
        // unsigned on 64-bit systems, convert to signed
45
        if ($value > 0x7FFFFFFF) {
110✔
46
            $value -= 0x100000000;
60✔
47
        }
48

49
        return $value;
110✔
50
    }
51
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc