-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add class SafeCast #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 introduces a new SafeCast utility class that provides safe type casting alternatives to PHP's native type casting operators. The utility validates input before casting and throws exceptions for invalid inputs instead of silently producing incorrect values.
- Adds
SafeCastclass with methods for safe casting to int, float, string, and bool types - Includes comprehensive test coverage for all casting methods and edge cases
- Updates documentation with usage examples and explanations
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/SafeCast.php | Main implementation of the SafeCast utility class with validation logic |
| tests/SafeCastTest.php | Comprehensive test suite covering all methods and edge cases |
| README.md | Documentation update with usage examples for the new SafeCast utility |
| src/FluidXPlate/FluidXScanner.php | Minor refactoring to use in_array() for better readability |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| return (int) $value; | ||
| } | ||
|
|
||
| throw new \InvalidArgumentException('Float value "' . $value . '" cannot be safely cast to int (not a whole number or not finite)'); |
Copilot
AI
Oct 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message concatenates a float value directly into the string, which may produce inconsistent formatting (e.g., scientific notation). Consider using a more controlled string representation.
| throw new \InvalidArgumentException('Float value "' . $value . '" cannot be safely cast to int (not a whole number or not finite)'); | |
| throw new \InvalidArgumentException('Float value "' . sprintf('%.10g', $value) . '" cannot be safely cast to int (not a whole number or not finite)'); |
| * this method is strict and only accepts: | ||
| * - Booleans (returned as-is) | ||
| * - Integers 0 and 1 | ||
| * - Strings "0", "1", "true", "false", "yes", "no" (case-insensitive) |
Copilot
AI
Oct 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation is incomplete - it mentions 'yes', 'no' but doesn't include 'on', 'off' which are actually supported in the implementation (lines 180, 184).
| * - Strings "0", "1", "true", "false", "yes", "no" (case-insensitive) | |
| * - Strings "0", "1", "true", "false", "yes", "no", "on", "off" (case-insensitive) |
Changes
Breaking changes