8000 [TypeInfo] Add `ExplicitStringType` and `ClassLikeStringType` classes to hold specific type details for `string` builtin types by DerManoMann · Pull Request #59833 · symfony/symfony · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
Add ExplicitStringType classes to hold specific type details for `s…
…tring` builtin types
  • Loading branch information
DerManoMann committed Feb 22, 2025
commit 12f2e97647941989ccce1359517816a06aa39fd9
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::explicitString('class-string')];
}

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 @@ -4,6 +4,7 @@ CHANGELOG
7.3
---

* Add `ExplicitStringType` classes to hold specific type details for `string` builtin types
* Add `Type::accepts()` method
* Add `TypeFactoryTrait::fromValue()` method
* Deprecate constructing a `CollectionType` instance as a list that is not an array
Expand Down
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 @@ -106,18 +106,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 +134,21 @@ 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::explicitString('trait-string'), 'trait-string'];
yield [Type::explicitString('interface-string'), 'interface-string'];

// 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/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>
*/
final 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;
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\TypeInfo\Type\BuiltinType;
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 +71,11 @@ public static function string(): BuiltinType
return self::builtin(TypeIdentifier::STRING);
}

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

/**
* @return BuiltinType<TypeIdentifier::BOOL>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,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 +149,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
0