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

Upgrade Symfony 6.0 dependencies #1914

14 changes: 4 additions & 10 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ jobs:
strategy:
matrix:
include:
- php-version: 7.1
composer-flags: "--prefer-lowest"
- php-version: 7.2
symfony-require: "^4.0"
- php-version: 7.3
symfony-require: "^5.0"
- php-version: 7.4
symfony-require: "^4.0"
- php-version: 7.3
symfony-require: "^5.0"
- php-version: 8.0
symfony-require: "^5.3"
- php-version: 8.0
symfony-require: "^6.0"
- php-version: 8.1
composer-flags: "--ignore-platform-reqs"

steps:
Expand Down
2 changes: 1 addition & 1 deletion Annotation/Areas.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
final class Areas
{
/** @var string[] */
private $areas;
private array $areas;

public function __construct(array $properties)
{
Expand Down
9 changes: 3 additions & 6 deletions Annotation/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ final class Model extends AbstractAnnotation
Parameter::class,
];

/**
* @var string
*/
public $type;
public string $type;

/**
* @var string[]
*/
public $groups;
public array $groups;
Copy link

Choose a reason for hiding this comment

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

This will lead to fatal errors too: only $type is required.


/**
* @var mixed[]
*/
public $options;
public array $options;
}
7 changes: 2 additions & 5 deletions Annotation/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ class Security extends AbstractAnnotation

public static $_required = ['name'];

/**
* @var string
*/
public $name;
public string $name;

/**
* @var string[]
*/
public $scopes = [];
public array $scopes = [];
}
40 changes: 16 additions & 24 deletions ApiDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,33 @@
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Generator;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerAwareTrait;

final class ApiDocGenerator
{
use LoggerAwareTrait;

/** @var OpenApi */
private $openApi;

/** @var iterable|DescriberInterface[] */
private $describers;

/** @var iterable|ModelDescriberInterface[] */
private $modelDescribers;

/** @var CacheItemPoolInterface|null */
private $cacheItemPool;

/** @var string|null */
private $cacheItemId;
private ?OpenApi $openApi = null;

/** @var string[] */
private $alternativeNames = [];
private array $alternativeNames = [];

/** @var string[] */
private $mediaTypes = ['json'];
private array $mediaTypes = ['json'];

/**
* @param DescriberInterface[]|iterable $describers
* @param iterable|DescriberInterface[] $describers
* @param ModelDescriberInterface[]|iterable $modelDescribers
*/
public function __construct($describers, $modelDescribers, CacheItemPoolInterface $cacheItemPool = null, string $cacheItemId = null)
{
$this->describers = $describers;
$this->modelDescribers = $modelDescribers;
$this->cacheItemPool = $cacheItemPool;
$this->cacheItemId = $cacheItemId;
public function __construct(
private iterable $describers,
private iterable $modelDescribers,
private ?CacheItemPoolInterface $cacheItemPool = null,
private ?string $cacheItemId = null
) {
}

public function setAlternativeNames(array $alternativeNames)
Expand All @@ -70,6 +59,9 @@ public function setMediaTypes(array $mediaTypes)
$this->mediaTypes = $mediaTypes;
}

/**
* @throws InvalidArgumentException
*/
public function generate(): OpenApi
{
if (null !== $this->openApi) {
Expand Down Expand Up @@ -110,7 +102,7 @@ public function generate(): OpenApi
$defaultOperationIdProcessor = new DefaultOperationId();
$defaultOperationIdProcessor($analysis);

$analysis->process();
$analysis->process((new Generator())->getProcessors());
$analysis->validate();

if (isset($item)) {
Expand Down
19 changes: 3 additions & 16 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,13 @@

class DumpCommand extends Command
{
/**
* @var RenderOpenApi
*/
private $renderOpenApi;

/**
* @var mixed[]
*/
private $defaultHtmlConfig = [
private array $defaultHtmlConfig = [
'assets_mode' => AssetsMode::CDN,
'swagger_ui_config' => [],
];

public function __construct(RenderOpenApi $renderOpenApi)
public function __construct(private RenderOpenApi $renderOpenApi)
{
$this->renderOpenApi = $renderOpenApi;

parent::__construct();
}

Expand All @@ -62,10 +52,7 @@ protected function configure()
;
}

/**
* @return int|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$area = $input->getOption('area');
$format = $input->getOption('format');
Expand Down
12 changes: 3 additions & 9 deletions Controller/DocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@

final class DocumentationController
{
/**
* @var RenderOpenApi
*/
private $renderOpenApi;

public function __construct(RenderOpenApi $renderOpenApi)
public function __construct(private RenderOpenApi $renderOpenApi)
{
$this->renderOpenApi = $renderOpenApi;
}

public function __invoke(Request $request, $area = 'default')
public function __invoke(Request $request, $area = 'default'): JsonResponse
{
try {
return JsonResponse::fromJsonString(
$this->renderOpenApi->renderFromRequest($request, RenderOpenApi::JSON, $area)
);
} catch (InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area));
}
}
Expand Down
15 changes: 4 additions & 11 deletions Controller/SwaggerUiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Nelmio\ApiDocBundle\Controller;

use InvalidArgumentException;
use Nelmio\ApiDocBundle\Render\Html\AssetsMode;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -20,17 +19,11 @@

final class SwaggerUiController
{
/**
* @var RenderOpenApi
*/
private $renderOpenApi;

public function __construct(RenderOpenApi $renderOpenApi)
public function __construct(private RenderOpenApi $renderOpenApi)
{
$this->renderOpenApi = $renderOpenApi;
}

public function __invoke(Request $request, $area = 'default')
public function __invoke(Request $request, $area = 'default'): Response
{
try {
$response = new Response(
Expand All @@ -42,9 +35,9 @@ public function __invoke(Request $request, $area = 'default')
);

return $response->setCharset('UTF-8');
} catch (InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
$advice = '';
if (false !== strpos($area, '.json')) {
if (str_contains($area, '.json')) {
$advice = ' Since the area provided contains `.json`, the issue is likely caused by route priorities. Try switching the Swagger UI / the json documentation routes order.';
}

Expand Down
13 changes: 3 additions & 10 deletions Controller/YamlDocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@

namespace Nelmio\ApiDocBundle\Controller;

use InvalidArgumentException;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

final class YamlDocumentationController
{
/**
* @var RenderOpenApi
*/
private $renderOpenApi;

public function __construct(RenderOpenApi $renderOpenApi)
public function __construct(private RenderOpenApi $renderOpenApi)
{
$this->renderOpenApi = $renderOpenApi;
}

public function __invoke(Request $request, $area = 'default')
public function __invoke(Request $request, $area = 'default'): Response
{
try {
$response = new Response(
Expand All @@ -39,7 +32,7 @@ public function __invoke(Request $request, $area = 'default')
);

return $response->setCharset('UTF-8');
} catch (InvalidArgumentException $e) {
} catch (\InvalidArgumentException) {
throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area));
}
}
Expand Down
10 changes: 2 additions & 8 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@

final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('nelmio_api_doc');

if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// symfony < 4.2 support
$rootNode = $treeBuilder->root('nelmio_api_doc');
}
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
Expand Down
5 changes: 3 additions & 2 deletions DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ public function load(array $configs, ContainerBuilder $container)
->setArgument(1, $config['media_types']);
}

// ApiPlatform support
$bundles = $container->getParameter('kernel.bundles');
if (!isset($bundles['TwigBundle'])) {
if (!isset($bundles['TwigBundle']) || !class_exists('Symfony\Component\Asset\Packages')) {
$container->removeDefinition('nelmio_api_doc.controller.swagger_ui');

$container->removeDefinition('nelmio_api_doc.render_docs.html');
$container->removeDefinition('nelmio_api_doc.render_docs.html.asset');
}

// ApiPlatform support
if (isset($bundles['ApiPlatformBundle']) && class_exists('ApiPlatform\Core\Documentation\Documentation')) {
$loader->load('api_platform.xml');
}
Expand Down
2 changes: 1 addition & 1 deletion Describer/ApiPlatformDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class ApiPlatformDescriber extends ExternalDocDescriber
public function __construct(Documentation $documentation, NormalizerInterface $normalizer)
{
if (!$normalizer->supportsNormalization($documentation, 'json')) {
throw new \InvalidArgumentException(sprintf('Argument 2 passed to %s() must implement %s and support normalization of %s. The normalizer provided is an instance of %s.', __METHOD__, NormalizerInterface::class, Documentation::class, get_class($normalizer)));
throw new \InvalidArgumentException(sprintf('Argument 2 passed to %s() must implement %s and support normalization of %s. The normalizer provided is an instance of %s.', __METHOD__, NormalizerInterface::class, Documentation::class, $normalizer::class));
}

parent::__construct(function () use ($documentation, $normalizer) {
Expand Down
9 changes: 5 additions & 4 deletions Describer/DefaultDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use OpenApi\Generator;

/**
* Makes the swagger documentation valid even if there are missing fields.
Expand All @@ -26,22 +27,22 @@ public function describe(OA\OpenApi $api)
// Info
/** @var OA\Info $info */
$info = Util::getChild($api, OA\Info::class);
if (OA\UNDEFINED === $info->title) {
if (Generator::UNDEFINED === $info->title) {
$info->title = '';
}
if (OA\UNDEFINED === $info->version) {
if (Generator::UNDEFINED === $info->version) {
$info->version = '0.0.0';
}

// Paths
if (OA\UNDEFINED === $api->paths) {
if (Generator::UNDEFINED === $api->paths) {
$api->paths = [];
}
foreach ($api->paths as $path) {
foreach (Util::OPERATIONS as $method) {
/** @var OA\Operation $operation */
$operation = $path->{$method};
if (OA\UNDEFINED !== $operation && null !== $operation && (OA\UNDEFINED === $operation->responses || empty($operation->responses))) {
if (Generator::UNDEFINED !== $operation && null !== $operation && (Generator::UNDEFINED === $operation->responses || empty($operation->responses))) {
/** @var OA\Response $response */
$response = Util::getIndexedCollectionItem($operation, OA\Response::class, 'default');
$response->description = '';
Expand Down
8 changes: 1 addition & 7 deletions Describer/ExternalDocDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ class ExternalDocDescriber implements DescriberInterface
{
private $externalDoc;

private $overwrite;

/**
* @param array|callable $externalDoc
*/
public function __construct($externalDoc, bool $overwrite = false)
public function __construct(callable|array $externalDoc, private bool $overwrite = false)
{
$this->externalDoc = $externalDoc;
$this->overwrite = $overwrite;
}

public function describe(OA\OpenApi $api)
Expand Down
5 changes: 1 addition & 4 deletions Describer/ModelRegistryAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

trait ModelRegistryAwareTrait
{
/**
* @var ModelRegistry
*/
private $modelRegistry;
private ModelRegistry $modelRegistry;

public function setModelRegistry(ModelRegistry $modelRegistry)
{
Expand Down
Loading