Add PHP 8.4 RoundingMode enum support with polyfill compatibility#35
Add PHP 8.4 RoundingMode enum support with polyfill compatibility#35
Conversation
Deleted the following configuration files: - `.scrutinizer.yml` - `.styleci.yml` - `.travis.yml` - `phpcs.xml.dist`
Implement comprehensive RoundingMode enum support for both PHP 8.4+ and PHP 8.1-8.3: - Add RoundingMode.php: Polyfill enum for PHP 8.1-8.3 compatibility - Update BCMath::round(): Support both RoundingMode enum and PHP_ROUND_* constants - Throw appropriate exceptions for unsupported modes (TowardsZero, AwayFromZero, NegativeInfinity) in PHP < 8.4 - Maintain full backward compatibility with existing PHP_ROUND_* constants - Add comprehensive test coverage with DataProvider pattern - Update CI configuration to enable additional bcround tests 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Code Review: PHP 8.4 RoundingMode Enum SupportOverall Assessment ⭐⭐⭐⭐⭐This is an excellent implementation that demonstrates careful consideration of backward compatibility, proper error handling, and comprehensive testing. The PR successfully adds PHP 8.4 RoundingMode enum support while maintaining full backward compatibility. Code Quality & Best Practices ✅Strengths:
Minor Suggestions:
Security Assessment ✅No security concerns identified. The implementation:
Potential Issues & Edge Cases 🔍Critical Issue Found:Incorrect rounding mode mapping for PHP 8.4+ modes: // Lines 1018-1026: These mappings may not be accurate
\RoundingMode::TowardsZero => PHP_ROUND_HALF_DOWN, // Should be different logic
\RoundingMode::AwayFromZero => PHP_ROUND_HALF_UP, // Should be different logic
\RoundingMode::NegativeInfinity => PHP_ROUND_HALF_DOWN, // Should be different logicIssue: The new PHP 8.4 rounding modes ( Recommendation: These modes should either:
Minor Issues:
Test Coverage Assessment ✅Excellent test coverage with 225+ new test cases: Strengths:
Suggestions:
Performance Considerations ⚡Current Impact: Minimal
Optimization Opportunities:
Compatibility & Migration 🔄Perfect backward compatibility - no breaking changes:
Configuration Changes Review 📁Positive Changes:
Cleanup Benefits:
Recommendations 📋High Priority:
Medium Priority:
Low Priority:
Final Verdict ✨This is a well-architected feature addition that demonstrates excellent engineering practices. The only significant concern is the incorrect mapping of new rounding modes, which should be addressed before merging. Once that's resolved, this will be a valuable addition to the library. Recommended action: Request changes for rounding mode implementation, then approve. Great work overall! 🚀 |
Remove version-dependent support for TowardsZero, AwayFromZero, and NegativeInfinity RoundingMode enums. These modes now consistently throw ValueError with clear error messages across all PHP versions, preparing for future proper implementation. Changes: - Update convertRoundingMode() to throw ValueError for unsupported modes - Remove PHP version checks for these specific modes - Update all related test cases to expect ValueError exceptions - Improve test coverage for supported modes (HalfAwayFromZero, HalfTowardsZero, HalfEven, HalfOdd) - Add comprehensive enum compatibility tests 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
This PR adds comprehensive support for PHP 8.4's new RoundingMode enum while maintaining full backward compatibility with existing PHP_ROUND_* constants and providing polyfill support for PHP 8.1-8.3.
- Introduces a polyfill
RoundingModeenum for PHP 8.1-8.3 that mirrors PHP 8.4's native enum interface - Updates the BCMath
round()method to accept both enum values and traditional constants with proper conversion logic - Adds extensive test coverage for enum functionality, backward compatibility, and error handling across PHP versions
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
lib/RoundingMode.php |
New polyfill enum providing PHP 8.4 compatibility for older versions |
src/BCMath.php |
Enhanced round() method with enum support and conversion logic |
tests/BCMathTest.php |
Comprehensive test suite covering enum functionality and edge cases |
composer.json |
Added RoundingMode.php to autoload files |
.github/workflows/php-src-bcmath-tests.yml |
Enabled additional bcround tests by removing some skips |
| Configuration files | Removed legacy configuration files (.travis.yml, .styleci.yml, .scrutinizer.yml, phpcs.xml.dist) |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
tests/BCMathTest.php
Outdated
| public function testInvalidRoundingMode(array|float|int|string|null $invalidMode): void | ||
| { | ||
| try { | ||
| BCMath::round('1.55', 1, $invalidMode); // @phpstan-ignore-line argment.type |
There was a problem hiding this comment.
There's a typo in the PHPStan ignore comment. 'argment.type' should be 'argument.type'.
| $unsupportedModes = [\RoundingMode::TowardsZero, \RoundingMode::AwayFromZero, \RoundingMode::NegativeInfinity]; | ||
| if (in_array($mode, $unsupportedModes, true)) { | ||
| $this->expectException(\ValueError::class); | ||
| $this->expectExceptionMessage("RoundingMode::{$mode->name} is not supported"); | ||
| } |
There was a problem hiding this comment.
The exception message check uses $mode->name but the actual implementation in convertRoundingMode() throws messages like 'RoundingMode::TowardsZero is not supported' (without the interpolated name). This will cause test failures when the exception message doesn't match exactly.
| // TODO: Support additional modes if needed | ||
| \RoundingMode::NegativeInfinity => throw new \ValueError('RoundingMode::NegativeInfinity is not supported'), | ||
| \RoundingMode::TowardsZero => throw new \ValueError('RoundingMode::TowardsZero is not supported'), | ||
| \RoundingMode::AwayFromZero => throw new \ValueError('RoundingMode::AwayFromZero is not supported'), // @phpstan-ignore-line |
There was a problem hiding this comment.
The PHPStan ignore comment should include a reason for why it's needed. Consider adding a brief explanation like // @phpstan-ignore-line match.unhandled or similar to clarify why this line needs to be ignored.
| \RoundingMode::NegativeInfinity => throw new \ValueError('RoundingMode::NegativeInfinity is not supported'), | ||
| \RoundingMode::TowardsZero => throw new \ValueError('RoundingMode::TowardsZero is not supported'), | ||
| \RoundingMode::AwayFromZero => throw new \ValueError('RoundingMode::AwayFromZero is not supported'), // @phpstan-ignore-line |
There was a problem hiding this comment.
The error messages are inconsistent with the test expectations. The tests expect messages ending with 'is not supported' but these say 'is not supported'. Consider adding a consistent suffix like 'in PHP < 8.4' to match the PR description and provide clearer context.
Code Review: Add PHP 8.4 RoundingMode enum support with polyfill compatibilityOverall AssessmentThis is a well-structured PR that adds PHP 8.4's RoundingMode enum support while maintaining backward compatibility. The implementation is thoughtful and comprehensive. Here are my findings: Code Quality & Best Practices ✅Strengths:
Minor improvements:
Implementation Quality ✅lib/RoundingMode.php:
src/BCMath.php:
Potential Issues & Concerns1. Thread Safety (Minor)
|
Code Review: PHP 8.4 RoundingMode enum support with polyfill compatibilityOverall Assessment ✅This PR provides a well-implemented solution for PHP 8.4 RoundingMode enum support with excellent backward compatibility. The implementation is solid, but there are several areas for improvement. Code Quality & Best PracticesStrengths:
Areas for Improvement: 1. Security & Validation
|
Code Review FeedbackI've thoroughly reviewed this PR and overall it's a well-implemented addition that brings valuable PHP 8.4 compatibility to the library. Here's my detailed feedback: ✅ Strengths1. Excellent Backward Compatibility Design
2. Comprehensive Test Coverage
3. Clean Code Architecture
🔧 Code Quality Issues1. Minor Performance Concern (src/BCMath.php:1010) 2. Inconsistent Error Messages (src/BCMath.php:1017-1019) 3. Polyfill Enum Values (lib/RoundingMode.php:18-24) 🐛 Potential Issues1. PHPStan Suppression (lib/RoundingMode.php:14) 2. Match Expression Ordering (src/BCMath.php:1011-1021) 🔒 Security Considerations
⚡ Performance Considerations
📝 Documentation & StyleExcellent documentation updates in README.md:
🎯 Recommendations
📊 Test Coverage AssessmentThe test coverage is excellent and comprehensive. 🏆 Overall AssessmentThis is a high-quality implementation that successfully adds PHP 8.4 RoundingMode enum support while maintaining full backward compatibility. Recommendation: ✅ Approve with minor suggestions Great work on maintaining backward compatibility while adding modern PHP features! 🎉 |
f4f6daf to
7e6ccb2
Compare
Code Review for PR #35: Add PHP 8.4 RoundingMode enum support🎯 Overall Assessment: Excellent Implementation ⭐⭐⭐⭐⭐This PR demonstrates exceptional engineering practices and adds valuable PHP 8.4 compatibility while maintaining full backward compatibility. The implementation is well-architected, thoroughly tested, and follows best practices. ✅ Strengths1. Architecture & Design Excellence
2. Code Quality
3. Testing Excellence
4. Documentation Quality
🔍 Technical ReviewRoundingMode.php (
|
Summary
This PR adds comprehensive support for PHP 8.4's new
RoundingModeenum while maintaining full backward compatibility with existingPHP_ROUND_*constants and providing polyfill support for PHP 8.1-8.3.Key Features
PHP_ROUND_*constants continue to workValueErrorfor unsupported modes in PHP < 8.4Changes Made
Core Implementation
lib/RoundingMode.php- New polyfill enum for PHP 8.1-8.3 compatibilitysrc/BCMath.php- Updatedround()method to support both enum and constantsconvertRoundingMode()method for enum/constant conversionTesting & CI
tests/BCMathTest.php- Added comprehensive test coverage:testRoundingModeEnumSupport()- Tests enum functionality for PHP 8.4+testRoundingModeBackwardCompatibility()- Ensures PHP_ROUND_* constants worktestInvalidRoundingMode()- Tests error handling for invalid modestestRoundingModeComprehensive()- Extensive edge case testing.github/workflows/php-src-bcmath-tests.yml- Enabled additional bcround testsConfiguration
composer.json- Added RoundingMode.php to autoload filesUsage Examples
RoundingMode Support Matrix
HalfAwayFromZeroPHP_ROUND_HALF_UPHalfTowardsZeroPHP_ROUND_HALF_DOWNHalfEvenPHP_ROUND_HALF_EVENHalfOddPHP_ROUND_HALF_ODDTowardsZeroAwayFromZeroNegativeInfinityImplementation Details
The polyfill enum is conditionally defined in the global namespace:
Backward Compatibility
This change is 100% backward compatible:
PHP_ROUND_*constants continues to work unchangedTest Results
All existing tests pass, plus 225+ new test cases covering:
Test Plan
🤖 Generated with Claude Code