Skip to content

[TypeInfo] Add type alias support #59804

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
Feb 19, 2025
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
1 change: 1 addition & 0 deletions src/Symfony/Component/TypeInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add `TypeFactoryTrait::fromValue()` method
* Deprecate constructing a `CollectionType` instance as a list that is not an array
* Deprecate the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead
* Add type alias support in `TypeContext` and `StringTypeResolver`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

abstract class AbstractDummy
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

final class Dummy extends AbstractDummy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

enum DummyBackedEnum: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

final class DummyCollection implements \IteratorAggregate
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyEnum.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

enum DummyEnum
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

final class DummyExtendingStdClass extends \stdClass
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithPhpDoc.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
<?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;

/**
* @phpstan-type CustomInt = int
* @psalm-type PsalmCustomInt = int
*/
final class DummyWithPhpDoc
{
/**
* @var array<Dummy>
*/
public mixed $arrayOfDummies = [];

/**
* @var CustomInt
*/
public mixed $aliasedInt;

/**
* @param bool $promoted
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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;

/**
* @phpstan-type CustomString = string
* @phpstan-import-type CustomInt from DummyWithPhpDoc
* @phpstan-import-type CustomInt from DummyWithPhpDoc as AliasedCustomInt
*
* @psalm-type PsalmCustomString = string
* @psalm-import-type PsalmCustomInt from DummyWithPhpDoc
* @psalm-import-type PsalmCustomInt from DummyWithPhpDoc as PsalmAliasedCustomInt
*/
final class DummyWithTypeAliases
{
/**
* @var CustomString
*/
public mixed $localAlias;

/**
* @var CustomInt
*/
public mixed $externalAlias;

/**
* @var AliasedCustomInt
*/
public mixed $aliasedExternalAlias;

/**
* @var PsalmCustomString
*/
public mixed $psalmLocalAlias;

/**
* @var PsalmCustomInt
*/
public mixed $psalmExternalAlias;

/**
* @var PsalmAliasedCustomInt
*/
public mixed $psalmOtherAliasedExternalAlias;
}

/**
* @phpstan-import-type Invalid from DummyWithTypeAliases
*/
final class DummyWithInvalidTypeAliasImport
{
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

use Symfony\Component\TypeInfo\Type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?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;

final class ReflectionExtractableDummy extends AbstractDummy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
namespace Symfony\Component\TypeInfo\Tests\TypeContext;

use PHPUnit\Framework\TestCase;
use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Tests\Fixtures\AbstractDummy;
use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithInvalidTypeAliasImport;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTypeAliases;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithUses;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
Expand Down Expand Up @@ -119,4 +122,49 @@ public function testDoNotCollectTemplatesWhenToStringTypeResolver()

$this->assertEquals([], $typeContextFactory->createFromClassName(DummyWithTemplates::class)->templates);
}

public function testCollectTypeAliases()
{
$this->assertEquals([
'CustomString' => Type::string(),
'CustomInt' => Type::int(),
'AliasedCustomInt' => Type::int(),
'PsalmCustomString' => Type::string(),
'PsalmCustomInt' => Type::int(),
'PsalmAliasedCustomInt' => Type::int(),
], $this->typeContextFactory->createFromClassName(DummyWithTypeAliases::class)->typeAliases);

$this->assertEquals([
'CustomString' => Type::string(),
'CustomInt' => Type::int(),
'AliasedCustomInt' => Type::int(),
'PsalmCustomString' => Type::string(),
'PsalmCustomInt' => Type::int(),
'PsalmAliasedCustomInt' => Type::int(),
], $this->typeContextFactory->createFromReflection(new \ReflectionClass(DummyWithTypeAliases::class))->typeAliases);

$this->assertEquals([
'CustomString' => Type::string(),
'CustomInt' => Type::int(),
'AliasedCustomInt' => Type::int(),
'PsalmCustomString' => Type::string(),
'PsalmCustomInt' => Type::int(),
'PsalmAliasedCustomInt' => Type::int(),
], $this->typeContextFactory->createFromReflection(new \ReflectionProperty(DummyWithTypeAliases::class, 'localAlias'))->typeAliases);
}

public function testDoNotCollectTypeAliasesWhenToStringTypeResolver()
{
$typeContextFactory = new TypeContextFactory();

$this->assertEquals([], $typeContextFactory->createFromClassName(DummyWithTypeAliases::class)->typeAliases);
}

public function testThrowWhenImportingInvalidAlias()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage(\sprintf('Cannot find any "Invalid" type alias in "%s".', DummyWithTypeAliases::class));

$this->typeContextFactory->createFromClassName(DummyWithInvalidTypeAliasImport::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@

class PhpDocAwareReflectionTypeResolverTest extends TestCase
{
public function testReadPhpDoc()
/**
* @dataProvider readPhpDocDataProvider
*/
public function testReadPhpDoc(Type $expected, \Reflector $reflector)
{
$resolver = new PhpDocAwareReflectionTypeResolver(TypeResolver::create(), new StringTypeResolver(), new TypeContextFactory(new StringTypeResolver()));

$this->assertEquals($expected, $resolver->resolve($reflector));
}

/**
* @return iterable<array{0: Type, 1: \Reflector}>
*/
public static function readPhpDocDataProvider(): iterable
{
$resolver = new PhpDocAwareReflectionTypeResolver(TypeResolver::create(), new StringTypeResolver(), new TypeContextFactory());
$reflection = new \ReflectionClass(DummyWithPhpDoc::class);

$this->assertEquals(Type::array(Type::object(Dummy::class)), $resolver->resolve($reflection->getProperty('arrayOfDummies')));
$this->assertEquals(Type::bool(), $resolver->resolve($reflection->getProperty('promoted')));
$this->assertEquals(Type::object(Dummy::class), $resolver->resolve($reflection->getMethod('getNextDummy')));
$this->assertEquals(Type::object(Dummy::class), $resolver->resolve($reflection->getMethod('getNextDummy')->getParameters()[0]));
yield [Type::array(Type::object(Dummy::class)), $reflection->getProperty('arrayOfDummies')];
yield [Type::bool(), $reflection->getProperty('promoted')];
yield [Type::object(Dummy::class), $reflection->getMethod('getNextDummy')];
yield [Type::object(Dummy::class), $reflection->getMethod('getNextDummy')->getParameters()[0]];
yield [Type::int(), $reflection->getProperty('aliasedInt')];
}

public function testFallbackWhenNoPhpDoc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyCollection;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTypeAliases;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
Expand Down Expand Up @@ -175,6 +176,10 @@ public static function resolveDataProvider(): iterable
yield [Type::collection(Type::object(\IteratorAggregate::class), Type::string()), \IteratorAggregate::class.'<string>'];
yield [Type::collection(Type::object(\IteratorAggregate::class), Type::bool(), Type::string()), \IteratorAggregate::class.'<string, bool>'];
yield [Type::collection(Type::object(DummyCollection::class), Type::bool(), Type::string()), DummyCollection::class.'<string, bool>'];

// type aliases
yield [Type::int(), 'CustomInt', $typeContextFactory->createFromClassName(DummyWithTypeAliases::class)];
yield [Type::string(), 'CustomString', $typeContextFactory->createFromClassName(DummyWithTypeAliases::class)];
}

public function testCannotResolveNonStringType()
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/TypeInfo/TypeContext/TypeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ final class TypeContext
/**
* @param array<string, string> $uses
* @param array<string, Type> $templates
* @param array<string, Type> $typeAliases
*/
public function __construct(
public readonly string $calledClassName,
public readonly string $declaringClassName,
public readonly ?string $namespace = null,
public readonly array $uses = [],
public readonly array $templates = [],
public readonly array $typeAliases = [],
) {
}

Expand Down
Loading
Loading