Skip to content

Enhance modes#3

Merged
kduma merged 18 commits intomainfrom
codex/refactor-readuint16be-to-readint-with-inttype
Sep 18, 2025
Merged

Enhance modes#3
kduma merged 18 commits intomainfrom
codex/refactor-readuint16be-to-readint-with-inttype

Conversation

@kduma
Copy link
Contributor

@kduma kduma commented Sep 18, 2025

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

  • Introduced the readBytesWith and readStringWith methods, 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]
  • Added support for reading integers with arbitrary width, signedness, and endianness via the new readInt(IntType $type) method, improving platform compatibility and error handling.

Documentation and developer experience

  • Added detailed PHPDoc comments to all public methods in BinaryReader, BinaryWriter, and BinaryString to clarify usage, parameters, and error conditions. [1] [2] [3]
  • Improved error messages for out-of-bounds seeking and invalid data conditions, aiding debugging and reliability.

BinaryString enhancements

  • Added new helper methods: contains(BinaryString $needle) for timing-safe substring checks, and improved parameter documentation and naming for encoding/decoding methods.

BinaryWriter improvements

  • Added PHPDoc comments to all public methods, clarifying buffer management and error conditions.

Tooling and developer workflow

  • Added a new docs script to composer.json for generating API documentation, improving maintainability and onboarding.

@github-actions
Copy link

github-actions bot commented Sep 18, 2025

🔍 Test Results - PHP 8.4

Code Style Check Failed

Code style violations found. Please run composer fix to automatically fix them.

Tests Passed

Code Coverage: 100%


This comment was automatically generated by the test workflow.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 readInt method 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.

@kduma kduma requested a review from Copilot September 18, 2025 15:02
@kduma
Copy link
Contributor Author

kduma commented Sep 18, 2025

@codex review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

@kduma kduma marked this pull request as ready for review September 18, 2025 15:57
@kduma
Copy link
Contributor Author

kduma commented Sep 18, 2025

@codex review

@kduma kduma requested a review from Copilot September 18, 2025 15:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

@kduma kduma requested a review from Copilot September 18, 2025 16:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kduma
Copy link
Contributor Author

kduma commented Sep 18, 2025

@codex review

@kduma kduma requested a review from Copilot September 18, 2025 19:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
{
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
{
{
// 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;
}

Copilot uses AI. Check for mistakes.
}

// Handle negative values for signed types
if ($type->isSigned() && $value < 0) {
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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
)
);
}

Copilot uses AI. Check for mistakes.
Comment on lines +172 to +176
if ($bits <= PHP_INT_SIZE * 8) {
$signBit = 1 << ($bits - 1);
if (($value & $signBit) !== 0) {
$value -= 1 << $bits;
}
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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;
}

Copilot uses AI. Check for mistakes.
@kduma kduma changed the title Support little-endian and 64-bit integer reads Enhance modes Sep 18, 2025
@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Can't wait for the next one!

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".

@kduma kduma merged commit bd839a1 into main Sep 18, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant