Skip to content

Conversation

@spawnia
Copy link
Member

@spawnia spawnia commented Oct 10, 2025

  • Added automated tests
  • Documented in README
  • Updated the changelog (handled by semantic-release)

Changes

Adds a new SafeCast utility class that provides safe alternatives to PHP's native type casts.

PHP's native type casts like (int) and (float) can produce unexpected results, especially when casting from strings:

  • (int)"hello" returns 0 (misleading, not an error)
  • (int)"123abc" returns 123 (partial conversion, data loss)
  • (float)"1.23.45" returns 1.23 (invalid format accepted)

The SafeCast class validates input before casting and throws exceptions for invalid inputs instead of silently producing incorrect values.

Methods included:

  • SafeCast::toInt() - Safely cast to integer
  • SafeCast::toFloat() - Safely cast to float
  • SafeCast::toString() - Safely cast to string

See README.md for usage examples and SafeCastTest.php for comprehensive test coverage.

Breaking changes

None - this is a new feature addition.

@spawnia spawnia requested a review from Copilot October 10, 2025 13:10
Copy link

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 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 SafeCast class 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.

Copy link
Member Author

@spawnia spawnia left a comment

Choose a reason for hiding this comment

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

Thanks for the review! Addressing both comments:

Float formatting (comment 1): We've added a comment in the code explaining that we're using PHP's default float-to-string conversion for error messages. While sprintf('%.10g', $value) would provide more consistent formatting, the default conversion is sufficient for error reporting purposes and keeps the code simpler.

toBool documentation (comment 2): The toBool() method has been removed entirely from this PR. Boolean casting is too context-dependent and opinionated to include in a "safe" casting utility - different contexts use different conventions (HTML forms use 'on'/'off', APIs might use 'true'/'false', databases might use '1'/'0'). The final implementation only includes toInt(), toFloat(), and toString().

@spawnia spawnia marked this pull request as ready for review November 7, 2025 11:50
@spawnia spawnia marked this pull request as draft November 7, 2025 13:04
@spawnia spawnia requested a review from simbig November 7, 2025 13:04
@simbig simbig marked this pull request as ready for review November 7, 2025 15:38
@spawnia
Copy link
Member Author

spawnia commented Nov 10, 2025

Merge erst nach Vorstellung und generellem Approval im Fachvortrag. Ich implementiere es bis dahin noch in unseren Projekten um sicherzustellen dass das Design passt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants