Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions Tests/CompoundTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/**
* @copyright Copyright (C) 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\DI\Tests;

use Joomla\DI\Container;
use Joomla\DI\ContainerResource;
use Joomla\DI\Exception\DependencyResolutionException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

/**
* Tests for autowiring constructor arguments with union and intersection types.
*/
#[CoversClass(Container::class)]
#[UsesClass(ContainerResource::class)]
class CompoundTypeTest extends TestCase
{
#[TestDox('A required union typed argument cannot be autowired')]
public function testRequiredUnionTypeThrowsAnException()
{
$this->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;
}
}
2 changes: 2 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ Scalar arguments are provided with their default value.
<!-- [x] A DependencyResolutionException is thrown, if an object can not be built due to dependency on unknown interfaces -->
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`.

<!-- [x] Attempting to build a non-class returns false -->
When you try to build a non-class, `buildObject` and `buildSharedObject` both return `false`.

Expand Down
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Loading