Skip to content
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
@@ -0,0 +1,25 @@
<?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\TypeInfo\Tests\Fixtures;

/**
* @template T
*
* @phpstan-type AliasWithTemplate = T
*/
final class DummyWithTemplateTypeAlias
{
/**
* @var AliasWithTemplate
*/
public mixed $foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithInvalidTypeAliasImport;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithRecursiveTypeAliases;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplateTypeAlias;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTypeAliases;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTypeAliasImportedFromInvalidClassName;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithUses;
Expand Down Expand Up @@ -146,9 +147,23 @@ public function testDoNotCollectTemplatesWhenToStringTypeResolver()
$this->assertEquals([], $typeContextFactory->createFromClassName(DummyWithTemplates::class)->templates);
}

public function testCollectTypeAliases()
/**
* @dataProvider collectTypeAliasesDataProvider
*
* @param array<string, Type> $expectedTypeAliases
* @param class-string $className
*/
public function testCollectTypeAliases(array $expectedTypeAliases, string $className)
{
$this->assertEquals([
$this->assertEquals($expectedTypeAliases, $this->typeContextFactory->createFromClassName($className)->typeAliases);
}

/**
* @return iterable<array{0: array<string, Type>, 1: class-string}>
*/
public static function collectTypeAliasesDataProvider(): iterable
{
yield [[
'CustomString' => Type::string(),
'CustomInt' => Type::int(),
'CustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]),
Expand All @@ -157,9 +172,9 @@ public function testCollectTypeAliases()
'PsalmCustomInt' => Type::int(),
'PsalmCustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]),
'PsalmAliasedCustomInt' => Type::int(),
], $this->typeContextFactory->createFromClassName(DummyWithTypeAliases::class)->typeAliases);
], DummyWithTypeAliases::class];

$this->assertEquals([
yield [[
'CustomString' => Type::string(),
'CustomInt' => Type::int(),
'CustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]),
Expand All @@ -168,9 +183,9 @@ public function testCollectTypeAliases()
'PsalmCustomInt' => Type::int(),
'PsalmCustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]),
'PsalmAliasedCustomInt' => Type::int(),
], $this->typeContextFactory->createFromReflection(new \ReflectionClass(DummyWithTypeAliases::class))->typeAliases);
], DummyWithTypeAliases::class];

$this->assertEquals([
yield [[
'CustomString' => Type::string(),
'CustomInt' => Type::int(),
'CustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]),
Expand All @@ -179,11 +194,10 @@ public function testCollectTypeAliases()
'PsalmCustomInt' => Type::int(),
'PsalmCustomArray' => Type::arrayShape([0 => Type::int(), 1 => Type::string(), 2 => Type::bool()]),
'PsalmAliasedCustomInt' => Type::int(),
], $this->typeContextFactory->createFromReflection(new \ReflectionProperty(DummyWithTypeAliases::class, 'localAlias'))->typeAliases);
], DummyWithTypeAliases::class];

$this->assertEquals([
'CustomInt' => Type::int(),
], $this->typeContextFactory->createFromReflection(new \ReflectionClass(DummyWithImportedOnlyTypeAliases::class))->typeAliases);
yield [['CustomInt' => Type::int()], DummyWithImportedOnlyTypeAliases::class];
yield [['AliasWithTemplate' => Type::template('T')], DummyWithTemplateTypeAlias::class];
}

public function testDoNotCollectTypeAliasesWhenToStringTypeResolver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithConstants;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplateTypeAlias;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTypeAliases;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
Expand Down Expand Up @@ -202,6 +203,7 @@ public static function resolveDataProvider(): iterable
// type aliases
yield [Type::int(), 'CustomInt', $typeContextFactory->createFromClassName(DummyWithTypeAliases::class)];
yield [Type::string(), 'CustomString', $typeContextFactory->createFromClassName(DummyWithTypeAliases::class)];
yield [Type::template('T'), 'AliasWithTemplate', $typeContextFactory->createFromClassName(DummyWithTemplateTypeAlias::class)];
}

public function testCannotResolveNonStringType()
Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ public function createFromClassName(string $calledClassName, ?string $declaringC
$this->collectUses($declaringClassReflection),
);

return new TypeContext(
$typeContext = new TypeContext(
$typeContext->calledClassName,
$typeContext->declaringClassName,
$typeContext->namespace,
$typeContext->uses,
$this->collectTemplates($declaringClassReflection, $typeContext),
);

return new TypeContext(
$typeContext->calledClassName,
$typeContext->declaringClassName,
$typeContext->namespace,
$typeContext->uses,
$typeContext->templates,
$this->collectTypeAliases($declaringClassReflection, $typeContext),
);
}
Expand Down Expand Up @@ -103,12 +111,20 @@ public function createFromReflection(\Reflector $reflection): ?TypeContext
default => $this->collectTemplates($declaringClassReflection, $typeContext),
};

return new TypeContext(
$typeContext = new TypeContext(
$typeContext->calledClassName,
$typeContext->declaringClassName,
$typeContext->namespace,
$typeContext->uses,
$templates,
);

return new TypeContext(
$typeContext->calledClassName,
$typeContext->declaringClassName,
$typeContext->namespace,
$typeContext->uses,
$typeContext->templates,
$this->collectTypeAliases($declaringClassReflection, $typeContext),
);
}
Expand Down
Loading