Skip to content

Commit a655ddf

Browse files
authored
chore(symfony): deprecations (#6401)
1 parent e06c88b commit a655ddf

File tree

15 files changed

+291
-40
lines changed

15 files changed

+291
-40
lines changed

.github/workflows/ci.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,16 @@ jobs:
259259
tools: pecl, composer
260260
extensions: intl, bcmath, curl, openssl, mbstring, pdo_sqlite, mongodb
261261
ini-values: memory_limit=-1
262-
- name: Run ${{ matrix.component }} tests
262+
- name: Run ${{ matrix.component }} install
263263
run: |
264264
composer update
265265
composer ${{matrix.component}} update
266+
- name: PHP version tweaks
267+
if: matrix.component == 'api-platform/metadata' && matrix.php != '8.1'
268+
run: composer require symfony/type-info
269+
working-directory: 'src/Metadata'
270+
- name: Run ${{ matrix.component }} tests
271+
run: |
266272
mkdir -p /tmp/build/logs/phpunit
267273
composer ${{matrix.component}} test --log-junit "/tmp/build/logs/phpunit/junit.xml" ${{ matrix.coverage && '--coverage-clover /tmp/build/logs/phpunit/clover.xml' || '' }}
268274
- name: Upload test artifacts

phpstan.neon.dist

-3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,3 @@ parameters:
101101
- '#Class Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface not found\.#'
102102
- '#Access to undefined constant Symfony\\Component\\HttpKernel\\HttpKernelInterface::MASTER_REQUEST\.#'
103103
- '#Attribute class PHPUnit\\Framework\\Attributes\\DataProvider does not exist.#'
104-
# Because of a bc-layer in MetadataAwareNameConverter it does not implement AdvancedNameConverterInterface
105-
- '#Instanceof between Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface\|null and Symfony\\Component\\Serializer\\NameConverter\\MetadataAwareNameConverter will always evaluate to false\.#'
106-
- '#Instanceof between Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface and Symfony\\Component\\Serializer\\NameConverter\\MetadataAwareNameConverter will always evaluate to false\.#'

phpunit.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<server name="KERNEL_CLASS" value="AppKernel" />
1313
<env name="APP_ENV" value="test" />
1414
<env name="SYMFONY_PHPUNIT_REQUIRE" value="nikic/php-parser:^4.16"/>
15+
<!-- to remove behat extensions in the AppKernel -->
16+
<env name="APP_PHPUNIT" value="true"/>
1517
</php>
1618

1719
<testsuites>

phpunit10.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<server name="KERNEL_DIR" value="tests/Fixtures/app/"/>
1111
<server name="KERNEL_CLASS" value="AppKernel"/>
1212
<env name="APP_ENV" value="test"/>
13+
<!-- to remove behat extensions in the AppKernel -->
14+
<env name="APP_PHPUNIT" value="true"/>
1315
</php>
1416

1517
<testsuites>

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

+26-19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Doctrine\Odm\PropertyInfo;
1515

16+
use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
1617
use Doctrine\Common\Collections\Collection;
1718
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDbClassMetadata;
1819
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
@@ -22,7 +23,8 @@
2223
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
2324
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
2425
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
25-
use Symfony\Component\PropertyInfo\Type;
26+
use Symfony\Component\PropertyInfo\Type as LegacyType;
27+
use Symfony\Component\TypeInfo\Type;
2628

2729
/**
2830
* Extracts data using Doctrine MongoDB ODM metadata.
@@ -53,9 +55,9 @@ public function getProperties($class, array $context = []): ?array
5355
/**
5456
* {@inheritdoc}
5557
*
56-
* @return Type[]|null
58+
* @return LegacyType[]|null
5759
*/
58-
public function getTypes($class, $property, array $context = []): ?array
60+
public function getTypes(string $class, string $property, array $context = []): ?array
5961
{
6062
if (null === $metadata = $this->getMetadata($class)) {
6163
return null;
@@ -72,19 +74,19 @@ public function getTypes($class, $property, array $context = []): ?array
7274
if ($metadata->isSingleValuedAssociation($property)) {
7375
$nullable = $metadata instanceof MongoDbClassMetadata && $metadata->isNullable($property);
7476

75-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)];
77+
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, $class)];
7678
}
7779

78-
$collectionKeyType = Type::BUILTIN_TYPE_INT;
80+
$collectionKeyType = LegacyType::BUILTIN_TYPE_INT;
7981

8082
return [
81-
new Type(
82-
Type::BUILTIN_TYPE_OBJECT,
83+
new LegacyType(
84+
LegacyType::BUILTIN_TYPE_OBJECT,
8385
false,
8486
Collection::class,
8587
true,
86-
new Type($collectionKeyType),
87-
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
88+
new LegacyType($collectionKeyType),
89+
new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, $class)
8890
),
8991
];
9092
}
@@ -94,18 +96,18 @@ public function getTypes($class, $property, array $context = []): ?array
9496
$nullable = $metadata instanceof MongoDbClassMetadata && $metadata->isNullable($property);
9597
$enumType = null;
9698
if (null !== $enumClass = $metadata instanceof MongoDbClassMetadata ? $metadata->getFieldMapping($property)['enumType'] ?? null : null) {
97-
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
99+
$enumType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
98100
}
99101

100102
switch ($typeOfField) {
101103
case MongoDbType::DATE:
102-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, \DateTime::class)];
104+
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, \DateTime::class)];
103105
case MongoDbType::DATE_IMMUTABLE:
104-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, \DateTimeImmutable::class)];
106+
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, \DateTimeImmutable::class)];
105107
case MongoDbType::HASH:
106-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
108+
return [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
107109
case MongoDbType::COLLECTION:
108-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT))];
110+
return [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, $nullable, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT))];
109111
case MongoDbType::INT:
110112
case MongoDbType::STRING:
111113
if ($enumType) {
@@ -115,7 +117,7 @@ public function getTypes($class, $property, array $context = []): ?array
115117

116118
$builtinType = $this->getPhpType($typeOfField);
117119

118-
return $builtinType ? [new Type($builtinType, $nullable)] : null;
120+
return $builtinType ? [new LegacyType($builtinType, $nullable)] : null;
119121
}
120122

121123
return null;
@@ -154,16 +156,21 @@ private function getMetadata(string $class): ?ClassMetadata
154156
}
155157
}
156158

159+
public function getType(string $class, string $property, array $context = []): ?Type
160+
{
161+
return PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($this->getTypes($class, $property, $context));
162+
}
163+
157164
/**
158165
* Gets the corresponding built-in PHP type.
159166
*/
160167
private function getPhpType(string $doctrineType): ?string
161168
{
162169
return match ($doctrineType) {
163-
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => Type::BUILTIN_TYPE_INT,
164-
MongoDbType::FLOAT => Type::BUILTIN_TYPE_FLOAT,
165-
MongoDbType::STRING, MongoDbType::ID, MongoDbType::OBJECTID, MongoDbType::TIMESTAMP, MongoDbType::BINDATA, MongoDbType::BINDATABYTEARRAY, MongoDbType::BINDATACUSTOM, MongoDbType::BINDATAFUNC, MongoDbType::BINDATAMD5, MongoDbType::BINDATAUUID, MongoDbType::BINDATAUUIDRFC4122 => Type::BUILTIN_TYPE_STRING,
166-
MongoDbType::BOOLEAN, MongoDbType::BOOL => Type::BUILTIN_TYPE_BOOL,
170+
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => LegacyType::BUILTIN_TYPE_INT,
171+
MongoDbType::FLOAT => LegacyType::BUILTIN_TYPE_FLOAT,
172+
MongoDbType::STRING, MongoDbType::ID, MongoDbType::OBJECTID, MongoDbType::TIMESTAMP, MongoDbType::BINDATA, MongoDbType::BINDATABYTEARRAY, MongoDbType::BINDATACUSTOM, MongoDbType::BINDATAFUNC, MongoDbType::BINDATAMD5, MongoDbType::BINDATAUUID, MongoDbType::BINDATAUUIDRFC4122 => LegacyType::BUILTIN_TYPE_STRING,
173+
MongoDbType::BOOLEAN, MongoDbType::BOOL => LegacyType::BUILTIN_TYPE_BOOL,
167174
default => null,
168175
};
169176
}

src/Doctrine/Odm/Tests/Fixtures/CustomConverter.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
namespace ApiPlatform\Doctrine\Odm\Tests\Fixtures;
1515

16+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
1617
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1718
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1819

1920
/**
2021
* Custom converter that will only convert a property named "nameConverted"
2122
* with the same logic as Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter.
2223
*/
23-
class CustomConverter implements NameConverterInterface
24+
class CustomConverter implements AdvancedNameConverterInterface
2425
{
2526
private NameConverterInterface $nameConverter;
2627

@@ -29,12 +30,12 @@ public function __construct()
2930
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter();
3031
}
3132

32-
public function normalize(string $propertyName): string
33+
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3334
{
3435
return 'nameConverted' === $propertyName ? $this->nameConverter->normalize($propertyName) : $propertyName;
3536
}
3637

37-
public function denormalize(string $propertyName): string
38+
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3839
{
3940
return 'name_converted' === $propertyName ? $this->nameConverter->denormalize($propertyName) : $propertyName;
4041
}

src/Doctrine/Orm/Tests/Fixtures/CustomConverter.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
namespace ApiPlatform\Doctrine\Orm\Tests\Fixtures;
1515

16+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
1617
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1718
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1819

1920
/**
2021
* Custom converter that will only convert a property named "nameConverted"
2122
* with the same logic as Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter.
2223
*/
23-
class CustomConverter implements NameConverterInterface
24+
class CustomConverter implements AdvancedNameConverterInterface
2425
{
2526
private NameConverterInterface $nameConverter;
2627

@@ -29,12 +30,12 @@ public function __construct()
2930
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter();
3031
}
3132

32-
public function normalize(string $propertyName): string
33+
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3334
{
3435
return 'nameConverted' === $propertyName ? $this->nameConverter->normalize($propertyName) : $propertyName;
3536
}
3637

37-
public function denormalize(string $propertyName): string
38+
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3839
{
3940
return 'name_converted' === $propertyName ? $this->nameConverter->denormalize($propertyName) : $propertyName;
4041
}

src/GraphQl/Tests/Fixtures/Serializer/NameConverter/CustomConverter.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
namespace ApiPlatform\GraphQl\Tests\Fixtures\Serializer\NameConverter;
1515

16+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
1617
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1718
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1819

1920
/**
2021
* Custom converter that will only convert a property named "nameConverted"
2122
* with the same logic as Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter.
2223
*/
23-
class CustomConverter implements NameConverterInterface
24+
class CustomConverter implements AdvancedNameConverterInterface
2425
{
2526
private NameConverterInterface $nameConverter;
2627

@@ -29,12 +30,12 @@ public function __construct()
2930
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter();
3031
}
3132

32-
public function normalize(string $propertyName): string
33+
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3334
{
3435
return 'nameConverted' === $propertyName ? $this->nameConverter->normalize($propertyName) : $propertyName;
3536
}
3637

37-
public function denormalize(string $propertyName): string
38+
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
3839
{
3940
return 'name_converted' === $propertyName ? $this->nameConverter->denormalize($propertyName) : $propertyName;
4041
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
/*
15+
* This file is part of the Symfony package.
16+
*
17+
* (c) Fabien Potencier <[email protected]>
18+
*
19+
* For the full copyright and license information, please view the LICENSE
20+
* file that was distributed with this source code.
21+
*/
22+
23+
namespace ApiPlatform\Metadata\Tests\Util;
24+
25+
use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
26+
use PHPUnit\Framework\TestCase;
27+
use Symfony\Component\PropertyInfo\Type as LegacyType;
28+
use Symfony\Component\TypeInfo\Type;
29+
use Symfony\Component\TypeInfo\TypeIdentifier;
30+
31+
/**
32+
* @group legacy
33+
*/
34+
class PropertyInfoToTypeInfoHelperTest extends TestCase
35+
{
36+
/**
37+
* @dataProvider convertLegacyTypesToTypeDataProvider
38+
*
39+
* @param list<LegacyType>|null $legacyTypes
40+
*/
41+
public function testConvertLegacyTypesToType(?Type $type, ?array $legacyTypes): void
42+
{
43+
if (!class_exists(Type::class)) {
44+
$this->markTestSkipped('symfony/type-info requires PHP > 8.2');
45+
}
46+
47+
$this->assertEquals($type, PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($legacyTypes));
48+
}
49+
50+
/**
51+
* @return iterable<array{0: ?Type, 1: list<LegacyType>|null}>
52+
*/
53+
public function convertLegacyTypesToTypeDataProvider(): iterable
54+
{
55+
if (!class_exists(Type::class)) {
56+
return;
57+
}
58+
59+
yield [null, null];
60+
yield [Type::null(), [new LegacyType('null')]];
61+
// yield [Type::void(), [new LegacyType('void')]];
62+
yield [Type::int(), [new LegacyType('int')]];
63+
yield [Type::object(\stdClass::class), [new LegacyType('object', false, \stdClass::class)]];
64+
yield [
65+
Type::generic(Type::object('Foo'), Type::string(), Type::int()), // @phpstan-ignore-line
66+
[new LegacyType('object', false, 'Foo', false, [new LegacyType('string')], new LegacyType('int'))],
67+
];
68+
yield [Type::nullable(Type::int()), [new LegacyType('int', true)]]; // @phpstan-ignore-line
69+
yield [Type::union(Type::int(), Type::string()), [new LegacyType('int'), new LegacyType('string')]];
70+
yield [
71+
Type::union(Type::int(), Type::string(), Type::null()),
72+
[new LegacyType('int', true), new LegacyType('string', true)],
73+
];
74+
75+
$type = Type::collection(Type::builtin(TypeIdentifier::ARRAY), Type::int(), Type::string()); // @phpstan-ignore-line
76+
yield [$type, [new LegacyType('array', false, null, true, [new LegacyType('string')], new LegacyType('int'))]];
77+
}
78+
}

0 commit comments

Comments
 (0)