Throw TypeError when container factory returns Closure#302
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR tightens type validation for container variable factory functions by explicitly rejecting factories that return a Closure, preventing accidental “factory returning a factory” behavior and surfacing a clear TypeError earlier.
Changes:
- Add an explicit
Closurereturn-value check for variable factories inContainer::loadVariable()and throwTypeError. - Add PHPUnit coverage for the new behavior when calling
getEnv()and to assert class-factory behavior remains consistent forgetObject().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Container.php |
Adds explicit rejection of Closure return values from variable factories via a dedicated TypeError. |
tests/ContainerTest.php |
Adds tests ensuring getEnv() rejects factories returning a Closure and getObject() continues to error consistently when a class factory returns a Closure. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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 adds improved type checking for container factory functions to ensure they return the expected value instead of a
Closure. Factory functions in the container configuration remain fully supported and continue to work as before, but their return value must be a usable value (object, scalar, or null) rather than another function:This includes a number of updated tests to verify correct behavior and avoid introducing any potential regressions, so this has 100% code coverage and should be safe to apply. For class-type factories, this case is already caught by the existing type checks. For variable factories, a
Closureis technically an object and would previously pass the type checks silently but would trigger a different return value on the next invocation. This adds an explicit check to catch this early.Builds on top of #301, #284 and others