Skip to content

Commit

Permalink
refactor: PHPStan bump to level 6 (#2259)
Browse files Browse the repository at this point in the history
* PHPStan bump to level 6

* set checkGenericClassInNonGenericObjectType: false

* PHPStan level 6 typehints (src)

* add void return type to tests

* rewrite dataProviders to return Generator

* exclude more test fixtures

* PHPStan level 6 (tests)

* Apply suggestions from code review

Co-authored-by: Dominic Luidold <[email protected]>

* fix breaking changes to RenderOpenApi class

---------

Co-authored-by: Dominic Luidold <[email protected]>
  • Loading branch information
DjordyKoert and DominicLuidold authored Apr 15, 2024
1 parent 4b40eec commit 38682dd
Show file tree
Hide file tree
Showing 95 changed files with 1,266 additions and 875 deletions.
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
parameters:
ignoreErrors:
-
message: "#^Property Nelmio\\\\ApiDocBundle\\\\Annotation\\\\Model\\:\\:\\$_required type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Annotation/Model.php

-
message: "#^Property Nelmio\\\\ApiDocBundle\\\\Annotation\\\\Security\\:\\:\\$_required type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Annotation/Security.php

-
message: "#^Only booleans are allowed in an if condition, mixed given\\.$#"
count: 1
path: src/Describer/ExternalDocDescriber.php

-
message: "#^Method Nelmio\\\\ApiDocBundle\\\\PropertyDescriber\\\\PropertyDescriberInterface\\:\\:describe\\(\\) invoked with 5 parameters, 2\\-3 required\\.$#"
count: 1
Expand Down Expand Up @@ -50,6 +65,11 @@ parameters:
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
path: src/Render/Html/HtmlOpenApiRenderer.php

-
message: "#^Property Nelmio\\\\ApiDocBundle\\\\Render\\\\Html\\\\HtmlOpenApiRenderer\\:\\:\\$twig has unknown class Twig_Environment as its type\\.$#"
count: 1
Expand Down
6 changes: 5 additions & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
level: 5
level: 6
paths:
- src
- tests
excludePaths:
- tests/Functional/Entity/*
- tests/Functional/EntityExcluded/*
- tests/Functional/Controller/*
- tests/ModelDescriber/Annotations/Fixture/*
dynamicConstantNames:
- Symfony\Component\HttpKernel\Kernel::VERSION
- Symfony\Component\HttpKernel\Kernel::VERSION_ID
Expand All @@ -28,3 +29,6 @@ parameters:
universalObjectCratesClasses:
- OpenApi\Context
treatPhpDocTypesAsCertain: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- '#^Property class@anonymous/tests/.* has no type specified.$#'
2 changes: 1 addition & 1 deletion src/Annotation/Areas.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
final class Areas
{
/** @var string[] */
private $areas;
private array $areas;

/**
* @param string[]|array{value: string[]} $properties
Expand Down
15 changes: 6 additions & 9 deletions src/Annotation/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,22 @@ final class Model extends Attachable
Parameter::class,
];

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

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

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

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

/**
* @param mixed[] $properties
Expand Down
11 changes: 6 additions & 5 deletions src/Annotation/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class Security extends AbstractAnnotation

public static $_required = ['name'];

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

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

/**
* @param array<string, mixed> $properties
* @param string[] $scopes
*/
public function __construct(
array $properties = [],
?string $name = null,
Expand Down
12 changes: 9 additions & 3 deletions src/ApiDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,23 @@ public function __construct($describers, $modelDescribers, ?CacheItemPoolInterfa
$this->generator = $generator ?? new Generator($this->logger);
}

public function setAlternativeNames(array $alternativeNames)
/**
* @param string[] $alternativeNames
*/
public function setAlternativeNames(array $alternativeNames): void
{
$this->alternativeNames = $alternativeNames;
}

public function setMediaTypes(array $mediaTypes)
/**
* @param string[] $mediaTypes
*/
public function setMediaTypes(array $mediaTypes): void
{
$this->mediaTypes = $mediaTypes;
}

public function setOpenApiVersion(?string $openApiVersion)
public function setOpenApiVersion(?string $openApiVersion): void
{
$this->openApiVersion = $openApiVersion;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

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

/**
* @var mixed[]
Expand Down
7 changes: 2 additions & 5 deletions src/Controller/DocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

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

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

public function __invoke(Request $request, $area = 'default')
public function __invoke(Request $request, string $area = 'default'): JsonResponse
{
try {
return JsonResponse::fromJsonString(
Expand Down
12 changes: 3 additions & 9 deletions src/Controller/SwaggerUiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,17 @@

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

/**
* @var string
*/
private $uiRenderer;
private string $uiRenderer;

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

public function __invoke(Request $request, $area = 'default')
public function __invoke(Request $request, string $area = 'default'): Response
{
try {
$response = new Response(
Expand Down
7 changes: 2 additions & 5 deletions src/Controller/YamlDocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@

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

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

public function __invoke(Request $request, $area = 'default')
public function __invoke(Request $request, string $area = 'default'): Response
{
try {
$response = new Response(
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ public function load(array $configs, ContainerBuilder $container): void
}
}

/**
* @param array<int, mixed> $names
*
* @return array<string, mixed>
*/
private function findNameAliases(array $names, string $area): array
{
$nameAliases = array_filter($names, function (array $aliasInfo) use ($area) {
Expand Down
2 changes: 1 addition & 1 deletion src/Describer/DefaultDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
final class DefaultDescriber implements DescriberInterface
{
public function describe(OA\OpenApi $api)
public function describe(OA\OpenApi $api): void
{
// Info
/** @var OA\Info $info */
Expand Down
3 changes: 3 additions & 0 deletions src/Describer/DescriberInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@

interface DescriberInterface
{
/**
* @return void
*/
public function describe(OpenApi $api);
}
13 changes: 11 additions & 2 deletions src/Describer/ExternalDocDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@

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

private $overwrite;
private bool $overwrite;

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

/**
* @return void
*/
public function describe(OA\OpenApi $api)
{
$externalDoc = $this->getExternalDoc();
Expand All @@ -38,6 +44,9 @@ public function describe(OA\OpenApi $api)
}
}

/**
* @return mixed The external doc
*/
private function getExternalDoc()
{
if (is_callable($this->externalDoc)) {
Expand Down
3 changes: 3 additions & 0 deletions src/Describer/ModelRegistryAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@

interface ModelRegistryAwareInterface
{
/**
* @return void
*/
public function setModelRegistry(ModelRegistry $modelRegistry);
}
6 changes: 3 additions & 3 deletions src/Describer/ModelRegistryAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

trait ModelRegistryAwareTrait
{
private ModelRegistry $modelRegistry;

/**
* @var ModelRegistry
* @return void
*/
private $modelRegistry;

public function setModelRegistry(ModelRegistry $modelRegistry)
{
$this->modelRegistry = $modelRegistry;
Expand Down
18 changes: 9 additions & 9 deletions src/Describer/OpenApiPhpDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ final class OpenApiPhpDescriber
{
use SetsContextTrait;

private $routeCollection;
private $controllerReflector;
private RouteCollection $routeCollection;
private ControllerReflector $controllerReflector;

/**
* @var Reader|null
*/
private $annotationReader;
private $logger;
private ?Reader $annotationReader;
private LoggerInterface $logger;

public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, ?Reader $annotationReader, LoggerInterface $logger, bool $overwrite = false)
{
Expand All @@ -52,7 +49,7 @@ public function __construct(RouteCollection $routeCollection, ControllerReflecto
$this->logger = $logger;
}

public function describe(OA\OpenApi $api)
public function describe(OA\OpenApi $api): void
{
$classAnnotations = [];

Expand Down Expand Up @@ -200,6 +197,9 @@ private function getMethodsToParse(): \Generator
}
}

/**
* @return string[]
*/
private function getSupportedHttpMethods(Route $route): array
{
$allMethods = Util::OPERATIONS;
Expand All @@ -223,7 +223,7 @@ private function normalizePath(string $path): string
}

/**
* @param \ReflectionClass|\ReflectionMethod $reflection
* @param \ReflectionClass<object>|\ReflectionMethod $reflection
*
* @return OA\AbstractAnnotation[]
*/
Expand Down
Loading

0 comments on commit 38682dd

Please sign in to comment.