From 7658fbc38a1e6f00c07bd5ec3ca8c74b88968a68 Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Fri, 27 May 2022 14:32:16 +1200 Subject: [PATCH 1/2] More tweaks to static analysis --- .../SchemaQueryParameter.php | 6 ++++-- .../schema-query-parameter/app/OpenApi.php | 17 +++++++++++++++++ .../schema-query-parameter/app/api-spec.php | 16 ---------------- .../processors/schema-query-parameter/scan.php | 9 ++++++--- composer.json | 3 +-- phpstan-baseline.neon | 10 ++++++++++ phpstan.neon | 7 +++++-- psalm.xml | 3 +++ tests/Processors/AugmentSchemasTest.php | 2 ++ tests/Processors/ExpandEnumsTest.php | 3 +-- tests/Processors/MergeJsonContentTest.php | 3 +++ tests/Processors/MergeXmlContentTest.php | 2 ++ 12 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 Examples/processors/schema-query-parameter/app/OpenApi.php delete mode 100644 Examples/processors/schema-query-parameter/app/api-spec.php diff --git a/Examples/processors/schema-query-parameter/SchemaQueryParameter.php b/Examples/processors/schema-query-parameter/SchemaQueryParameter.php index f93c2d40b..8b03fcae8 100644 --- a/Examples/processors/schema-query-parameter/SchemaQueryParameter.php +++ b/Examples/processors/schema-query-parameter/SchemaQueryParameter.php @@ -20,7 +20,9 @@ class SchemaQueryParameter public function __invoke(Analysis $analysis) { + /** @var Schema[] $schemas */ $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var Operation[] $operations */ $operations = $analysis->getAnnotationsOfType(Operation::class); foreach ($operations as $operation) { @@ -52,11 +54,11 @@ protected function schemaForRef(array $schemas, string $ref) */ protected function expandQueryArgs(Operation $operation, Schema $schema) { - if ($schema->properties === Generator::UNDEFINED || !$schema->properties) { + if ($schema->properties == Generator::UNDEFINED || !$schema->properties) { return; } - $operation->parameters = $operation->parameters === Generator::UNDEFINED ? [] : $operation->parameters; + $operation->parameters = $operation->parameters == Generator::UNDEFINED ? [] : $operation->parameters; foreach ($schema->properties as $property) { $parameter = new Parameter([ 'name' => $property->property, diff --git a/Examples/processors/schema-query-parameter/app/OpenApi.php b/Examples/processors/schema-query-parameter/app/OpenApi.php new file mode 100644 index 000000000..1e7897894 --- /dev/null +++ b/Examples/processors/schema-query-parameter/app/OpenApi.php @@ -0,0 +1,17 @@ + -Uses a custom processor `QueryArgsFromSchema` processor to convert a vendor extension into query parameters. -The parameters are extracted from the schema referenced by the custom extension. diff --git a/Examples/processors/schema-query-parameter/scan.php b/Examples/processors/schema-query-parameter/scan.php index 037cf32de..27ff62d4e 100644 --- a/Examples/processors/schema-query-parameter/scan.php +++ b/Examples/processors/schema-query-parameter/scan.php @@ -4,9 +4,12 @@ use OpenApi\Processors\BuildPaths; use SchemaQueryParameterProcessor\SchemaQueryParameter; -require __DIR__ . '/../../../vendor/autoload.php'; -// also load our custom processor... -require __DIR__ . '/SchemaQueryParameter.php'; +$classLoader = require __DIR__ . '/../../../vendor/autoload.php'; + +// register our app namespace... +$classLoader->addPsr4('App\\', __DIR__ . '/app'); +// and our custom processor +$classLoader->addPsr4('SchemaQueryParameterProcessor\\', __DIR__); $generator = new Generator(); diff --git a/composer.json b/composer.json index ce82aa05f..8728e63f9 100644 --- a/composer.json +++ b/composer.json @@ -70,8 +70,7 @@ "psr-4": { "OpenApi\\Tools\\": "tools/src/", "OpenApi\\Tests\\": "tests/", - "AnotherNamespace\\": "tests/Fixtures/AnotherNamespace", - "OpenApi\\Tests\\Fixtures\\Annotations\\": "tests/Fixtures/Annotations" + "AnotherNamespace\\": "tests/Fixtures/AnotherNamespace" } }, "scripts-descriptions": { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fb019c023..f0a6e99d5 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,15 @@ parameters: ignoreErrors: + - + message: "#^Result of && is always true\\.$#" + count: 1 + path: Examples/processors/sort-components/SortComponents.php + + - + message: "#^Attribute class JetBrains\\\\PhpStorm\\\\ArrayShape does not exist\\.$#" + count: 1 + path: Examples/using-links-php81/User.php + - message: "#^Access to an undefined property OpenApi\\\\Annotations\\\\AbstractAnnotation\\:\\:\\$description\\.$#" count: 1 diff --git a/phpstan.neon b/phpstan.neon index 08ad627dc..58e16e09e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,9 +6,12 @@ parameters: paths: - src - tests + - Examples parallel: processTimeout: 300.0 - ignoreErrors: - - '#does not accept default value of type #' excludePaths: - 'tests/Fixtures/*' + ignoreErrors: + - '#does not accept default value of type #' + ## Examples + - '#^Property [a-zA-Z0-9\\]+::\$[a-zA-Z]+ is unused.$#' diff --git a/psalm.xml b/psalm.xml index 035863639..e9e027b01 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,10 +6,13 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" errorBaseline="psalm-baseline.xml" + phpVersion="8.1" > + + diff --git a/tests/Processors/AugmentSchemasTest.php b/tests/Processors/AugmentSchemasTest.php index e12db4ae8..0125249d3 100644 --- a/tests/Processors/AugmentSchemasTest.php +++ b/tests/Processors/AugmentSchemasTest.php @@ -31,6 +31,7 @@ public function testAugmentSchemas(): void $analysis->process([new AugmentSchemas()]); $this->assertSame('Customer', $customer->schema, '@OA\Schema()->schema based on classname'); + $this->assertIsArray($customer->properties); $this->assertCount(10, $customer->properties, '@OA\Property()s are merged into the @OA\Schema of the class'); } @@ -49,6 +50,7 @@ public function testAugmentSchemasForInterface(): void $this->assertSame(Generator::UNDEFINED, $customer->properties, 'Sanity check. @OA\Property\'s not yet merged '); $analysis->process([new AugmentSchemas()]); + $this->assertIsArray($customer->properties); $this->assertCount(9, $customer->properties, '@OA\Property()s are merged into the @OA\Schema of the class'); } } diff --git a/tests/Processors/ExpandEnumsTest.php b/tests/Processors/ExpandEnumsTest.php index cb07f029c..c19a19615 100644 --- a/tests/Processors/ExpandEnumsTest.php +++ b/tests/Processors/ExpandEnumsTest.php @@ -1,5 +1,4 @@ -assertCount(1, $response->_unmerged); $analysis->process([new MergeJsonContent()]); + $this->assertIsArray($response->content); $this->assertCount(1, $response->content); $this->assertCount(0, $response->_unmerged); $json = json_decode(json_encode($response), true); @@ -70,9 +71,11 @@ public function testParameter(): void /** @var Parameter $parameter */ $parameter = $analysis->getAnnotationsOfType(Parameter::class)[0]; $this->assertSame(Generator::UNDEFINED, $parameter->content); + $this->assertIsArray($parameter->_unmerged); $this->assertCount(1, $parameter->_unmerged); $analysis->process([new MergeJsonContent()]); + $this->assertIsArray($parameter->content); $this->assertCount(1, $parameter->content); $this->assertCount(0, $parameter->_unmerged); $json = json_decode(json_encode($parameter), true); diff --git a/tests/Processors/MergeXmlContentTest.php b/tests/Processors/MergeXmlContentTest.php index e9d0d6a6f..76bfc3ff0 100644 --- a/tests/Processors/MergeXmlContentTest.php +++ b/tests/Processors/MergeXmlContentTest.php @@ -32,6 +32,7 @@ public function testXmlContent(): void $this->assertCount(1, $response->_unmerged); $analysis->process([new MergeXmlContent()]); + $this->assertIsArray($response->content); $this->assertCount(1, $response->content); $this->assertCount(0, $response->_unmerged); $json = json_decode(json_encode($response), true); @@ -72,6 +73,7 @@ public function testParameter(): void $this->assertCount(1, $parameter->_unmerged); $analysis->process([new MergeXmlContent()]); + $this->assertIsArray($parameter->content); $this->assertCount(1, $parameter->content); $this->assertCount(0, $parameter->_unmerged); $json = json_decode(json_encode($parameter), true); From 971c57295250f3eb2cfdfb2df7adbe0d8d09bbeb Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Sun, 29 May 2022 17:13:24 +1200 Subject: [PATCH 2/2] Fix type-hint issues around annotation/attribute Schema classes Attributes that inherit from `Annotations\Schema` typically do so by extending from their corresponding annotations. That means they do not have `Attributes\Schema` in their ancestor list. --- src/Annotations/Components.php | 2 +- src/Annotations/Schema.php | 6 ++-- src/Attributes/AdditionalProperties.php | 20 ++++++------- src/Attributes/Components.php | 22 +++++++------- src/Attributes/Items.php | 20 ++++++------- src/Attributes/JsonContent.php | 22 +++++++------- src/Attributes/Property.php | 20 ++++++------- src/Attributes/Schema.php | 22 +++++++------- src/Attributes/XmlContent.php | 22 +++++++------- tests/Samples/AnyOf.php | 38 +++++++++++++++++++++++++ tests/Samples/Readme.md | 3 ++ 11 files changed, 119 insertions(+), 78 deletions(-) create mode 100644 tests/Samples/AnyOf.php create mode 100644 tests/Samples/Readme.md diff --git a/src/Annotations/Components.php b/src/Annotations/Components.php index 1b5c3c944..ae88dc3e2 100644 --- a/src/Annotations/Components.php +++ b/src/Annotations/Components.php @@ -33,7 +33,7 @@ class Components extends AbstractAnnotation /** * Reusable Schemas. * - * @var Schema[] + * @var array */ public $schemas = Generator::UNDEFINED; diff --git a/src/Annotations/Schema.php b/src/Annotations/Schema.php index c1bf4bd02..c7f960b68 100644 --- a/src/Annotations/Schema.php +++ b/src/Annotations/Schema.php @@ -293,7 +293,7 @@ class Schema extends AbstractAnnotation * An instance validates successfully against this property if it validates successfully against all schemas * defined by this property's value. * - * @var Schema[] + * @var array */ public $allOf = Generator::UNDEFINED; @@ -301,7 +301,7 @@ class Schema extends AbstractAnnotation * An instance validates successfully against this property if it validates successfully against at least one * schema defined by this property's value. * - * @var Schema[] + * @var array */ public $anyOf = Generator::UNDEFINED; @@ -309,7 +309,7 @@ class Schema extends AbstractAnnotation * An instance validates successfully against this property if it validates successfully against exactly one schema * defined by this property's value. * - * @var Schema[] + * @var array */ public $oneOf = Generator::UNDEFINED; diff --git a/src/Attributes/AdditionalProperties.php b/src/Attributes/AdditionalProperties.php index 381f5edd7..bc2a7bafb 100644 --- a/src/Attributes/AdditionalProperties.php +++ b/src/Attributes/AdditionalProperties.php @@ -12,16 +12,16 @@ class AdditionalProperties extends \OpenApi\Annotations\AdditionalProperties { /** - * @param string[] $required - * @param Property[] $properties - * @param int|float $maximum - * @param int|float $minimum - * @param string[]|int[]|float[] $enum - * @param Schema[] $allOf - * @param Schema[] $anyOf - * @param Schema[] $oneOf - * @param array|null $x - * @param Attachable[]|null $attachables + * @param string[] $required + * @param Property[] $properties + * @param int|float $maximum + * @param int|float $minimum + * @param string[]|int[]|float[] $enum + * @param array $allOf + * @param array $anyOf + * @param array $oneOf + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( // schema diff --git a/src/Attributes/Components.php b/src/Attributes/Components.php index eb760c994..87c8834c6 100644 --- a/src/Attributes/Components.php +++ b/src/Attributes/Components.php @@ -12,17 +12,17 @@ class Components extends \OpenApi\Annotations\Components { /** - * @param Schema[]|null $schemas - * @param Response[]|null $responses - * @param Parameter[]|null $parameters - * @param RequestBody[]|null $requestBodies - * @param Examples[]|null $examples - * @param Header[]|null $headers - * @param SecurityScheme[]|null $securitySchemes - * @param Link[]|null $links - * @param callable[]|null $callbacks - * @param array|null $x - * @param Attachable[]|null $attachables + * @param array|null $schemas + * @param Response[]|null $responses + * @param Parameter[]|null $parameters + * @param RequestBody[]|null $requestBodies + * @param Examples[]|null $examples + * @param Header[]|null $headers + * @param SecurityScheme[]|null $securitySchemes + * @param Link[]|null $links + * @param callable[]|null $callbacks + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( ?array $schemas = null, diff --git a/src/Attributes/Items.php b/src/Attributes/Items.php index 76a2d5781..d94dc2243 100644 --- a/src/Attributes/Items.php +++ b/src/Attributes/Items.php @@ -12,16 +12,16 @@ class Items extends \OpenApi\Annotations\Items { /** - * @param string[] $required - * @param Property[] $properties - * @param int|float $maximum - * @param int|float $minimum - * @param string[]|int[]|float[] $enum - * @param Schema[] $allOf - * @param Schema[] $anyOf - * @param Schema[] $oneOf - * @param array|null $x - * @param Attachable[]|null $attachables + * @param string[] $required + * @param Property[] $properties + * @param int|float $maximum + * @param int|float $minimum + * @param string[]|int[]|float[] $enum + * @param array $allOf + * @param array $anyOf + * @param array $oneOf + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( // schema diff --git a/src/Attributes/JsonContent.php b/src/Attributes/JsonContent.php index 99d4e8105..21e6c4a6b 100644 --- a/src/Attributes/JsonContent.php +++ b/src/Attributes/JsonContent.php @@ -12,17 +12,17 @@ class JsonContent extends \OpenApi\Annotations\JsonContent { /** - * @param array $examples - * @param string[] $required - * @param Property[] $properties - * @param int|float $maximum - * @param int|float $minimum - * @param string[]|int[]|float[] $enum - * @param Schema[] $allOf - * @param Schema[] $anyOf - * @param Schema[] $oneOf - * @param array|null $x - * @param Attachable[]|null $attachables + * @param array $examples + * @param string[] $required + * @param Property[] $properties + * @param int|float $maximum + * @param int|float $minimum + * @param string[]|int[]|float[] $enum + * @param array $allOf + * @param array $anyOf + * @param array $oneOf + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( ?array $examples = null, diff --git a/src/Attributes/Property.php b/src/Attributes/Property.php index 7f6315d2b..ab4483742 100644 --- a/src/Attributes/Property.php +++ b/src/Attributes/Property.php @@ -12,16 +12,16 @@ class Property extends \OpenApi\Annotations\Property { /** - * @param string[] $required - * @param Property[] $properties - * @param int|float $maximum - * @param int|float $minimum - * @param string[]|int[]|float[] $enum - * @param Schema[] $allOf - * @param Schema[] $anyOf - * @param Schema[] $oneOf - * @param array|null $x - * @param Attachable[]|null $attachables + * @param string[] $required + * @param Property[] $properties + * @param int|float $maximum + * @param int|float $minimum + * @param string[]|int[]|float[] $enum + * @param array $allOf + * @param array $anyOf + * @param array $oneOf + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( ?string $property = null, diff --git a/src/Attributes/Schema.php b/src/Attributes/Schema.php index e8009d7f1..2fda6c773 100644 --- a/src/Attributes/Schema.php +++ b/src/Attributes/Schema.php @@ -12,17 +12,17 @@ class Schema extends \OpenApi\Annotations\Schema { /** - * @param string[] $required - * @param Property[] $properties - * @param int|float $maximum - * @param int|float $minimum - * @param string[]|int[]|float[] $enum - * @param Schema[] $allOf - * @param Schema[] $anyOf - * @param Schema[] $oneOf - * @param mixed $const - * @param array|null $x - * @param Attachable[]|null $attachables + * @param string[] $required + * @param Property[] $properties + * @param int|float $maximum + * @param int|float $minimum + * @param string[]|int[]|float[] $enum + * @param array $allOf + * @param array $anyOf + * @param array $oneOf + * @param mixed $const + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( // schema diff --git a/src/Attributes/XmlContent.php b/src/Attributes/XmlContent.php index 9e2b18957..5bf89f9e6 100644 --- a/src/Attributes/XmlContent.php +++ b/src/Attributes/XmlContent.php @@ -12,17 +12,17 @@ class XmlContent extends \OpenApi\Annotations\XmlContent { /** - * @param array $examples - * @param string[] $required - * @param int|float $maximum - * @param int|float $minimum - * @param Property[] $properties - * @param string[]|int[]|float[] $enum - * @param Schema[] $allOf - * @param Schema[] $anyOf - * @param Schema[] $oneOf - * @param array|null $x - * @param Attachable[]|null $attachables + * @param array $examples + * @param string[] $required + * @param int|float $maximum + * @param int|float $minimum + * @param Property[] $properties + * @param string[]|int[]|float[] $enum + * @param array $allOf + * @param array $anyOf + * @param array $oneOf + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( ?array $examples = null, diff --git a/tests/Samples/AnyOf.php b/tests/Samples/AnyOf.php new file mode 100644 index 000000000..116bc371f --- /dev/null +++ b/tests/Samples/AnyOf.php @@ -0,0 +1,38 @@ +