8000 [TypeInfo] Deprecate creation of `CollectionType` as list and not as … · symfony/symfony@9902e58 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9902e58

Browse files
committed
[TypeInfo] Deprecate creation of CollectionType as list and not as array
1 parent 0051b15 commit 9902e58

File tree

7 files changed

+42
-10
lines changed

7 files changed

+42
-10
lines changed

UPGRADE-7.3.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ Serializer
4242

4343
* Deprecate the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes
4444

45+
TypeInfo
46+
--------
47+
48+
* Deprecate creation of `CollectionType` as list and not as array
49+
* Deprecate `TypeFactoryTrait::iterable`'s `$asList` argument
50+
4551
VarDumper
4652
---------
4753

src/Symfony/Component/TypeInfo/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* Add `Type::accepts()` method
88
* Add `TypeFactoryTrait::fromValue()` method
9+
* Deprecate creation of `CollectionType` as list and not as array
10+
* Deprecate `TypeFactoryTrait::iterable`'s `$asList` argument
911

1012
7.2
1113
---

src/Symfony/Component/TypeInfo/Tests/Type/CollectionTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\TypeInfo\Tests\Type;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
1516
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
1617
use Symfony\Component\TypeInfo\Type;
1718
use Symfony\Component\TypeInfo\Type\CollectionType;
@@ -20,6 +21,8 @@
2021

2122
class CollectionTypeTest extends TestCase
2223
{
24+
use ExpectUserDeprecationMessageTrait;
25+
2326
public function testCannotCreateInvalidBuiltinType()
2427
{
2528
$this->expectException(InvalidArgumentException::class);
@@ -122,4 +125,13 @@ public function testAccepts()
122125
$this->assertTrue($type->accepts(new \ArrayObject([0 => true, 1 => false])));
123126
$this->assertFalse($type->accepts(new \ArrayObject([0 => true, 1 => 'string'])));
124127
}
128+
129+
/**
130+
* @group legacy
131+
*/
132+
public function testCannotCreateIterableList()
133+
{
134+
$this->expectUserDeprecationMessage('Since symfony/type-info 7.3: Creating a "Symfony\Component\TypeInfo\Type\CollectionType" that is a list and not an array is deprecated and will throw a "Symfony\Component\TypeInfo\Exception\InvalidArgumentException" exception in 8.0.');
135+
new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ITERABLE), Type::bool()), isList: true);
136+
}
125137
}

src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\TypeInfo\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
1516
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyBackedEnum;
1617
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyEnum;
1718
use Symfony\Component\TypeInfo\Type;
@@ -29,6 +30,8 @@
2930

3031
class TypeFactoryTest extends TestCase
3132
{
33+
use ExpectUserDeprecationMessageTrait;
34+
3235
public function testCreateBuiltin()
3336
{
3437
$this->assertEquals(new BuiltinType(TypeIdentifier::INT), Type::builtin(TypeIdentifier::INT));
@@ -136,15 +139,6 @@ public function testCreateIterable()
136139
)),
137140
Type::iterable(Type::bool(), Type::string()),
138141
);
139-
140-
$this->assertEquals(
141-
new CollectionType(new GenericType(
142-
new BuiltinType(TypeIdentifier::ITERABLE),
143-
new BuiltinType(TypeIdentifier::INT),
144-
new BuiltinType(TypeIdentifier::BOOL),
145-
), isList: true),
146-
Type::iterable(Type::bool(), Type::int(), true),
147-
);
148142
}
149143

150144
public function testCreateObject()
@@ -263,4 +257,13 @@ public function offsetUnset(mixed $offset): void
263257
yield [Type::collection(Type::object(\Generator::class), Type::string(), Type::int()), (fn (): iterable => yield 'string')()];
264258
yield [Type::collection(Type::object($arrayAccess::class)), $arrayAccess];
265259
}
260+
261+
/**
262+
* @group legacy
263+
*/
264+
public function testCannotCreateIterableList()
265+
{
266+
$this->expectUserDeprecationMessage('Since symfony/type-info 7.3: "$asList" argument is deprecated, it will always be considered as "false" in 8.0.');
267+
Type::iterable(key: Type::int(), asList: true);
268+
}
266269
}

src/Symfony/Component/TypeInfo/Type/CollectionType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function __construct(
3939
}
4040

4141
if ($this->isList()) {
42+
if (!$type->isIdentifiedBy(TypeIdentifier::ARRAY)) {
43+
trigger_deprecation('symfony/type-info', '7.3', 'Creating a "%s" that is a list and not an array is deprecated and will throw a "%s" exception in 8.0.', self::class, InvalidArgumentException::class);
44+
}
45+
4246
$keyType = $this->getCollectionKeyType();
4347

4448
if (!$keyType instanceof BuiltinType || TypeIdentifier::INT !== $keyType->getTypeIdentifier()) {

src/Symfony/Component/TypeInfo/TypeFactoryTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ public static function array(?Type $value = null, ?Type $key = null, bool $asLis
171171
*/
172172
public static function iterable(?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
173173
{
174+
if (true === $asList) {
175+
trigger_deprecation('symfony/type-info', '7.3', 'The third argument of "%s()" is deprecated. Use the "%s::list()" method to create a list instead.', __METHOD__, self::class);
176+
}
177+
174178
return self::collection(self::builtin(TypeIdentifier::ITERABLE), $value, $key, $asList);
175179
}
176180

src/Symfony/Component/TypeInfo/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
],
2727
"require": {
2828
"php": ">=8.2",
29-
"psr/container": "^1.1|^2.0"
29+
"psr/container": "^1.1|^2.0",
30+
"symfony/deprecation-contracts": "^2.5|^3"
3031
},
3132
"require-dev": {
3233
"phpstan/phpdoc-parser": "^1.0|^2.0"

0 commit comments

Comments
 (0)
0