[DependencyInjection] Support PHP 8 builtin types in CheckTypeDeclarationsPass
This commit is contained in:
parent
92ccb0a9df
commit
f16230be5d
@ -280,15 +280,26 @@ final class CheckTypeDeclarationsPass extends AbstractRecursivePass
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('mixed' === $type) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_a($class, $type, true)) {
|
if (is_a($class, $type, true)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$checkFunction = sprintf('is_%s', $type);
|
if ('false' === $type) {
|
||||||
|
if (false === $value) {
|
||||||
if (!$reflectionType->isBuiltin() || !$checkFunction($value)) {
|
return;
|
||||||
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : \gettype($value), $parameter);
|
}
|
||||||
|
} elseif ($reflectionType->isBuiltin()) {
|
||||||
|
$checkFunction = sprintf('is_%s', $type);
|
||||||
|
if ($checkFunction($value)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : \gettype($value), $parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getExpressionLanguage(): ExpressionLanguage
|
private function getExpressionLanguage(): ExpressionLanguage
|
||||||
|
@ -836,6 +836,22 @@ class CheckTypeDeclarationsPassTest extends TestCase
|
|||||||
$this->addToAssertionCount(1);
|
$this->addToAssertionCount(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testUnionTypePassesWithFalse()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('union', UnionConstructor::class)
|
||||||
|
->setFactory([UnionConstructor::class, 'create'])
|
||||||
|
->setArguments([false]);
|
||||||
|
|
||||||
|
(new CheckTypeDeclarationsPass(true))->process($container);
|
||||||
|
|
||||||
|
$this->addToAssertionCount(1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @requires PHP 8
|
* @requires PHP 8
|
||||||
*/
|
*/
|
||||||
@ -851,8 +867,6 @@ class CheckTypeDeclarationsPassTest extends TestCase
|
|||||||
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\UnionConstructor::__construct()" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo|int", "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo" passed.');
|
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\UnionConstructor::__construct()" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo|int", "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo" passed.');
|
||||||
|
|
||||||
(new CheckTypeDeclarationsPass(true))->process($container);
|
(new CheckTypeDeclarationsPass(true))->process($container);
|
||||||
|
|
||||||
$this->addToAssertionCount(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -869,6 +883,57 @@ class CheckTypeDeclarationsPassTest extends TestCase
|
|||||||
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\UnionConstructor::__construct()" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo|int", "array" passed.');
|
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\UnionConstructor::__construct()" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo|int", "array" passed.');
|
||||||
|
|
||||||
(new CheckTypeDeclarationsPass(true))->process($container);
|
(new CheckTypeDeclarationsPass(true))->process($container);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testUnionTypeWithFalseFailsWithReference()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('waldo', Waldo::class);
|
||||||
|
$container->register('union', UnionConstructor::class)
|
||||||
|
->setFactory([UnionConstructor::class, 'create'])
|
||||||
|
->setArguments([new Reference('waldo')]);
|
||||||
|
|
||||||
|
$this->expectException(\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\UnionConstructor::create()" accepts "array|false", "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo" passed.');
|
||||||
|
|
||||||
|
(new CheckTypeDeclarationsPass(true))->process($container);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testUnionTypeWithFalseFailsWithTrue()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('waldo', Waldo::class);
|
||||||
|
$container->register('union', UnionConstructor::class)
|
||||||
|
->setFactory([UnionConstructor::class, 'create'])
|
||||||
|
->setArguments([true]);
|
||||||
|
|
||||||
|
$this->expectException(\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Invalid definition for service "union": argument 1 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\UnionConstructor::create()" accepts "array|false", "boolean" passed.');
|
||||||
|
|
||||||
|
(new CheckTypeDeclarationsPass(true))->process($container);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires PHP 8
|
||||||
|
*/
|
||||||
|
public function testReferencePassesMixed()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('waldo', Waldo::class);
|
||||||
|
$container->register('union', UnionConstructor::class)
|
||||||
|
->setFactory([UnionConstructor::class, 'make'])
|
||||||
|
->setArguments([new Reference('waldo')]);
|
||||||
|
|
||||||
|
(new CheckTypeDeclarationsPass(true))->process($container);
|
||||||
|
|
||||||
$this->addToAssertionCount(1);
|
$this->addToAssertionCount(1);
|
||||||
}
|
}
|
||||||
|
@ -7,4 +7,14 @@ class UnionConstructor
|
|||||||
public function __construct(Foo|int $arg)
|
public function __construct(Foo|int $arg)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function create(array|false $arg): static
|
||||||
|
{
|
||||||
|
return new static(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function make(mixed $arg): static
|
||||||
|
{
|
||||||
|
return new static(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user