8000 [DependencyInjection] Support for intersection types by derrabus · Pull Request #42097 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Support for intersection types #42097

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
Jul 15, 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
[DependencyInjection] Support for intersection types
Signed-off-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
derrabus committed Jul 14, 2021
commit d28cf2425856a8684ff1ed361fbcbaec1d48b3a7
2 changes: 2 additions & 0 deletions .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
case false !== strpos($file, '/src/Symfony/Component/Debug/Tests/Fixtures/'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Compiler/OptionalServiceClass.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/intersectiontype_classes.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/MultipleArgumentsOptionalScalarNotReallyOptional.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CheckTypeDeclarationsPass/IntersectionConstructor.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ParentNotExists.php'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Preload/'):
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/BadClasses/MissingParent.php'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar

throw new InvalidParameterTypeException($this->currentId, $e->getCode(), $parameter);
}
if ($reflectionType instanceof \ReflectionIntersectionType) {
foreach ($reflectionType->getTypes() as $t) {
$this->checkType($checkedDefinition, $value, $parameter, $envPlaceholderUniquePrefix, $t);
}

return;
}
if (!$reflectionType instanceof \ReflectionNamedType) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static function preloadType(?\ReflectionType $t, array &$preloaded): voi
return;
}

foreach ($t instanceof \ReflectionUnionType ? $t->getTypes() : [$t] as $t) {
foreach (($t instanceof \ReflectionUnionType || $t instanceof \ReflectionIntersectionType) ? $t->getTypes() : [$t] as $t) {
if (!$t->isBuiltin()) {
self::doPreload($t instanceof \ReflectionNamedType ? $t->getName() : $t, $preloaded);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,31 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
}

$types = [];
$glue = '|';
if ($type instanceof \ReflectionUnionType) {
$reflectionTypes = $type->getTypes();
} elseif ($type instanceof \ReflectionIntersectionType) {
$reflectionTypes = $type->getTypes();
$glue = '&';
} elseif ($type instanceof \ReflectionNamedType) {
$reflectionTypes = [$type];
} else {
return null;
}

foreach ($type instanceof \ReflectionUnionType ? $type->getTypes() : [$type] as $type) {
$name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;

foreach ($reflectionTypes as $type) {
if ($type->isBuiltin()) {
if (!$noBuiltin) {
$types[] = $name;
$types[] = $type->getName();
}
continue;
}

$lcName = strtolower($name);
$lcName = strtolower($type->getName());
$prefix = $noBuiltin ? '' : '\\';

if ('self' !== $lcName && 'parent' !== $lcName) {
$types[] = '' !== $prefix ? $prefix.$name : $name;
$types[] = $prefix.$type->getName();
continue;
}
if (!$r instanceof \ReflectionMethod) {
Expand All @@ -61,6 +70,6 @@ public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionPa
}
}

return $types ? implode('|', $types) : null;
return $types ? implode($glue, $types) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@ public function testTypeNotGuessableUnionType()
$pass->process($container);
}

/**
* @requires PHP 8.1
*/
public function testTypeNotGuessableIntersectionType()
{
$this->expectException(AutowiringFailedException::class);
$this->expectExceptionMessage('Cannot autowire service "a": argument "$collision" of method "Symfony\Component\DependencyInjection\Tests\Compiler\IntersectionClasses::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface&Symfony\Component\DependencyInjection\Tests\Compiler\AnotherInterface" but this class was not found.');
$container = new ContainerBuilder();

$container->register(CollisionInterface::class);
$container->register(AnotherInterface::class);

$aDefinition = $container->register('a', IntersectionClasses::class);
$aDefinition->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);
}

public function testTypeNotGuessableWithTypeSet()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Deprecated;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\FooObject;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\IntersectionConstructor;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\UnionConstructor;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\WaldoFoo;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Wobble;
use Symfony\Component\ExpressionLanguage\Expression;

Expand Down Expand Up @@ -953,4 +955,37 @@ public function testReferencePassesMixed()

$this->addToAssertionCount(1);
}

/**
* @requires PHP 8.1
*/
public function testIntersectionTypePassesWithReference()
{
$container = new ContainerBuilder();

$container->register('foo', WaldoFoo::class);
$container->register('intersection', IntersectionConstructor::class)
->setArguments([new Reference('foo')]);

(new CheckTypeDeclarationsPass(true))->process($container);

$this->addToAssertionCount(1);
}

/**
* @requires PHP 8.1
*/
public function testIntersectionTypeFailsWithReference()
{
$container = new ContainerBuilder();

$container->register('waldo', Waldo::class);
$container->register('intersection', IntersectionConstructor::class)
->setArguments([new Reference('waldo')]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid definition for service "intersection": argument 1 of "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CheckTypeDeclarationsPass\\IntersectionConstructor::__construct()" accepts "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo&Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\WaldoInterface", "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Waldo" passed.');

(new CheckTypeDeclarationsPass(true))->process($container);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\Dummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\DummyWithInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\E;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\F;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\G;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\IntersectionDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\UnionDummy;

class PreloaderTest extends TestCase
Expand Down Expand Up @@ -72,4 +75,20 @@ public function testPreloadUnion()
self::assertTrue(class_exists(D::class, false));
self::assertTrue(class_exists(E::class, false));
}

/**
* @requires PHP 8.1
*/
public function testPreloadIntersection()
{
$r = new \ReflectionMethod(Preloader::class, 'doPreload');

$preloaded = [];

$r->invokeArgs(null, ['Symfony\Component\DependencyInjection\Tests\Fixtures\Preload\IntersectionDummy', &$preloaded]);

self::assertTrue(class_exists(IntersectionDummy::class, false));
self::assertTrue(class_exists(F::class, false));
self::assertTrue(class_exists(G::class, false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass;

class IntersectionConstructor
{
public function __construct(Foo&WaldoInterface $arg)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass;

class WaldoFoo extends Foo implements WaldoInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload;

final class F
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload;

final class G
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Preload;

final class IntersectionDummy
{
public F&G $fg;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
if (\PHP_VERSION_ID >= 80000) {
require __DIR__.'/uniontype_classes.php';
}
if (\PHP_VERSION_ID >= 80100) {
require __DIR__.'/intersectiontype_classes.php';
}

class Foo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

interface AnotherInterface
{
}

class IntersectionClasses
{
public function __construct(CollisionInterface&AnotherInterface $collision)
{
}
}
0