Skip to content

Throw TypeError when container factory returns Closure#302

Merged
clue merged 1 commit intoclue:mainfrom
clue-labs:container-factory-return
Apr 1, 2026
Merged

Throw TypeError when container factory returns Closure#302
clue merged 1 commit intoclue:mainfrom
clue-labs:container-factory-return

Conversation

@clue
Copy link
Copy Markdown
Owner

@clue clue commented Apr 1, 2026

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:

$container = new Container([
    'X_VALID' => function () { // supported: factory returns a string value
        return 'bar';
    },
    'X_INVALID' => function () { // error: factory returns a Closure instead of the expected value
        return function () {
            return 'bar';
        };
    },
    'X_FOO' => function ($X_VALID, $X_INVALID) {
    
    };
]);

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 Closure is 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

@clue clue added this to the v0.18.0 milestone Apr 1, 2026
@clue clue added the new feature New feature or request label Apr 1, 2026
@clue clue requested a review from Copilot April 1, 2026 11:39
Copy link
Copy Markdown

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 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 Closure return-value check for variable factories in Container::loadVariable() and throw TypeError.
  • Add PHPUnit coverage for the new behavior when calling getEnv() and to assert class-factory behavior remains consistent for getObject().

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.

@clue clue merged commit 575c46b into clue:main Apr 1, 2026
79 checks passed
@clue clue deleted the container-factory-return branch April 1, 2026 11:48
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.

2 participants