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

Skip to content

Commit 3e57ac6

Browse files
committed
[TypeInfo] Deprecate creation of CollectionType as list and not as array
1 parent 78f4d9a commit 3e57ac6

File tree

7 files changed

+55
-10
lines changed

7 files changed

+55
-10
lines changed

UPGRADE-7.3.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
UPGRADE FROM 7.2 to 7.3
2+
=======================
3+
4+
Symfony 7.3 is a minor release. According to the Symfony release process, there should be no significant
5+
backward compatibility breaks. Minor backward compatibility breaks are prefixed in this document with
6+
`[BC BREAK]`, make sure your code is compatible with these entries before upgrading.
7+
Read more about this in the [Symfony documentation](https://symfony.com/doc/7.3/setup/upgrade_minor.html).
8+
9+
If you're upgrading from a version below 7.2, follow the [7.2 upgrade guide](UPGRADE-7.2.md) first.
10+
11+
TypeInfo
12+
--------
13+
14+
* Deprecate creation of `CollectionType` as list and not as array
15+
* Deprecate `TypeFactoryTrait::iterable`'s `$asList` argument

src/Symfony/Component/TypeInfo/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Deprecate creation of `CollectionType` as list and not as array
8+
* Deprecate `TypeFactoryTrait::iterable`'s `$asList` argument
9+
410
7.2
511
---
612

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

+12
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);
@@ -88,4 +91,13 @@ public function testToString()
8891
$type = new CollectionType(new GenericType(Type::builtin(TypeIdentifier::ARRAY), Type::string(), Type::bool()));
8992
$this->assertEquals('array<string,bool>', (string) $type);
9093
}
94+
95+
/**
96+
* @group legacy
97+
*/
98+
public function testCannotCreateIterableList()
99+
{
100+
$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.');
101+
new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ITERABLE), Type::bool()), isList: true);
102+
}
91103
}

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

+12-9
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()
@@ -206,4 +200,13 @@ public function testCreateNullable()
206200
Type::nullable(Type::union(Type::int(), Type::string(), Type::null())),
207201
);
208202
}
203+
204+
/**
205+
* @group legacy
206+
*/
207+
public function testCannotCreateIterableList()
208+
{
209+
$this->expectUserDeprecationMessage('Since symfony/type-info 7.3: "$asList" argument is deprecated, it will always be considered as "false" in 8.0.');
210+
Type::iterable(key: Type::int(), asList: true);
211+
}
209212
}

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

+4
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

+4
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

+2-1
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