Conversation
There was a problem hiding this comment.
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
\BadMethodCallExceptionto\TypeErrorfor type mismatches and\Errorfor 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.
This was referenced Dec 14, 2025
This was referenced Dec 24, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This changeset improves exception messages for the
Containerclass to improve type safety, error handling, and clarity of exception messages. It replaces most uses of\BadMethodCallExceptioninstances with more appropriate exception types (\TypeErrorand\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