From 7b1eebfbf428476e1b889517d2ca06eae594678b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Thu, 5 Dec 2019 14:15:32 +0100 Subject: [PATCH 1/5] Require PHPStan --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1ee46f86..de6253ac 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "oai/openapi-specification": "3.0.2", "mermade/openapi3-examples": "1.0.0", "apis-guru/openapi-directory": "1.0.0", - "nexmo/api-specification": "1.0.0" + "nexmo/api-specification": "1.0.0", + "phpstan/phpstan": "^0.12.0" }, "autoload": { "psr-4": { From d1eb928878e305b4a9d5ddc62def4737f037909a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Thu, 5 Dec 2019 14:23:33 +0100 Subject: [PATCH 2/5] Fix PHPStan level 2 errors --- src/ReferenceContext.php | 6 ------ src/Writer.php | 8 ++++---- src/json/JsonReference.php | 3 --- src/spec/PathItem.php | 4 ++-- src/spec/Reference.php | 4 ++-- 5 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/ReferenceContext.php b/src/ReferenceContext.php index c22d770f..7eaab54f 100644 --- a/src/ReferenceContext.php +++ b/src/ReferenceContext.php @@ -57,17 +57,11 @@ private function normalizeUri($uri) throw new UnresolvableReferenceException('Can not resolve references for a specification given as a relative path.'); } - /** - * @return mixed - */ public function getBaseSpec(): ?SpecObjectInterface { return $this->_baseSpec; } - /** - * @return mixed - */ public function getUri(): string { return $this->_uri; diff --git a/src/Writer.php b/src/Writer.php index aecb2132..43d1c70d 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -19,7 +19,7 @@ class Writer { /** * Convert OpenAPI spec object to JSON data. - * @param SpecObjectInterface|OpenApi the OpenApi object instance. + * @param SpecObjectInterface|OpenApi $object the OpenApi object instance. * @return string JSON string. */ public static function writeToJson(SpecObjectInterface $object): string @@ -29,7 +29,7 @@ public static function writeToJson(SpecObjectInterface $object): string /** * Convert OpenAPI spec object to YAML data. - * @param SpecObjectInterface|OpenApi the OpenApi object instance. + * @param SpecObjectInterface|OpenApi $object the OpenApi object instance. * @return string YAML string. */ public static function writeToYaml(SpecObjectInterface $object): string @@ -39,7 +39,7 @@ public static function writeToYaml(SpecObjectInterface $object): string /** * Write OpenAPI spec object to JSON file. - * @param SpecObjectInterface|OpenApi the OpenApi object instance. + * @param SpecObjectInterface|OpenApi $object the OpenApi object instance. * @param string $fileName file name to write to. * @throws IOException when writing the file fails. */ @@ -52,7 +52,7 @@ public static function writeToJsonFile(SpecObjectInterface $object, string $file /** * Write OpenAPI spec object to YAML file. - * @param SpecObjectInterface|OpenApi the OpenApi object instance. + * @param SpecObjectInterface|OpenApi $object the OpenApi object instance. * @param string $fileName file name to write to. * @throws IOException when writing the file fails. */ diff --git a/src/json/JsonReference.php b/src/json/JsonReference.php index 504fa5e2..fe27ced1 100644 --- a/src/json/JsonReference.php +++ b/src/json/JsonReference.php @@ -90,9 +90,6 @@ public function __clone() } - /** - * @return string returns the JSON Pointer. - */ public function getJsonPointer(): JsonPointer { return $this->_pointer; diff --git a/src/spec/PathItem.php b/src/spec/PathItem.php index 8d3f2445..236bda81 100644 --- a/src/spec/PathItem.php +++ b/src/spec/PathItem.php @@ -66,7 +66,7 @@ protected function attributes(): array /** * Create an object from spec data. * @param array $data spec data read from YAML or JSON - * @throws TypeErrorException in case invalid data is supplied. + * @throws \cebe\openapi\exceptions\TypeErrorException in case invalid data is supplied. */ public function __construct(array $data) { @@ -142,7 +142,7 @@ public function setReferenceContext(ReferenceContext $context) /** * Resolves all Reference Objects in this object and replaces them with their resolution. - * @throws exceptions\UnresolvableReferenceException in case resolving a reference fails. + * @throws \cebe\openapi\exceptions\UnresolvableReferenceException in case resolving a reference fails. */ public function resolveReferences(ReferenceContext $context = null) { diff --git a/src/spec/Reference.php b/src/spec/Reference.php index 1610162d..d99ac99c 100644 --- a/src/spec/Reference.php +++ b/src/spec/Reference.php @@ -172,7 +172,7 @@ public function resolve(ReferenceContext $context = null) $baseSpec = $context->getBaseSpec(); if ($baseSpec !== null) { // TODO type error if resolved object does not match $this->_to ? - /** @var $referencedObject SpecObjectInterface */ + /** @var SpecObjectInterface $referencedObject */ $referencedObject = $jsonReference->getJsonPointer()->evaluate($baseSpec); if ($referencedObject instanceof SpecObjectInterface) { $referencedObject->setReferenceContext($context); @@ -199,7 +199,7 @@ public function resolve(ReferenceContext $context = null) if (isset($referencedData['$ref'])) { return (new Reference($referencedData, $this->_to))->resolve(new ReferenceContext(null, $file)); } - /** @var $referencedObject SpecObjectInterface|array */ + /** @var SpecObjectInterface|array $referencedObject */ $referencedObject = $this->_to !== null ? new $this->_to($referencedData) : $referencedData; if ($jsonReference->getJsonPointer()->getPointer() === '') { From f585394e9064881ff5aed6d4bc7f4ad20e614af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Thu, 5 Dec 2019 15:18:25 +0100 Subject: [PATCH 3/5] Fix PHPStan level 5 errors --- src/spec/Paths.php | 4 ++-- src/spec/Reference.php | 2 +- src/spec/Responses.php | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/spec/Paths.php b/src/spec/Paths.php index ae364954..e87606a0 100644 --- a/src/spec/Paths.php +++ b/src/spec/Paths.php @@ -31,7 +31,7 @@ class Paths implements SpecObjectInterface, DocumentContextInterface, ArrayAccess, Countable, IteratorAggregate { /** - * @var PathItem[] + * @var (PathItem|null)[] */ private $_paths = []; @@ -43,7 +43,7 @@ class Paths implements SpecObjectInterface, DocumentContextInterface, ArrayAcces /** * Create an object from spec data. - * @param PathItem[]|array[] $data spec data read from YAML or JSON + * @param (PathItem|array|null)[] $data spec data read from YAML or JSON * @throws TypeErrorException in case invalid data is supplied. */ public function __construct(array $data) diff --git a/src/spec/Reference.php b/src/spec/Reference.php index d99ac99c..134c7b28 100644 --- a/src/spec/Reference.php +++ b/src/spec/Reference.php @@ -145,7 +145,7 @@ public function getContext() : ?ReferenceContext * @param ReferenceContext $context the reference context to use for resolution. * If not specified, `getContext()` will be called to determine the context, if * that does not return a context, the UnresolvableReferenceException will be thrown. - * @return SpecObjectInterface the resolved spec type. + * @return SpecObjectInterface|null the resolved spec type. * You might want to call resolveReferences() on the resolved object to recursively resolve recursive references. * This is not done automatically to avoid recursion to run into the same function again. * If you call resolveReferences() make sure to replace the Reference with the resolved object first. diff --git a/src/spec/Responses.php b/src/spec/Responses.php index d45f48e9..52476031 100644 --- a/src/spec/Responses.php +++ b/src/spec/Responses.php @@ -27,7 +27,7 @@ class Responses implements SpecObjectInterface, DocumentContextInterface, ArrayAccess, Countable, IteratorAggregate { /** - * @var Response[]|Reference[] + * @var (Response|Reference|null)[] */ private $_responses = []; private $_errors = []; @@ -90,7 +90,7 @@ public function hasResponse($statusCode): bool /** * @param string $statusCode HTTP status code - * @return Response|Reference + * @return Response|Reference|null */ public function getResponse($statusCode) { @@ -115,7 +115,7 @@ public function removeResponse($statusCode) } /** - * @return Response[]|Reference[] + * @return (Response|Reference|null)[] */ public function getResponses(): array { @@ -239,6 +239,7 @@ public function resolveReferences(ReferenceContext $context = null) { foreach ($this->_responses as $key => $response) { if ($response instanceof Reference) { + /** @var Response|Reference|null $referencedObject */ $referencedObject = $response->resolve($context); $this->_responses[$key] = $referencedObject; if (!$referencedObject instanceof Reference && $referencedObject !== null) { From 446a816989d543f69baecf2984461d2ccbff34d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Thu, 5 Dec 2019 15:27:41 +0100 Subject: [PATCH 4/5] Run static analysis on Travis --- .travis.yml | 1 + Makefile | 3 +++ 2 files changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index c80f7dda..8ef4733f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,6 +54,7 @@ install: - composer require symfony/yaml:"${YAML}" --prefer-dist --no-interaction script: - make lint + - make stan - make test - if [[ $TRAVIS_PHP_VERSION = "7.3" || $TRAVIS_PHP_VERSION = "nightly" ]]; then true; else make check-style; fi - make coverage diff --git a/Makefile b/Makefile index c7e1d77c..0b75f0a6 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,9 @@ lint: node_modules/.bin/speccy lint tests/spec/data/reference/playlist.json node_modules/.bin/speccy lint tests/spec/data/recursion.json +stan: + php $(PHPARGS) vendor/bin/phpstan analyse -l 5 src + # copy openapi3 json schema schemas/openapi-v3.0.json: vendor/oai/openapi-specification/schemas/v3.0/schema.json cp $< $@ From 3e793e241e832fa621ef7c144edbe7cb4b205361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Wed, 11 Dec 2019 15:36:01 +0100 Subject: [PATCH 5/5] Update src/spec/Reference.php Co-Authored-By: Carsten Brandt --- src/spec/Reference.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spec/Reference.php b/src/spec/Reference.php index 134c7b28..f68a5b64 100644 --- a/src/spec/Reference.php +++ b/src/spec/Reference.php @@ -145,7 +145,7 @@ public function getContext() : ?ReferenceContext * @param ReferenceContext $context the reference context to use for resolution. * If not specified, `getContext()` will be called to determine the context, if * that does not return a context, the UnresolvableReferenceException will be thrown. - * @return SpecObjectInterface|null the resolved spec type. + * @return SpecObjectInterface|array|null the resolved spec type. * You might want to call resolveReferences() on the resolved object to recursively resolve recursive references. * This is not done automatically to avoid recursion to run into the same function again. * If you call resolveReferences() make sure to replace the Reference with the resolved object first.