From 39aca2f7331b8d9835d33b9107c8afc0e065802d Mon Sep 17 00:00:00 2001 From: Guillaume Ernst Date: Mon, 28 Sep 2026 16:21:44 -0400 Subject: [PATCH] fix: avoid PHP 8.5 deprecation in applyComplement --- src/BitArray/BitArray.php | 2 +- tests/BitArray/BitArrayTest.php | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/BitArray/BitArray.php b/src/BitArray/BitArray.php index 06cfbec..e17d8ca 100644 --- a/src/BitArray/BitArray.php +++ b/src/BitArray/BitArray.php @@ -636,7 +636,7 @@ public function applyComplement() $length = strlen($this->data); for ($index = 0; $index < $length; $index++) { - $this->data[$index] = chr(~ ord($this->data[$index])); + $this->data[$index] = chr((~ ord($this->data[$index])) & 0xFF); } return $this->restrict(); diff --git a/tests/BitArray/BitArrayTest.php b/tests/BitArray/BitArrayTest.php index 5f29ec1..b53f740 100644 --- a/tests/BitArray/BitArrayTest.php +++ b/tests/BitArray/BitArrayTest.php @@ -747,6 +747,41 @@ public function testApplyComplement() ); } + /** + * Tests BitArray::applyComplement does not trigger a deprecation on PHP 8.5 + * + * @return void + */ + public function testApplyComplementDoesNotTriggerDeprecation() + { + if (PHP_VERSION_ID < 80500) { + $this->markTestSkipped('This test requires PHP 8.5 or later'); + } + + set_error_handler( + static function ( + int $severity, + string $message, + string $filename, + int $line + ): never { + throw new \ErrorException($message, 0, $severity, $filename, $line); + }, + E_DEPRECATED + ); + + try { + $bits = BitArray::fromString('10011'); + + $this->assertEquals( + '01100', + (string) $bits->applyComplement() + ); + } finally { + restore_error_handler(); + } + } + /** * Tests BitArray::applyOr *