Conversation
🔍 Test Results - PHP 8.4❌ Code Style Check Failed Code style violations found. Please run ✅ Tests Passed ✅ Code Coverage: 100% This comment was automatically generated by the test workflow. |
There was a problem hiding this comment.
Pull Request Overview
This PR extends the BinaryReader with support for little-endian integer formats and 64-bit integers. It introduces a new IntType enum to handle various integer types and their properties, while updating the reading logic to support endianness conversion and platform-specific integer size validation.
- Replaces specific integer reading methods with a unified
readIntmethod that uses IntType enum - Adds comprehensive test coverage for all integer types including little-endian variants
- Implements endianness conversion and platform compatibility checks
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/IntType.php | New enum defining integer types with helper methods for bytes, signedness, and endianness |
| src/BinaryReader.php | Updated to use unified readInt method with endianness handling and platform size validation |
| tests/BinaryReaderTest.php | Comprehensive test suite covering all integer types and endianness combinations |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
@codex review |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
tests/BinaryWriterTest.php:1
- The Deprecated attribute should be
#[\\Deprecated('Use writeInt(IntType::UINT16, $value) instead')]with a backslash prefix to properly reference the global attribute class.
<?php declare(strict_types=1);
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Codex Review: Here are some suggestions.
Reply with @codex fix comments to fix any unresolved comments.
About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Codex Review: Here are some suggestions.
Reply with @codex fix comments to fix any unresolved comments.
About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/BinaryReader.php:1
- The error message uses string interpolation but doesn't provide context about valid range. Consider changing to 'Invalid seek position: $value (valid range: 0-{$this->length})'.
<?php declare(strict_types=1);
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
…ex/refactor-readuint16be-to-readint-with-inttype
…ex/refactor-readuint16be-to-readint-with-inttype
|
@codex review |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| } | ||
|
|
||
| public function isSupported(): bool | ||
| { |
There was a problem hiding this comment.
The isSupported() method only checks byte size but doesn't account for the fact that unsigned 64-bit integers on 64-bit platforms cannot represent the full range (they're limited to PHP_INT_MAX). This could lead to incorrect support detection for UINT64 types.
| { | |
| { | |
| // For unsigned 64-bit types, check that PHP_INT_MAX is large enough | |
| if ( | |
| ($this === self::UINT64 || $this === self::UINT64_LE) | |
| && PHP_INT_SIZE === 8 | |
| && PHP_INT_MAX < 0xFFFFFFFFFFFFFFFF | |
| ) { | |
| return false; | |
| } |
| } | ||
|
|
||
| // Handle negative values for signed types | ||
| if ($type->isSigned() && $value < 0) { |
There was a problem hiding this comment.
The two's complement conversion for negative values will overflow on 64-bit signed integers when $bytesCount * 8 equals or exceeds PHP_INT_SIZE * 8, causing incorrect bit manipulation results.
| if ($type->isSigned() && $value < 0) { | |
| if ($type->isSigned() && $value < 0) { | |
| if (($bytesCount * 8) >= (PHP_INT_SIZE * 8)) { | |
| throw new \RuntimeException( | |
| sprintf( | |
| 'Two\'s complement conversion for negative values will overflow on %d-bit signed integers when %d bytes are used (requested: %d bits, platform: %d bits)', | |
| PHP_INT_SIZE * 8, | |
| $bytesCount, | |
| $bytesCount * 8, | |
| PHP_INT_SIZE * 8 | |
| ) | |
| ); | |
| } |
| if ($bits <= PHP_INT_SIZE * 8) { | ||
| $signBit = 1 << ($bits - 1); | ||
| if (($value & $signBit) !== 0) { | ||
| $value -= 1 << $bits; | ||
| } |
There was a problem hiding this comment.
The sign extension logic has the same overflow issue as the writer - when $bits equals PHP_INT_SIZE * 8, the bit shift operations (1 << ($bits - 1) and 1 << $bits) will overflow, producing incorrect results for 64-bit signed integers on 64-bit platforms.
| if ($bits <= PHP_INT_SIZE * 8) { | |
| $signBit = 1 << ($bits - 1); | |
| if (($value & $signBit) !== 0) { | |
| $value -= 1 << $bits; | |
| } | |
| if ($bits < PHP_INT_SIZE * 8) { | |
| $signBit = 1 << ($bits - 1); | |
| if (($value & $signBit) !== 0) { | |
| $value -= 1 << $bits; | |
| } | |
| } elseif ($bits == PHP_INT_SIZE * 8) { | |
| // Use GMP for sign extension to avoid overflow | |
| if (!function_exists('gmp_init')) { | |
| throw new RuntimeException('GMP extension required for 64-bit signed integer parsing'); | |
| } | |
| $gmpValue = gmp_init($value, 10); | |
| $signBit = gmp_init('1', 10); | |
| $signBit = gmp_mul($signBit, gmp_pow(2, $bits - 1)); | |
| if (gmp_cmp(gmp_and($gmpValue, $signBit), 0) !== 0) { | |
| $gmpValue = gmp_sub($gmpValue, gmp_pow(2, $bits)); | |
| } | |
| // Convert back to PHP int if possible, else string | |
| $value = gmp_strval($gmpValue); | |
| if (gmp_cmp($gmpValue, PHP_INT_MIN) >= 0 && gmp_cmp($gmpValue, PHP_INT_MAX) <= 0) { | |
| $value = (int)$value; | |
| } |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback". |
This pull request introduces a significant refactor and enhancement to the binary reading and writing utilities. The main focus is on improving flexibility for variable-length and padded binary data parsing, modernizing the API, and enhancing documentation for maintainability. Deprecated methods have been replaced with more configurable alternatives, and new helper methods have been added for safer and more expressive binary operations.
BinaryReader API modernization and flexibility
readBytesWithandreadStringWithmethods, allowing flexible reading of variable-length, terminator-based, optional terminator, and padding-based binary fields, replacing the older fixed-length methods. Deprecated legacy methods (readBytesWithLength,readStringWithLength,readUint16BE) now delegate to the new API. [1] [2] [3]readInt(IntType $type)method, improving platform compatibility and error handling.Documentation and developer experience
BinaryReader,BinaryWriter, andBinaryStringto clarify usage, parameters, and error conditions. [1] [2] [3]BinaryString enhancements
contains(BinaryString $needle)for timing-safe substring checks, and improved parameter documentation and naming for encoding/decoding methods.BinaryWriter improvements
Tooling and developer workflow
docsscript tocomposer.jsonfor generating API documentation, improving maintainability and onboarding.