Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d4566b2

Browse filesBrowse files
committed
feature #59302 [TypeInfo] Deprecate CollectionType as list and not as array (mtarld)
This PR was merged into the 7.3 branch. Discussion ---------- [TypeInfo] Deprecate `CollectionType` as list and not as array | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | yes | Issues | | License | MIT As mentioned in #59291 (comment), creating a list with a type that is not an `array` is not possible. The `CollectionType` should reflect that, that's why this PR deprecates the creation of such types. Commits ------- 50ad14d [TypeInfo] Deprecate creation of `CollectionType` as list and not as array
2 parents b3a9f51 + 50ad14d commit d4566b2
Copy full SHA for d4566b2

File tree

7 files changed

+43
-10
lines changed
Filter options

7 files changed

+43
-10
lines changed

‎UPGRADE-7.3.md

Copy file name to clipboardExpand all lines: UPGRADE-7.3.md
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ Validator
9898
)
9999
```
100100

101+
TypeInfo
102+
--------
103+
104+
* Deprecate constructing a `CollectionType` instance as a list that is not an array
105+
* Deprecate the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead
106+
101107
VarDumper
102108
---------
103109

‎src/Symfony/Component/TypeInfo/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/CHANGELOG.md
+2Lines 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 constructing a `CollectionType` instance as a list that is not an array
10+
* Deprecate the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead
911

1012
7.2
1113
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/Tests/Type/CollectionTypeTest.php
+12Lines 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" 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php
+12-9Lines 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: The third argument of "Symfony\Component\TypeInfo\TypeFactoryTrait::iterable()" is deprecated. Use the "Symfony\Component\TypeInfo\Type::list()" method to create a list instead.');
267+
Type::iterable(key: Type::int(), asList: true);
268+
}
266269
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/Type/CollectionType.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ 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" in 8.0.', self::class, InvalidArgumentException::class);
44+
// throw new InvalidArgumentException(\sprintf('Cannot create a "%s" as list when type is not "array".', self::class));
45+
}
46+
4247
$keyType = $this->getCollectionKeyType();
4348

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

‎src/Symfony/Component/TypeInfo/TypeFactoryTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
+4Lines 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 ($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

Copy file name to clipboardExpand all lines: src/Symfony/Component/TypeInfo/composer.json
+2-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.