Skip to content

[TypeInfo] Remove deprecated code for non-array based collections #60988

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

Open
wants to merge 1 commit into
base: 8.0
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions UPGRADE-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,19 @@ TwigBundle

* Make `TemplateCacheWarmer` class `final`

TypeInfo
--------

* Constructing a `CollectionType` instance as a list that is not an array throws an `InvalidArgumentException`
* Remove the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead

```diff
use Symfony\Component\TypeInfo\Type;

-$type = Type::iterable(Type::string(), asList: true);
+$type = Type::list(Type::string());
```

Validator
---------

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/TypeInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
CHANGELOG
=========

8.0
---

* Constructing a `CollectionType` instance as a list that is not an array throws an `InvalidArgumentException`
* Remove the third `$asList` argument of `TypeFactoryTrait::iterable()`, use `TypeFactoryTrait::list()` instead

```diff
use Symfony\Component\TypeInfo\Type;

-$type = Type::iterable(Type::string(), asList: true);
+$type = Type::list(Type::string());
```
Comment on lines +10 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't put diffs in changelogs 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, but I am getting really mixed signals. In another PR, the UPGRADE contains before/after examples, the CHANGELOG did not. There it is suggested (by a Symfony Core Member) to also add before/after examples to the CHANGELOG. In this PR, it is suggested, to not do it.

I am happy to do the changes, but it appears that - unless I am misunderstanding things - there seems to be no consensus on this topic 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this example is necessary tbh, the sentance is self-sufficient to me. Anyway I'd say diffs are cool for CHANGELOGs as well as UPGRADE files :)


7.3
---

Expand Down
10 changes: 3 additions & 7 deletions src/Symfony/Component/TypeInfo/Tests/Type/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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 @@ -21,8 +20,6 @@

class CollectionTypeTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;

public function testCannotCreateInvalidBuiltinType()
{
$this->expectException(InvalidArgumentException::class);
Expand Down Expand Up @@ -126,12 +123,11 @@ public function testAccepts()
$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.');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot create a "Symfony\Component\TypeInfo\Type\CollectionType" as list when type is not "array".');

new CollectionType(Type::generic(Type::builtin(TypeIdentifier::ITERABLE), Type::bool()), isList: true);
}

Expand Down
12 changes: 0 additions & 12 deletions src/Symfony/Component/TypeInfo/Tests/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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 @@ -31,8 +30,6 @@

class TypeFactoryTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;

public function testCreateBuiltin()
{
$this->assertEquals(new BuiltinType(TypeIdentifier::INT), Type::builtin(TypeIdentifier::INT));
Expand Down Expand Up @@ -284,13 +281,4 @@ 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);
}
}
3 changes: 1 addition & 2 deletions src/Symfony/Component/TypeInfo/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ 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));
throw new InvalidArgumentException(\sprintf('Cannot create a "%s" as list when type is not "array".', self::class));
}

$keyType = $this->getCollectionKeyType();
Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Component/TypeInfo/TypeFactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,9 @@ public static function array(?Type $value = null, ?Type $key = null, bool $asLis
/**
* @return CollectionType<BuiltinType<TypeIdentifier::ITERABLE>>
*/
public static function iterable(?Type $value = null, ?Type $key = null, bool $asList = false): CollectionType
public static function iterable(?Type $value = null, ?Type $key = null): 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);
return self::collection(self::builtin(TypeIdentifier::ITERABLE), $value, $key);
}

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