Skip to content
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

feat: [5.x] php minimum to 8.1 #2362

Merged
merged 18 commits into from
Oct 22, 2024
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
8 changes: 1 addition & 7 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ jobs:
fail-fast: false
matrix:
include:
- php-version: 7.4
- php-version: 8.1
composer-flags: "--prefer-lowest"
doctrine-annotations: true
- php-version: 7.4
symfony-require: "5.4.*"
doctrine-annotations: true
- php-version: 8.0
symfony-require: "5.4.*"
doctrine-annotations: true
- php-version: 8.1
symfony-require: "5.4.*"
doctrine-annotations: true
Expand Down
3 changes: 3 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Upgrading From 4.x To 5.0

## BC BREAK: Bumped minimum PHP version to 8.1
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=8.1",
"ext-json": "*",
"phpdocumentor/reflection-docblock": "^4.3.4 || ^5.0",
"phpdocumentor/reflection-docblock": "^5.0",
"phpdocumentor/type-resolver": "^1.8.2",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"psr/container": "^1.0 || ^2.0",
Expand All @@ -32,17 +32,16 @@
},
"require-dev": {
"api-platform/core": "^2.7.0 || ^3",
"composer/package-versions-deprecated": "1.11.99.1",
"doctrine/annotations": "^2.0",
"friendsofphp/php-cs-fixer": "^3.52",
"friendsofsymfony/rest-bundle": "^2.8 || ^3.0",
"jms/serializer": "^1.14 || ^3.0",
"jms/serializer-bundle": "^2.3 || ^3.0 || ^4.0 || ^5.0",
"friendsofsymfony/rest-bundle": "^3.2.0",
"jms/serializer": "^3.0",
"jms/serializer-bundle": "^5.2",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^9.6 || ^10.5",
"phpunit/phpunit": "^10.5",
"symfony/asset": "^5.4 || ^6.4 || ^7.0",
"symfony/browser-kit": "^5.4 || ^6.4 || ^7.0",
"symfony/cache": "^5.4 || ^6.4 || ^7.0",
Expand All @@ -58,7 +57,8 @@
"symfony/twig-bundle": "^5.4 || ^6.4 || ^7.0",
"symfony/uid": "^5.4 || ^6.4 || ^7.0",
"symfony/validator": "^5.4 || ^6.4 || ^7.0",
"willdurand/hateoas-bundle": "^1.0 || ^2.0"
"willdurand/hateoas-bundle": "^2.0",
"willdurand/negotiation": "^3.0"
},
"conflict": {
"zircote/swagger-php": "4.8.7"
Expand Down
64 changes: 31 additions & 33 deletions src/DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,45 +184,43 @@ public function load(array $configs, ContainerBuilder $container): void
->setArgument(1, $config['media_types']);
}

if (PHP_VERSION_ID > 80100) {
// Add autoconfiguration for route argument describer
$container->registerForAutoconfiguration(RouteArgumentDescriberInterface::class)
->addTag('nelmio_api_doc.route_argument_describer');
// Add autoconfiguration for route argument describer
$container->registerForAutoconfiguration(RouteArgumentDescriberInterface::class)
->addTag('nelmio_api_doc.route_argument_describer');

$container->register('nelmio_api_doc.route_describers.route_argument', RouteArgumentDescriber::class)
$container->register('nelmio_api_doc.route_describers.route_argument', RouteArgumentDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_describer', ['priority' => -225])
->setArguments([
new Reference('argument_metadata_factory'),
new TaggedIteratorArgument('nelmio_api_doc.route_argument_describer'),
])
;

if (class_exists(MapQueryString::class)) {
$container->register('nelmio_api_doc.route_argument_describer.map_query_string', SymfonyMapQueryStringDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_describer', ['priority' => -225])
->setArguments([
new Reference('argument_metadata_factory'),
new TaggedIteratorArgument('nelmio_api_doc.route_argument_describer'),
])
;

if (class_exists(MapQueryString::class)) {
$container->register('nelmio_api_doc.route_argument_describer.map_query_string', SymfonyMapQueryStringDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_argument_describer', ['priority' => 0]);
->addTag('nelmio_api_doc.route_argument_describer', ['priority' => 0]);

$container->register('nelmio_api_doc.swagger.processor.map_query_string', MapQueryStringProcessor::class)
->setPublic(false)
->addTag('nelmio_api_doc.swagger.processor', ['priority' => 0]);
}
$container->register('nelmio_api_doc.swagger.processor.map_query_string', MapQueryStringProcessor::class)
->setPublic(false)
->addTag('nelmio_api_doc.swagger.processor', ['priority' => 0]);
}

if (class_exists(MapRequestPayload::class)) {
$container->register('nelmio_api_doc.route_argument_describer.map_request_payload', SymfonyMapRequestPayloadDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_argument_describer', ['priority' => 0]);
if (class_exists(MapRequestPayload::class)) {
$container->register('nelmio_api_doc.route_argument_describer.map_request_payload', SymfonyMapRequestPayloadDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_argument_describer', ['priority' => 0]);

$container->register('nelmio_api_doc.swagger.processor.map_request_payload', MapRequestPayloadProcessor::class)
->setPublic(false)
->addTag('nelmio_api_doc.swagger.processor', ['priority' => 0]);
}
$container->register('nelmio_api_doc.swagger.processor.map_request_payload', MapRequestPayloadProcessor::class)
->setPublic(false)
->addTag('nelmio_api_doc.swagger.processor', ['priority' => 0]);
}

if (class_exists(MapQueryParameter::class)) {
$container->register('nelmio_api_doc.route_argument_describer.map_query_parameter', SymfonyMapQueryParameterDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_argument_describer', ['priority' => 0]);
}
if (class_exists(MapQueryParameter::class)) {
$container->register('nelmio_api_doc.route_argument_describer.map_query_parameter', SymfonyMapQueryParameterDescriber::class)
->setPublic(false)
->addTag('nelmio_api_doc.route_argument_describer', ['priority' => 0]);
}

$bundles = $container->getParameter('kernel.bundles');
Expand Down
6 changes: 2 additions & 4 deletions src/ModelDescriber/Annotations/OpenApiAnnotationsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ private function getAnnotation(Context $parentContext, $reflection, string $clas
$this->setContextFromReflection($parentContext, $reflection);

try {
if (\PHP_VERSION_ID >= 80100) {
if (null !== $attribute = $reflection->getAttributes($className, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
return $attribute->newInstance();
}
if (null !== $attribute = $reflection->getAttributes($className, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
return $attribute->newInstance();
}

if (null !== $this->annotationsReader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private function getAnnotations(Context $parentContext, $reflection, ?array $val
*/
private function locateAnnotations($reflection): \Traversable
{
if (\PHP_VERSION_ID >= 80000 && class_exists(Constraint::class)) {
if (class_exists(Constraint::class)) {
foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
yield $attribute->newInstance();
}
Expand Down
3 changes: 0 additions & 3 deletions src/RouteDescriber/FosRestDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ private function describeCommonSchemaFromAnnotation(OA\Schema $schema, AbstractS
private function getAttributesAsAnnotation(\ReflectionMethod $reflection, string $className): array
{
$annotations = [];
if (\PHP_VERSION_ID < 80100) {
return $annotations;
}

foreach ($reflection->getAttributes($className, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$annotations[] = $attribute->newInstance();
Expand Down
12 changes: 3 additions & 9 deletions src/Routing/FilteredRouteCollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,9 @@ private function defaultRouteDisabled(Route $route): bool
? $this->annotationReader->getMethodAnnotations($method)
: [];

if (\PHP_VERSION_ID >= 80100) {
$annotations = array_merge($annotations, array_map(function (\ReflectionAttribute $attribute) {
return $attribute->newInstance();
}, $method->getAttributes(AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF)));
}
$annotations = array_merge($annotations, array_map(function (\ReflectionAttribute $attribute) {
return $attribute->newInstance();
}, $method->getAttributes(AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF)));

foreach ($annotations as $annotation) {
if (false !== strpos(get_class($annotation), 'Nelmio\\ApiDocBundle\\Annotation')
Expand All @@ -199,10 +197,6 @@ private function defaultRouteDisabled(Route $route): bool
private function getAttributesAsAnnotation($reflection, string $className): array
{
$annotations = [];
if (\PHP_VERSION_ID < 80100) {
return $annotations;
}

foreach ($reflection->getAttributes($className, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$annotations[] = $attribute->newInstance();
}
Expand Down
8 changes: 0 additions & 8 deletions tests/Functional/Configs/AlternativeNamesPHP80Entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,3 @@ nelmio_api_doc:
type: Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithPromotedPropertiesWithDefaults80
- alias: EntityWithAlternateType
type: Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithAlternateType80

# Clean unused components from the OpenAPI schema.
services:
OpenApi\Processors\CleanUnusedComponents:
tags:
- { name: 'nelmio_api_doc.swagger.processor', priority: -100 }
calls:
- setEnabled: [ true ]
8 changes: 0 additions & 8 deletions tests/Functional/Configs/AlternativeNamesPHP81Entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,3 @@ nelmio_api_doc:
type: Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithPromotedPropertiesWithDefaults81
- alias: EntityWithAlternateType
type: Nelmio\ApiDocBundle\Tests\Functional\Entity\EntityWithAlternateType81

# Clean unused components from the OpenAPI schema.
services:
OpenApi\Processors\CleanUnusedComponents:
tags:
- { name: 'nelmio_api_doc.swagger.processor', priority: -100 }
calls:
- setEnabled: [ true ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
OpenApi\Processors\CleanUnusedComponents:
tags:
- { name: 'nelmio_api_doc.swagger.processor', priority: -100 }
40 changes: 24 additions & 16 deletions tests/Functional/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use JMS\SerializerBundle\JMSSerializerBundle;
use OpenApi\Annotations as OA;
use OpenApi\Processors\CleanUnusedComponents;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
Expand Down Expand Up @@ -79,10 +80,6 @@ public function testControllers(?array $controller, ?string $fixtureName = null,

public static function provideAttributeTestCases(): \Generator
{
if (PHP_VERSION_ID < 80100) {
return;
}

$type = Kernel::MAJOR_VERSION === 5 ? 'annotation' : 'attribute';

yield 'Promoted properties defaults attributes' => [
Expand All @@ -92,7 +89,7 @@ public static function provideAttributeTestCases(): \Generator
],
'PromotedPropertiesDefaults',
[],
[__DIR__.'/Configs/AlternativeNamesPHP81Entities.yaml'],
[__DIR__.'/Configs/AlternativeNamesPHP81Entities.yaml', ...self::cleanUnusedComponentsConfig()],
];

yield 'JMS model opt out' => [
Expand Down Expand Up @@ -173,17 +170,15 @@ public static function provideAnnotationTestCases(): \Generator
return;
}

if (PHP_VERSION_ID >= 80000) {
yield 'Promoted properties defaults annotations' => [
[
'name' => 'PromotedPropertiesController80',
'type' => 'annotation',
],
'PromotedPropertiesDefaults',
[],
[__DIR__.'/Configs/AlternativeNamesPHP80Entities.yaml'],
];
}
yield 'Promoted properties defaults annotations' => [
[
'name' => 'PromotedPropertiesController80',
'type' => 'annotation',
],
'PromotedPropertiesDefaults',
[],
[__DIR__.'/Configs/AlternativeNamesPHP80Entities.yaml', ...self::cleanUnusedComponentsConfig()],
];
}

/**
Expand Down Expand Up @@ -213,4 +208,17 @@ private static function getFixture(string $fixture): string

return $content;
}

/**
* @return string[]
*/
private static function cleanUnusedComponentsConfig(): array
{
/* @phpstan-ignore-next-line */
if (method_exists(CleanUnusedComponents::class, 'setEnabled')) {
return [__DIR__.'/Configs/CleanUnusedComponentsProcessor.yaml'];
}

return [__DIR__.'/Configs/CleanUnusedComponentsProcessorNoSetter.yaml'];
}
}
33 changes: 8 additions & 25 deletions tests/Functional/Form/FormWithAlternateSchemaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,19 @@

namespace Nelmio\ApiDocBundle\Tests\Functional\Form;

use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

if (PHP_VERSION_ID < 80100) {
/**
* @OA\Schema(type="string")
*/
class FormWithAlternateSchemaType extends AbstractType
#[OAT\Schema(type: 'string')]
class FormWithAlternateSchemaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('ignored', TextType::class, [
'required' => false,
]);
}
}
} else {
#[OAT\Schema(type: 'string')]
class FormWithAlternateSchemaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('ignored', TextType::class, [
'required' => false,
]);
}
$builder
->add('ignored', TextType::class, [
'required' => false,
]);
}
}
33 changes: 8 additions & 25 deletions tests/Functional/Form/FormWithRefType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,19 @@

namespace Nelmio\ApiDocBundle\Tests\Functional\Form;

use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

if (PHP_VERSION_ID < 80100) {
/**
* @OA\Schema(ref="#/components/schemas/Test")
*/
class FormWithRefType extends AbstractType
#[OAT\Schema(ref: '#/components/schemas/Test')]
class FormWithRefType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('ignored', TextType::class, [
'required' => false,
]);
}
}
} else {
#[OAT\Schema(ref: '#/components/schemas/Test')]
class FormWithRefType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('ignored', TextType::class, [
'required' => false,
]);
}
$builder
->add('ignored', TextType::class, [
'required' => false,
]);
}
}
Loading
Loading