Skip to content

Conversation

@clue
Copy link
Owner

@clue clue commented Dec 14, 2025

This changeset updates the Container to not inject null values for any unset nullable parameters. With these changes applied, nullable arguments without explicit container configuration or a default value will cause an error if they are not set instead of being silently set to null. You can still use an explicit default value to restore the old behavior:

// old (nullable parameter no longer defaults to null if unset)
$container = new FrameworkX\Container([
    UserController::class => fn (?string $name) => new UserController($name ?? 'ACME')
]);

// replacement already supported (nullable parameter with explicit default if unset)
$container = new FrameworkX\Container([
    UserController::class => fn (?string $name = null) => new UserController($name ?? 'ACME')
]);

// replacement already supported (parameter with explicit default if unset)
$container = new FrameworkX\Container([
    UserController::class => fn (string $name = 'ACME') => new UserController($name)
]);

Note that this is a BC break with a very real chance to affect a number of projects relying on the existing behavior. The main motivation for this changes is to reduce complexity to introduce support for union types in a follow-up PR. Also, given the above upgrade path, upgrading should be fairly straightforward.

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. On top of this, I've updated/simplified the documentation slightly to emphasize assigning default values instead.

Builds on top of #284, #184, #181, #92, and others

@clue clue added this to the v0.18.0 milestone Dec 14, 2025
@clue clue requested a review from Copilot December 14, 2025 22:41
@clue clue added new feature New feature or request BC break labels Dec 14, 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 updates the Container to enforce explicit configuration for nullable parameters without default values, instead of automatically injecting null. This is a breaking change that requires developers to use explicit default values if they want nullable behavior.

  • Removes automatic null injection for unset nullable parameters in Container dependency resolution
  • Updates error handling to throw exceptions for nullable parameters without explicit defaults or container configuration
  • Adds comprehensive test coverage for the new behavior with nullable, union, and mixed types

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/Container.php Removes logic that auto-injects null for nullable parameters; now requires explicit defaults or container configuration
tests/ContainerTest.php Removes tests for old null-injection behavior; adds tests verifying errors for unset nullable parameters and proper handling of explicit defaults
tests/Io/RouteHandlerTest.php Removes test that verified nullable constructor autowiring with null injection
docs/best-practices/controllers.md Removes documentation example showing nullable parameter behavior; streamlines docs to emphasize explicit default values

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

@clue clue force-pushed the container-no-nulls branch from 9c6235c to 7fa20c4 Compare December 14, 2025 22:50
@clue clue merged commit 5bb03c0 into clue:main Dec 15, 2025
68 checks passed
@clue clue deleted the container-no-nulls branch December 15, 2025 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BC break new feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant