Skip to content

[TypeInfo] Add ExplicitStringType and ClassLikeStringType classes to hold specific type details for string builtin types #59833

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -946,23 +946,24 @@ public function testPseudoTypes(string $property, ?Type $type)
*/
public static function pseudoTypesProvider(): iterable
{
yield ['classString', Type::string()];
yield ['classString', Type::explicitString('class-string')];

// BC layer for type-info < 7.2
if (!interface_exists(WrappingTypeInterface::class)) {
yield ['classStringGeneric', Type::generic(Type::string(), Type::object(\stdClass::class))];
} else {
yield ['classStringGeneric', Type::string()];
yield ['classStringGeneric', Type::classLikeString('class-string', Type::object(\stdClass::class))];
}

yield ['htmlEscapedString', Type::string()];
yield ['lowercaseString', Type::string()];
yield ['nonEmptyLowercaseString', Type::string()];
yield ['nonEmptyString', Type::string()];
yield ['numericString', Type::string()];
yield ['traitString', Type::string()];
yield ['interfaceString', Type::string()];
yield ['literalString', Type::string()];
yield ['htmlEscapedString', Type::explicitString('html-escaped-string')];
yield ['lowercaseString', Type::explicitString('lowercase-string')];
yield ['nonEmptyLowercaseString', Type::explicitString('non-empty-lowercase-string')];
yield ['nonEmptyString', Type::explicitString('non-empty-string')];
yield ['numericString', Type::explicitString('numeric-string')];
yield ['traitString', Type::explicitString('trait-string')];
yield ['interfaceString', Type::explicitString('interface-string')];
yield ['literalString', Type::explicitString('literal-string')];

yield ['positiveInt', Type::int()];
yield ['negativeInt', Type::int()];
yield ['nonEmptyArray', Type::array()];
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/TypeInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* 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`
* Add `ExplicitStringType` and `ClassLikeStringType` classes to hold specific type details for `string` builtin types

7.2
---
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyInterface.php
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\TypeInfo\Tests\Fixtures;

interface DummyInterface
{
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyTrait.php
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\TypeInfo\Tests\Fixtures;

trait DummyTrait
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Type;

use PHPUnit\Framework\TestCase;
use Symfony\Component\TypeInfo\Type\ExplicitStringType;

class ExplicitStringTypeTest extends TestCase
{
public function testToString()
{
$this->assertSame('class-string', (string) new ExplicitStringType('class-string'));
}

public function testAccepts()
{
$this->assertFalse((new ExplicitStringType('interface-string'))->accepts(false));
$this->assertTrue((new ExplicitStringType('interface-string'))->accepts('Foo'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyBackedEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyCollection;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyInterface;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyTrait;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTypeAliases;
use Symfony\Component\TypeInfo\Type;
Expand Down Expand Up @@ -106,18 +108,6 @@ public static function resolveDataProvider(): iterable
yield [Type::float(), 'float'];
yield [Type::float(), 'double'];
yield [Type::string(), 'string'];
yield [Type::string(), 'class-string'];
yield [Type::string(), 'trait-string'];
yield [Type::string(), 'interface-string'];
yield [Type::string(), 'callable-string'];
yield [Type::string(), 'numeric-string'];
yield [Type::string(), 'lowercase-string'];
yield [Type::string(), 'non-empty-lowercase-string'];
yield [Type::string(), 'non-empty-string'];
yield [Type::string(), 'non-falsy-string'];
yield [Type::string(), 'truthy-string'];
yield [Type::string(), 'literal-string'];
yield [Type::string(), 'html-escaped-string'];
yield [Type::resource(), 'resource'];
yield [Type::object(), 'object'];
yield [Type::callable(), 'callable'];
Expand Down Expand Up @@ -146,6 +136,24 @@ public static function resolveDataProvider(): iterable
yield [Type::template('T', Type::union(Type::int(), Type::string())), 'T', $typeContextFactory->createFromClassName(DummyWithTemplates::class)];
yield [Type::template('V'), 'V', $typeContextFactory->createFromReflection(new \ReflectionMethod(DummyWithTemplates::class, 'getPrice'))];

// explicit string
yield [Type::explicitString('callable-string'), 'callable-string'];
yield [Type::explicitString('numeric-string'), 'numeric-string'];
yield [Type::explicitString('lowercase-string'), 'lowercase-string'];
yield [Type::explicitString('non-empty-lowercase-string'), 'non-empty-lowercase-string'];
yield [Type::explicitString('non-empty-string'), 'non-empty-string'];
yield [Type::explicitString('non-falsy-string'), 'non-falsy-string'];
yield [Type::explicitString('truthy-string'), 'truthy-string'];
yield [Type::explicitString('literal-string'), 'literal-string'];
yield [Type::explicitString('html-escaped-string'), 'html-escaped-string'];

yield [Type::explicitString('class-string'), 'class-string'];
yield [Type::classLikeString('class-string', Type::object(Dummy::class)), \sprintf('class-string<%s>', Dummy::class)];
yield [Type::explicitString('trait-string'), 'trait-string'];
yield [Type::classLikeString('trait-string', Type::object(DummyTrait::class)), \sprintf('trait-string<%s>', DummyTrait::class)];
yield [Type::explicitString('interface-string'), 'interface-string'];
yield [Type::classLikeString('interface-string', Type::object(DummyInterface::class)), \sprintf('interface-string<%s>', DummyInterface::class)];

// nullable
yield [Type::nullable(Type::int()), '?int'];

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/TypeInfo/Type/BuiltinType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @template T of TypeIdentifier
*/
final class BuiltinType extends Type
class BuiltinType extends Type
{
/**
* @param T $typeIdentifier
Expand Down
39 changes: 39 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/ClassLikeStringType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Type;

use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* Explicit string type.
*
* @author Martin Rademacher <mano@radebatz.net>
*
* @extends BuiltinType<TypeIdentifier::STRING>
*/
final class ClassLikeStringType extends ExplicitStringType
{
public function __construct(string $explicitType, private ObjectType $objectType)
{
parent::__construct($explicitType);
}

public function getObjectType(): ObjectType
{
return $this->objectType;
}

public function __toString(): string
{
return \sprintf('%s<%s>', $this->getExplicitType(), $this->objectType);
}
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/ExplicitStringType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Type;

use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* Explicit string type.
*
* @author Martin Rademacher <mano@radebatz.net>
*
* @extends BuiltinType<TypeIdentifier::STRING>
*/
class ExplicitStringType extends BuiltinType
{
public function __construct(private string $explicitType)
{
parent::__construct(TypeIdentifier::STRING);
}

public function getExplicitType(): string
{
return $this->explicitType;
}

public function __toString(): string
{
return $this->explicitType;
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

use Symfony\Component\TypeInfo\Type\BackedEnumType;
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\ClassLikeStringType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\EnumType;
use Symfony\Component\TypeInfo\Type\ExplicitStringType;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\IntersectionType;
use Symfony\Component\TypeInfo\Type\NullableType;
Expand Down Expand Up @@ -70,6 +72,16 @@ public static function string(): BuiltinType
return self::builtin(TypeIdentifier::STRING);
}

public static function explicitString(string $explicitType): ExplicitStringType
{
return new ExplicitStringType($explicitType);
}

public static function classLikeString(string $explicitType, ObjectType $objectType): ClassLikeStringType
{
return new ClassLikeStringType($explicitType, $objectType);
}

/**
* @return BuiltinType<TypeIdentifier::BOOL>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\BuiltinType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\ExplicitStringType;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
use Symfony\Component\TypeInfo\TypeIdentifier;
Expand Down Expand Up @@ -137,7 +138,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
'false' => Type::false(),
'int', 'integer', 'positive-int', 'negative-int', 'non-positive-int', 'non-negative-int', 'non-zero-int' => Type::int(),
'float', 'double' => Type::float(),
'string',
'string' => Type::string(),
'class-string',
'trait-string',
'interface-string',
Expand All @@ -149,7 +150,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
'non-falsy-string',
'truthy-string',
'literal-string',
'html-escaped-string' => Type::string(),
'html-escaped-string' => Type::explicitString($node->name),
'resource' => Type::resource(),
'object' => Type::object(),
'callable' => Type::callable(),
Expand Down Expand Up @@ -215,6 +216,12 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
};
}

if ($type instanceof ExplicitStringType
&& \in_array($type->getExplicitType(), ['class-string', 'interface-string', 'trait-string'], true)
&& 1 === \count($variableTypes) && $variableTypes[0] instanceof Type\ObjectType) {
return Type::classLikeString($type->getExplicitType(), $variableTypes[0]);
}

if ($type instanceof BuiltinType && TypeIdentifier::ARRAY !== $type->getTypeIdentifier() && TypeIdentifier::ITERABLE !== $type->getTypeIdentifier()) {
return $type;
}
Expand Down
Loading