Skip to content

[DependencyInjection] Support PHP 8 builtin types in CheckTypeDeclarationsPass #39747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,26 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
return;
}

if (is_a($class, $type, true)) {
if ('mixed' === $type) {
return;
}

$checkFunction = sprintf('is_%s', $type);
if (is_a($class, $type, true)) {
return;
}

if (!$reflectionType->isBuiltin() || !$checkFunction($value)) {
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : \gettype($value), $parameter);
if ('false' === $type) {
if (false === $value) {
return;
}
} 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,22 @@ public function testUnionTypePassesWithBuiltin()
$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
*/
Expand All @@ -851,8 +867,6 @@ public function testUnionTypeFailsWithReference()
$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);

$this->addToAssertionCount(1);
}

/**
Expand All @@ -869,6 +883,57 @@ public function testUnionTypeFailsWithBuiltin()
$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);
}

/**
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ class UnionConstructor
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);
}
}