Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit c200ac5

Browse files
committed
Improve Exception handling
1 parent 7e581ff commit c200ac5

File tree

9 files changed

+70
-47
lines changed

9 files changed

+70
-47
lines changed

src/Crc32/IsoHdlc/Computer.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
use Awesomized\Checksums;
88
use FFI;
9+
use FFI\Exception;
910

1011
/**
1112
* A wrapper around the CRC-32/ISO-HDLC FFI library.
1213
*
14+
* This produces output compatible with crc32() and hash('crc32b') in PHP at >10X the speed.
15+
*
16+
* Input of "123456789" (no quotes) should produce a checksum of 0xCBF43926
17+
*
1318
* @link https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32-iso-hdlc
1419
*/
1520
final class Computer implements Checksums\CrcInterface
@@ -26,27 +31,20 @@ final class Computer implements Checksums\CrcInterface
2631
* @param FFI|null $crc32IsoHdlc The FFI instance for the CRC-32 IEEE library.
2732
*
2833
* @throws \InvalidArgumentException
34+
* @throws Exception
2935
*/
3036
public function __construct(
3137
?FFI $crc32IsoHdlc = null,
3238
) {
3339
$this->crc32IsoHdlc = $crc32IsoHdlc ?? self::getFfi();
3440

35-
try {
36-
/**
37-
* @var FFI\CData $hasherHandle
38-
*
39-
* @psalm-suppress UndefinedMethod - from FFI, we'll catch the Exception if the method is missing
40-
*/
41-
// @phpstan-ignore-next-line
42-
$hasherHandle = $this->crc32IsoHdlc->hasher_new();
43-
} catch (FFI\Exception $e) {
44-
throw new \InvalidArgumentException(
45-
message: 'Could not create a new Hasher handle.'
46-
. ' Is the library loaded, and has the hasher_new() method?',
47-
previous: $e,
48-
);
49-
}
41+
/**
42+
* @var FFI\CData $hasherHandle
43+
*
44+
* @psalm-suppress UndefinedMethod - from FFI, we'll catch the Exception if the method is missing
45+
*/
46+
// @phpstan-ignore-next-line
47+
$hasherHandle = $this->crc32IsoHdlc->hasher_new();
5048

5149
$this->hasherHandle = $hasherHandle;
5250
}
@@ -62,7 +60,7 @@ public function write(
6260
$string,
6361
\strlen($string),
6462
);
65-
} catch (FFI\Exception $e) {
63+
} catch (Exception $e) {
6664
throw new \RuntimeException(
6765
message: 'Could not write to the Digest handle. '
6866
. 'Is the library loaded, and has the digest_write() method?',
@@ -85,7 +83,7 @@ public function sum(): string
8583
$crc32 = $this->crc32IsoHdlc->hasher_finalize(
8684
$this->hasherHandle,
8785
);
88-
} catch (FFI\Exception $e) {
86+
} catch (Exception $e) {
8987
throw new \RuntimeException(
9088
message: 'Could not calculate the CRC-32 checksum. '
9189
. ' Is the library loaded, and has the hasher_finalize() method?',
@@ -101,6 +99,7 @@ public function sum(): string
10199

102100
/**
103101
* @throws \InvalidArgumentException
102+
* @throws Exception
104103
*/
105104
protected static function getFfi(): FFI
106105
{

src/Crc64/Nvme/Computer.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
use Awesomized\Checksums;
88
use FFI;
9+
use FFI\Exception;
910

1011
/**
11-
* A wrapper around the CRC-64/NVME FFI library.
12+
* A wrapper around the CRC-64/NVME FFI library which calculates checksums at >20GiB/s on modern CPUs.
13+
*
14+
* Input of "123456789" (no quotes) should produce a checksum of 0xAE8B14860A799888.
1215
*
13-
* @see \Awesomized\Checksums\Crc64\Nvme\Ffi
1416
* @link https://github.com/awesomized/crc64fast-nvme
1517
* @link https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme
1618
*/
@@ -25,30 +27,23 @@ final class Computer implements Checksums\CrcInterface
2527
private static ?FFI $ffiAuto = null;
2628

2729
/**
28-
* @param FFI $crc64Nvme The FFI instance for the CRC-64 NVMe library.
30+
* @param FFI|null $crc64Nvme The FFI instance for the CRC-64 NVMe library.
2931
*
3032
* @throws \InvalidArgumentException
33+
* @throws Exception
3134
*/
3235
public function __construct(
3336
?FFI $crc64Nvme = null,
3437
) {
3538
$this->crc64Nvme = $crc64Nvme ?? self::getFfi();
3639

37-
try {
38-
/**
39-
* @var FFI\CData $digestHandle
40-
*
41-
* @psalm-suppress UndefinedMethod - from FFI, we'll catch the Exception if the method is missing
42-
*/
43-
// @phpstan-ignore-next-line
44-
$digestHandle = $this->crc64Nvme->digest_new();
45-
} catch (FFI\Exception $e) {
46-
throw new \InvalidArgumentException(
47-
message: 'Could not create a new Digest handle.'
48-
. ' Is the library loaded, and has the digest_new() method?',
49-
previous: $e,
50-
);
51-
}
40+
/**
41+
* @var FFI\CData $digestHandle
42+
*
43+
* @psalm-suppress UndefinedMethod - from FFI, we'll catch the Exception if the method is missing
44+
*/
45+
// @phpstan-ignore-next-line
46+
$digestHandle = $this->crc64Nvme->digest_new();
5247

5348
$this->digestHandle = $digestHandle;
5449
}
@@ -64,7 +59,7 @@ public function write(
6459
$string,
6560
\strlen($string),
6661
);
67-
} catch (FFI\Exception $e) {
62+
} catch (Exception $e) {
6863
throw new \RuntimeException(
6964
message: 'Could not write to the Digest handle. '
7065
. 'Is the library loaded, and has the digest_write() method?',
@@ -87,7 +82,7 @@ public function sum(): string
8782
$crc64 = $this->crc64Nvme->digest_sum64(
8883
$this->digestHandle,
8984
);
90-
} catch (FFI\Exception $e) {
85+
} catch (Exception $e) {
9186
throw new \RuntimeException(
9287
message: 'Could not calculate the CRC-64 checksum. '
9388
. ' Is the library loaded, and has the digest_sum64() method?',
@@ -103,6 +98,7 @@ public function sum(): string
10398

10499
/**
105100
* @throws \InvalidArgumentException
101+
* @throws Exception
106102
*/
107103
protected static function getFfi(): FFI
108104
{

src/CrcInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Awesomized\Checksums;
88
use FFI;
9+
use FFI\Exception;
910

1011
/**
1112
* A common Interface for CRC checksum calculations via FFI (Foreign Function Interface) libraries.
@@ -34,6 +35,7 @@ interface CrcInterface
3435
*
3536
* @throws \InvalidArgumentException
3637
* @throws \RuntimeException
38+
* @throws Exception
3739
*/
3840
public static function calculate(
3941
string $string,
@@ -53,6 +55,7 @@ public static function calculate(
5355
*
5456
* @throws \InvalidArgumentException
5557
* @throws \RuntimeException
58+
* @throws Exception
5659
*/
5760
public static function calculateFile(
5861
string $filename,

src/FfiInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ interface FfiInterface
2222
public const string OS_DARWIN = 'Darwin';
2323
public const string OS_WINDOWS = 'Windows';
2424

25+
/**
26+
* @throws \InvalidArgumentException
27+
* @throws Exception
28+
*/
29+
public static function fromAuto(): \FFI;
30+
2531
/**
2632
* Creates a new FFI instance from the given C declarations and library name.
2733
*
@@ -43,6 +49,8 @@ public static function fromCode(
4349
* @link https://www.php.net/manual/en/ffi.examples-complete.php
4450
*
4551
* @param string $ffiScopeName The FFI_SCOPE used during preloading
52+
*
53+
* @throws Exception
4654
*/
4755
public static function fromPreloadScope(
4856
string $ffiScopeName = self::SCOPE_DEFAULT,
@@ -54,6 +62,7 @@ public static function fromPreloadScope(
5462
* @param string $headerFile The C header file
5563
*
5664
* @throws \InvalidArgumentException
65+
* @throws Exception
5766
*/
5867
public static function fromHeaderFile(
5968
string $headerFile = '',

src/FfiTrait.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ trait FfiTrait
1717
*/
1818
private static array $ffis = [];
1919

20-
/**
21-
* @throws \InvalidArgumentException
22-
*/
2320
public static function fromAuto(): \FFI
2421
{
2522
try {

tests/unit/Crc32/IsoHdlc/ComputerTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Awesomized\Checksums\Crc32;
88
use Awesomized\Checksums\tests\unit\Definitions;
99
use FFI;
10+
use FFI\Exception;
1011
use PHPUnit\Framework\TestCase;
1112
use Random\RandomException;
1213

@@ -19,6 +20,7 @@ final class ComputerTest extends TestCase
1920

2021
/**
2122
* @throws \InvalidArgumentException
23+
* @throws Exception
2224
*/
2325
protected function setUp(): void
2426
{
@@ -27,11 +29,11 @@ protected function setUp(): void
2729

2830
/**
2931
* @throws \InvalidArgumentException
30-
* @throws \FFI\Exception
32+
* @throws Exception
3133
*/
3234
public function testConstructorInvalidLibraryShouldFail(): void
3335
{
34-
$this->expectException(\InvalidArgumentException::class);
36+
$this->expectException(Exception::class);
3537

3638
$ffi = \FFI::cdef();
3739

@@ -44,12 +46,13 @@ public function testConstructorInvalidLibraryShouldFail(): void
4446
* @depends testConstructorInvalidLibraryShouldFail
4547
*
4648
* @throws \InvalidArgumentException
49+
* @throws Exception
4750
*/
4851
public function testConstructorValidLibraryShouldSucceed(): void
4952
{
5053
$this->expectNotToPerformAssertions();
5154

52-
$ffi = new Crc32\IsoHdlc\Computer(
55+
new Crc32\IsoHdlc\Computer(
5356
crc32IsoHdlc: $this->ffi,
5457
);
5558
}
@@ -59,6 +62,7 @@ public function testConstructorValidLibraryShouldSucceed(): void
5962
*
6063
* @throws \InvalidArgumentException
6164
* @throws \RuntimeException
65+
* @throws Exception
6266
*/
6367
public function testCalculateHelloWorldShouldSucceed(): void
6468
{
@@ -77,6 +81,7 @@ public function testCalculateHelloWorldShouldSucceed(): void
7781
*
7882
* @throws \InvalidArgumentException
7983
* @throws \RuntimeException
84+
* @throws Exception
8085
*/
8186
public function testCalculateFileHelloWorldShouldSucceed(): void
8287
{
@@ -99,6 +104,7 @@ public function testCalculateFileHelloWorldShouldSucceed(): void
99104
* @throws \InvalidArgumentException
100105
* @throws \RuntimeException
101106
* @throws RandomException
107+
* @throws Exception
102108
*/
103109
public function testCalculateBinaryDataShouldSucceed(): void
104110
{
@@ -114,6 +120,7 @@ public function testCalculateBinaryDataShouldSucceed(): void
114120
*
115121
* @throws \InvalidArgumentException
116122
* @throws \RuntimeException
123+
* @throws Exception
117124
*/
118125
public function testCalculateChunkedDataShouldSucceed(): void
119126
{
@@ -133,6 +140,7 @@ public function testCalculateChunkedDataShouldSucceed(): void
133140
*
134141
* @throws \InvalidArgumentException
135142
* @throws \RuntimeException
143+
* @throws Exception
136144
*/
137145
public function testCalculateCheckValueShouldMatch(): void
138146
{
@@ -151,6 +159,7 @@ public function testCalculateCheckValueShouldMatch(): void
151159
*
152160
* @throws \InvalidArgumentException
153161
* @throws \RuntimeException
162+
* @throws Exception
154163
*/
155164
public function testComparePhpFunctionKnownValuesShouldMatch(): void
156165
{

tests/unit/Crc32/IsoHdlc/FfiTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function testFfiFromCodeInvalidLibraryShouldFail(): void
6969
* @depends testFfiFromCodeInvalidLibraryShouldFail
7070
*
7171
* @throws \InvalidArgumentException
72+
* @throws \FFI\Exception
7273
*/
7374
public function testFfiFromHeaderInvalidHeaderShouldFail(): void
7475
{

0 commit comments

Comments
 (0)