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 *