Skip to content

Commit

Permalink
refactor: [5.x] constructor property promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
DjordyKoert committed Nov 15, 2024
1 parent 8c47289 commit b98617f
Show file tree
Hide file tree
Showing 29 changed files with 115 additions and 307 deletions.
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
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
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
18 changes: 6 additions & 12 deletions src/Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,16 @@ final class ModelRegistry
private array $names = [];

/**
* @var iterable<ModelDescriberInterface>
*/
private iterable $modelDescribers;

private OA\OpenApi $api;

/**
* @param ModelDescriberInterface[]|iterable $modelDescribers
* @param array<string, mixed> $alternativeNames
* @param iterable<ModelDescriberInterface> $modelDescribers
* @param array<string, mixed> $alternativeNames
*
* @internal
*/
public function __construct($modelDescribers, OA\OpenApi $api, array $alternativeNames = [])
public function __construct(
private iterable $modelDescribers,
private OA\OpenApi $api,
array $alternativeNames = [])
{
$this->modelDescribers = $modelDescribers;
$this->api = $api;
$this->logger = new NullLogger();
foreach (array_reverse($alternativeNames) as $alternativeName => $criteria) {
$this->alternativeNames[] = $model = new Model(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ class SymfonyConstraintAnnotationReader
{
use SetsContextTrait;

/**
* @var OA\Schema
*/
private $schema;
private OA\Schema $schema;

private bool $useValidationGroups;

public function __construct(bool $useValidationGroups = false)
{
$this->useValidationGroups = $useValidationGroups;
public function __construct(
private bool $useValidationGroups = false,
) {
}

/**
Expand Down Expand Up @@ -68,11 +63,6 @@ private function processPropertyAttributes($reflection, OA\Property $property, a
return;
}

// The field is required
if (null === $this->schema) {
return;
}

$propertyName = Util::getSchemaPropertyName($this->schema, $property);
if (null === $propertyName) {
return;
Expand Down
11 changes: 4 additions & 7 deletions src/ModelDescriber/BazingaHateoasModelDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ class BazingaHateoasModelDescriber implements ModelDescriberInterface, ModelRegi
{
use ModelRegistryAwareTrait;

private MetadataFactoryInterface $factory;
private JMSModelDescriber $JMSModelDescriber;

public function __construct(MetadataFactoryInterface $factory, JMSModelDescriber $JMSModelDescriber)
{
$this->factory = $factory;
$this->JMSModelDescriber = $JMSModelDescriber;
public function __construct(
private MetadataFactoryInterface $factory,
private JMSModelDescriber $JMSModelDescriber,
) {
}

public function setModelRegistry(ModelRegistry $modelRegistry): void
Expand Down
Loading

0 comments on commit b98617f

Please sign in to comment.