Skip to content

[4.x] Throw DependencyResolutionException for intersection-typed arguments - #76

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-intersection-types
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-intersection-types

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Autowiring a class with an intersection-typed constructor argument (__construct(A&B $x)) ended in Error: Call to undefined method ReflectionIntersectionType::isBuiltin(): getMethodArgs() only treated ReflectionUnionType as unsupported and called isBuiltin()/getName() on every other type. Because it is an Error and not a DependencyResolutionException, a class depending on such a class through a nullable argument could not fall back to null either. PHPStan reported the calls, but they were hidden in the baseline.

Every parameter type that is not a ReflectionNamedType (union, intersection and DNF types) is now handled the same way: a nullable argument receives null, any other one throws a DependencyResolutionException. The message now reads "Union and intersection typehints are not supported." The two baseline entries are removed. This also finishes what #46 reported for PHP 8 types.

Testing Instructions

Tests/CompoundTypeTest.php covers required and nullable union and intersection types, and a class that depends on an intersection-typed class through a nullable argument. Before the change the intersection cases end in an Error; after it, all pass. php vendor/bin/phpstan analyse reports no errors without the removed baseline entries.

Documentation Changes Required

docs/overview.md ("When it does not work") now describes how union and intersection types are handled.

Fixes #46

🤖 Generated with Claude Code

getMethodArgs() only recognised union types as unsupported and called
isBuiltin() on every other parameter type. For an intersection type that
is a ReflectionIntersectionType, so building such a class ended in an
Error instead of a DependencyResolutionException, and the nullable
fallback of a dependent class could not catch it.

Any type that is not a named type is now treated like a union type:
null for a nullable argument, otherwise a DependencyResolutionException.
The PHPStan baseline entries hiding the problem are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP 8 incompatibility

1 participant