8000 [TypeInfo] Deprecate `CollectionType` as list and not as array by mtarld · Pull Request #59302 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TypeInfo] Deprecate CollectionType as list and not as array #59302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issu 8000 e 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

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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
6 changes: 6 additions & 0 deletions UPGRADE-7.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Validator
)
```

TypeInfo
--------

* 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

VarDumper
---------

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/TypeInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* Add `Type::accepts()` method
* Add `TypeFactoryTrait::fromValue()` method
* 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

7.2
---
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/TypeInfo/Tests/Type/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\TypeInfo\Tests\Type;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
Expand All @@ -20,6 +21,8 @@

class CollectionTypeTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;

public function testCannotCreateInvalidBuiltinType()
{
$this->expectException(InvalidArgumentException::class);
Expand Down Expand Up @@ -122,4 +125,13 @@ public function testAccepts()
$this->assertTrue($type->accepts(new \ArrayObject([0 => true, 1 => false])));
$this->assertFalse($type->accepts(new \ArrayObject([0 => true, 1 => 'string'])));
}

/**
* @group legacy
*/
public function testCannotCreateIterableList()
{
$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.');
new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ITERABLE), Type::bool()), isList: true);
}
}
21 changes: 12 additions & 9 deletions src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\TypeInfo\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyBackedEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyEnum;
use Symfony\Component\TypeInfo\Type;
Expand All @@ -29,6 +30,8 @@

class TypeFactoryTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;

public function testCreateBuiltin()
{
$this->assertEquals(new BuiltinType(TypeIdentifier::INT), Type::builtin(TypeIdentifier::INT));
Expand Down Expand Up @@ -136,15 +139,6 @@ public function testCreateIterable()
)),
Type::iterable(Type::bool(), Type::string()),
);

$this->assertEquals(
new CollectionType(new GenericType(
new BuiltinType(TypeIdentifier::ITERABLE),
new BuiltinType(TypeIdentifier::INT),
new BuiltinType(TypeIdentifier::BOOL),
), isList: true),
Type::iterable(Type::bool(), Type::int(), true),
);
}

public function testCreateObject()
Expand Down Expand Up @@ -263,4 +257,13 @@ public function offsetUnset(mixed $offset): void
yield [Type::collection(Type::object(\Generator::class), Type::string(), Type::int()), (fn (): iterable => yield 'string')()];
yield [Type::collection(Type::object($arrayAccess::class)), $arrayAccess];
}

/**
* @group legacy
*/
public function testCannotCreateIterableList()
{
$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.');
Type::iterable(key: Type::int(), asList: true);
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/TypeInfo/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function __construct(
}

if ($this->isList()) {
if (!$type->isIdentifiedBy(TypeIdentifier::ARRAY)) {
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);
// throw new InvalidArgumentException(\sprintf('Cannot create a "%s" as list when type is not "array".', self::class));
}

$keyType = $this->getCollectionKeyType();

if (!$keyType instanceof BuiltinType || TypeIdentifier::INT !== $keyType->getTypeIdentifier()) {
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public static function array(?Type $value = null, ?Type $key = null, bool $asLis
*/
public static function iterable(?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
{
if ($asList) {
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);
}

return self::collection(self::builtin(TypeIdentifier::ITERABLE), $value, $key, $asList);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/TypeInfo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
],
"require": {
"php": ">=8.2",
"psr/container": "^1.1|^2.0"
"psr/container": "^1.1|^2.0",
"symfony/deprecation-contracts": "^2.5|^3"
},
"require-dev": {
"phpstan/phpdoc-parser": "^1.0|^2.0"
Expand Down
Loading
0