Level-2 / Dice

Dice - a lightweight Dependency Injection Container for PHP

Home Page:https://r.je/dice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for union/intersection types?

jpirnat opened this issue · comments

Dice fails to instantiate classes which use union or intersection types. A quick demo:

<?php
declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

$dice = new \Dice\Dice();

$rule = [
	'constructParams' => [
		'one',
		'two',
		'three',
		'four',
		'five',
		'six',
		'seven',
		'eight',
		'nine',
		'ten',
	]
];
$dice = $dice->addRule(Foo::class, $rule);

class Foo
{
	public function __construct(
		string $one,
		string|int $two,
		string|int $three,
		string|int $four,
		string $five,
		string $six,
		string $seven,
		string $eight,
		string $nine,
		string $ten
	) {
		print_r([$one, $two, $three, $four, $five, $six, $seven, $eight, $nine, $ten]);
	}
}

$foo = $dice->create(Foo::class);

Output:

Fatal error: Uncaught Error: Call to undefined method ReflectionUnionType::getName() in dicetest/vendor/level-2/dice/Dice.php:257
Stack trace:
#0 dicetest/vendor/level-2/dice/Dice.php(119): Dice\Dice->Dice\{closure}(Array, Array)
#1 dicetest/vendor/level-2/dice/Dice.php(96): Dice\Dice->Dice\{closure}(Array, Array)
#2 dicetest/index.php(42): Dice\Dice->create('Foo')
#3 {main}
  thrown in dicetest/vendor/level-2/dice/Dice.php on line 257

I wouldn't have thought this would be worth asking for, but some popular libraries are apparently already using union types out in the wild (e.g. Symfony HttpFoundation).

See pull request #204 for support for scalar union types

I have merged a patch for this into a fork I maintain here:
https://packagist.org/packages/moddengine/dice