Skip to content

Conversation

@clue
Copy link
Owner

@clue clue commented Dec 11, 2025

This changeset improves exception messages for the Container class to improve type safety, error handling, and clarity of exception messages. It replaces most uses of \BadMethodCallException instances with more appropriate exception types (\TypeError and \Error), clarifies docblocks, and strengthens type validation throughout the container configuration and resolution logic.

These changes make the DI container more robust, easier to use correctly, and provide clearer feedback when misused. On top of this, the main motivation for this is really to prepare follow-up features like union types. More on this in a follow-up PR.

Note that this only affects exception handling for invalid arguments etc. and all its APIs are currently internal, so this does not affect BC.

Builds on top of #274, #267, #244, #97, #95, #94, #92, #12 and others

@clue clue added this to the v0.18.0 milestone Dec 11, 2025
@clue clue requested a review from Copilot December 11, 2025 22:39
@clue clue added the new feature New feature or request label Dec 11, 2025
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 improves exception handling in the Container class by replacing generic \BadMethodCallException instances with more semantically appropriate exception types (\TypeError for type violations and \Error for other runtime errors). The changes enhance error messages with detailed context including function names, line numbers, parameter positions, and expected/actual types.

Key changes include:

  • Migrating from \BadMethodCallException to \TypeError for type mismatches and \Error for configuration/loading errors
  • Enhanced error messages that include closure file/line information, parameter details, and clearer type expectations
  • New helper methods (functionName(), typeName(), validateType()) to support better error reporting

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Container.php Core implementation changes: renamed parameter from $loader to $config, updated exception types throughout, added helper methods for error message generation, improved type validation and error reporting
tests/ContainerTest.php Updated test expectations to match new exception types (\Error/\TypeError instead of \BadMethodCallException), added new test cases for edge cases (unknown classes, non-callable classes, factory errors), updated expected error messages to match new detailed format
tests/AppTest.php Updated error message assertions in integration tests to expect \Error instead of \BadMethodCallException and match new error message formats

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

new feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant