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

refactor: [5.x] property promotion #2399

Open
wants to merge 7 commits into
base: 5.x
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
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PHP84Migration' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP84Migration' => true,
'header_comment' => [
'header' => <<<HEADER
This file is part of the NelmioApiDocBundle package.
Expand Down
18 changes: 18 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ Future proofing for potential future changes and keeping it consistent with `des
- `Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait::setModelRegistry()`
- `Nelmio\ApiDocBundle\PropertyDescriber\PropertyDescriberInterface::describe()`
- `Nelmio\ApiDocBundle\RouteDescriber\RouteDescriberInterface::describe()`

## BC BREAK: `Nelmio\ApiDocBundle\Attribute\Area::__construct()` `$properties`been changed:
- `$properties` has been renamed to `$areas`
```diff
-#[Areas(properties: ['foo', 'bar'])]
+#[Areas(areas: ['foo', 'bar'])]
```
- `$properties` no longer allows an array key `values` with a list of strings, pass a list of string instead
```diff
-/** @param string[]|array{value: string[]} $properties */
-#[Areas(properties: ['values' => ['foo', 'bar']])]
-[Areas(['values' => ['foo', 'bar']])]
+/** @param string[] $areas */
+#[Areas(areas: ['foo', 'bar'])]
+#[Areas(['foo', 'bar'])]
```

## BC BREAK: `Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException` has been made final
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ parameters:
count: 2
path: src/Render/Html/HtmlOpenApiRenderer.php

-
message: "#^Class Twig_Environment not found\\.$#"
count: 1
path: src/Render/Html/HtmlOpenApiRenderer.php

-
message: "#^Parameter \\$twig of method Nelmio\\\\ApiDocBundle\\\\Render\\\\Html\\\\HtmlOpenApiRenderer\\:\\:__construct\\(\\) has invalid type Twig_Environment\\.$#"
count: 1
Expand Down
24 changes: 9 additions & 15 deletions src/ApiDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ final class ApiDocGenerator
/** @var OpenApi */
private $openApi;

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

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

private ?CacheItemPoolInterface $cacheItemPool;

private string $cacheItemId;

/** @var string[] */
Expand All @@ -53,14 +45,16 @@ final class ApiDocGenerator
private Generator $generator;

/**
* @param DescriberInterface[]|iterable $describers
* @param ModelDescriberInterface[]|iterable $modelDescribers
* @param iterable<DescriberInterface> $describers
* @param iterable<ModelDescriberInterface> $modelDescribers
*/
public function __construct($describers, $modelDescribers, ?CacheItemPoolInterface $cacheItemPool = null, ?string $cacheItemId = null, ?Generator $generator = null)
{
$this->describers = $describers;
$this->modelDescribers = $modelDescribers;
$this->cacheItemPool = $cacheItemPool;
public function __construct(
private iterable $describers,
private iterable $modelDescribers,
private ?CacheItemPoolInterface $cacheItemPool = null,
?string $cacheItemId = null,
?Generator $generator = null,
) {
$this->cacheItemId = $cacheItemId ?? 'openapi_doc';
$this->generator = $generator ?? new Generator($this->logger);
}
Expand Down
27 changes: 5 additions & 22 deletions src/Attribute/Areas.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,17 @@
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
final class Areas
{
/** @var string[] */
private array $areas;

/**
* @param string[]|array{value: string[]} $properties
* @param string[] $areas
*/
public function __construct(array $properties)
{
if (!\array_key_exists('value', $properties) || !\is_array($properties['value'])) {
$properties['value'] = array_values($properties);
}

if ([] === $properties['value']) {
throw new \InvalidArgumentException('An array of areas was expected');
}

$areas = [];
foreach ($properties['value'] as $area) {
public function __construct(
private array $areas,
) {
foreach ($areas as $area) {
if (!\is_string($area)) {
throw new \InvalidArgumentException('An area must be given as a string');
}

if (!\in_array($area, $areas, true)) {
$areas[] = $area;
}
}

$this->areas = $areas;
}

public function has(string $area): bool
Expand Down
25 changes: 4 additions & 21 deletions src/Attribute/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ final class Model extends Attachable
Parameter::class,
];

public string $type;

/**
* @var string[]|null
*/
public ?array $groups;

/**
* @var mixed[]
*/
public array $options;

/**
* @var array<string, mixed>
*/
public array $serializationContext;

/**
* @param mixed[] $properties
* @param string[] $groups
Expand All @@ -55,10 +38,10 @@ final class Model extends Attachable
*/
public function __construct(
array $properties = [],
string $type = Generator::UNDEFINED,
?array $groups = null,
array $options = [],
array $serializationContext = [],
public string $type = Generator::UNDEFINED,
public ?array $groups = null,
public array $options = [],
public array $serializationContext = [],
) {
parent::__construct($properties + [
'type' => $type,
Expand Down
11 changes: 2 additions & 9 deletions src/Attribute/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@ class Security extends AbstractAnnotation

public static $_required = ['name'];

public ?string $name;

/**
* @var string[]
*/
public array $scopes = [];

/**
* @param array<string, mixed> $properties
* @param string[] $scopes
*/
public function __construct(
array $properties = [],
?string $name = null,
array $scopes = [],
public ?string $name = null,
public array $scopes = [],
) {
parent::__construct($properties + [
'name' => $name,
Expand Down
11 changes: 4 additions & 7 deletions src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@

final class DumpCommand extends Command
{
private RenderOpenApi $renderOpenApi;

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

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

public function __construct(
private RenderOpenApi $renderOpenApi,
) {
parent::__construct();
}

Expand Down
8 changes: 3 additions & 5 deletions src/Controller/DocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

final class DocumentationController
{
private RenderOpenApi $renderOpenApi;

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

public function __invoke(Request $request, string $area = 'default'): JsonResponse
Expand Down
12 changes: 4 additions & 8 deletions src/Controller/SwaggerUiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@

final class SwaggerUiController
{
private RenderOpenApi $renderOpenApi;

private string $uiRenderer;

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

public function __invoke(Request $request, string $area = 'default'): Response
Expand Down
8 changes: 3 additions & 5 deletions src/Controller/YamlDocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

final class YamlDocumentationController
{
private RenderOpenApi $renderOpenApi;

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

public function __invoke(Request $request, string $area = 'default'): Response
Expand Down
15 changes: 4 additions & 11 deletions src/Describer/ExternalDocDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@

class ExternalDocDescriber implements DescriberInterface
{
/**
* @var array<string, mixed>|callable
*/
private $externalDoc;

private bool $overwrite;

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

public function describe(OA\OpenApi $api): void
Expand Down
14 changes: 5 additions & 9 deletions src/Describer/OpenApiPhpDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ final class OpenApiPhpDescriber
{
use SetsContextTrait;

private RouteCollection $routeCollection;
private ControllerReflector $controllerReflector;
private LoggerInterface $logger;

public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, LoggerInterface $logger)
{
$this->routeCollection = $routeCollection;
$this->controllerReflector = $controllerReflector;
$this->logger = $logger;
public function __construct(
private RouteCollection $routeCollection,
private ControllerReflector $controllerReflector,
private LoggerInterface $logger,
) {
}

public function describe(OA\OpenApi $api): void
Expand Down
21 changes: 6 additions & 15 deletions src/Describer/RouteDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,14 @@ final class RouteDescriber implements DescriberInterface, ModelRegistryAwareInte
{
use ModelRegistryAwareTrait;

private RouteCollection $routeCollection;

private ControllerReflector $controllerReflector;

/**
* @var iterable<RouteDescriberInterface>
*/
private iterable $routeDescribers;

/**
* @param RouteDescriberInterface[]|iterable $routeDescribers
* @param iterable<RouteDescriberInterface> $routeDescribers
*/
public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, $routeDescribers)
{
$this->routeCollection = $routeCollection;
$this->controllerReflector = $controllerReflector;
$this->routeDescribers = $routeDescribers;
public function __construct(
private RouteCollection $routeCollection,
private ControllerReflector $controllerReflector,
private iterable $routeDescribers,
) {
}

public function describe(OA\OpenApi $api): void
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/RenderInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

namespace Nelmio\ApiDocBundle\Exception;

class RenderInvalidArgumentException extends \InvalidArgumentException
final class RenderInvalidArgumentException extends \InvalidArgumentException
{
}
21 changes: 5 additions & 16 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,17 @@

final class Model
{
private Type $type;

/**
* @var mixed[]
*/
private array $options;

/**
* @var mixed[]
*/
private array $serializationContext;

/**
* @param string[]|null $groups
* @param mixed[] $options
* @param mixed[] $serializationContext
*/
public function __construct(Type $type, ?array $groups = null, array $options = [], array $serializationContext = [])
public function __construct(
private Type $type,
?array $groups = null,
private array $options = [],
private array $serializationContext = [])
{
$this->type = $type;
$this->options = $options;
$this->serializationContext = $serializationContext;
if (null !== $groups) {
$this->serializationContext['groups'] = $groups;
}
Expand Down
Loading
Loading