diff --git a/Tests/CompoundTypeTest.php b/Tests/CompoundTypeTest.php new file mode 100644 index 00000000..cbf5de23 --- /dev/null +++ b/Tests/CompoundTypeTest.php @@ -0,0 +1,114 @@ +expectException(DependencyResolutionException::class); + $this->expectExceptionMessage('Union and intersection typehints are not supported.'); + + (new Container())->buildObject(CompoundTypeUnion::class); + } + + #[TestDox('A required intersection typed argument cannot be autowired')] + public function testRequiredIntersectionTypeThrowsAnException() + { + $this->expectException(DependencyResolutionException::class); + $this->expectExceptionMessage('Union and intersection typehints are not supported.'); + + (new Container())->buildObject(CompoundTypeIntersection::class); + } + + #[TestDox('A nullable union typed argument receives null')] + public function testNullableUnionTypeReceivesNull() + { + $this->assertNull((new Container())->buildObject(CompoundTypeNullableUnion::class)->stub); + } + + #[TestDox('A nullable intersection typed argument receives null')] + public function testNullableIntersectionTypeReceivesNull() + { + $this->assertNull((new Container())->buildObject(CompoundTypeNullableIntersection::class)->stub); + } + + #[TestDox('A class with a required intersection typed argument is passed null for a nullable dependency')] + public function testIntersectionTypeInNullableDependency() + { + $this->assertNull((new Container())->buildObject(CompoundTypeIntersectionConsumer::class)->stub); + } +} + +// @codingStandardsIgnoreStart + +interface CompoundTypeFirst +{ +} + +interface CompoundTypeSecond +{ +} + +class CompoundTypeUnion +{ + public function __construct(CompoundTypeFirst|CompoundTypeSecond $stub) + { + } +} + +class CompoundTypeIntersection +{ + public function __construct(CompoundTypeFirst&CompoundTypeSecond $stub) + { + } +} + +class CompoundTypeNullableUnion +{ + public $stub; + + public function __construct(CompoundTypeFirst|CompoundTypeSecond|null $stub) + { + $this->stub = $stub; + } +} + +class CompoundTypeNullableIntersection +{ + public $stub; + + public function __construct((CompoundTypeFirst&CompoundTypeSecond)|null $stub) + { + $this->stub = $stub; + } +} + +class CompoundTypeIntersectionConsumer +{ + public $stub; + + public function __construct(?CompoundTypeIntersection $stub) + { + $this->stub = $stub; + } +} diff --git a/docs/overview.md b/docs/overview.md index b8d95e49..d0ab91c3 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -452,6 +452,8 @@ Scalar arguments are provided with their default value. The container can only resolve dependencies that have been properly typehinted or given a default value. If the resource can not be built, a `DependencyResolutionException` is thrown. +Union types (`A|B`) and intersection types (`A&B`) cannot be autowired either. A nullable argument with such a type (`A|B|null`, `(A&B)|null`) receives `null`; any other one makes `buildObject` throw a `DependencyResolutionException`. + When you try to build a non-class, `buildObject` and `buildSharedObject` both return `false`. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 61e22df3..4488aafa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6,18 +6,6 @@ parameters: count: 1 path: src/Container.php - - - message: '#^Call to an undefined method ReflectionType\:\:getName\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Container.php - - - - message: '#^Call to an undefined method ReflectionType\:\:isBuiltin\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Container.php - - message: '#^Unsafe usage of new static\(\)\.$#' identifier: new.static diff --git a/src/Container.php b/src/Container.php index e688d33c..0efdfe21 100644 --- a/src/Container.php +++ b/src/Container.php @@ -431,8 +431,8 @@ private function getMethodArgs(\ReflectionMethod $method): array if ($param->hasType()) { $dependency = $param->getType(); - // Don't support PHP 8 union types - if ($dependency instanceof \ReflectionUnionType) { + // Don't support union and intersection types + if (!$dependency instanceof \ReflectionNamedType) { // If this is a nullable parameter, then don't error out if ($param->allowsNull()) { $methodArgs[] = null; @@ -442,7 +442,7 @@ private function getMethodArgs(\ReflectionMethod $method): array throw new DependencyResolutionException( sprintf( - 'Could not resolve the parameter "$%s" of "%s::%s()": Union typehints are not supported.', + 'Could not resolve the parameter "$%s" of "%s::%s()": Union and intersection typehints are not supported.', $param->name, $method->class, $method->name