Skip to content

Commit 053bcee

Browse files
lookymancebe
andcommitted
Static analysis with PHPStan (#49)
* Require PHPStan * Fix PHPStan level 2 errors * Fix PHPStan level 5 errors * Run static analysis on Travis * Update src/spec/Reference.php Co-Authored-By: Carsten Brandt <[email protected]>
1 parent 7c1e7ef commit 053bcee

10 files changed

+21
-24
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ install:
5454
- composer require symfony/yaml:"${YAML}" --prefer-dist --no-interaction
5555
script:
5656
- make lint
57+
- make stan
5758
- make test
5859
- if [[ $TRAVIS_PHP_VERSION = "7.3" || $TRAVIS_PHP_VERSION = "nightly" ]]; then true; else make check-style; fi
5960
- make coverage

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ lint:
2525
node_modules/.bin/speccy lint tests/spec/data/reference/playlist.json
2626
node_modules/.bin/speccy lint tests/spec/data/recursion.json
2727

28+
stan:
29+
php $(PHPARGS) vendor/bin/phpstan analyse -l 5 src
30+
2831
# copy openapi3 json schema
2932
schemas/openapi-v3.0.json: vendor/oai/openapi-specification/schemas/v3.0/schema.json
3033
cp $< $@

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"oai/openapi-specification": "3.0.2",
3232
"mermade/openapi3-examples": "1.0.0",
3333
"apis-guru/openapi-directory": "1.0.0",
34-
"nexmo/api-specification": "1.0.0"
34+
"nexmo/api-specification": "1.0.0",
35+
"phpstan/phpstan": "^0.12.0"
3536
},
3637
"autoload": {
3738
"psr-4": {

src/ReferenceContext.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,11 @@ private function normalizeUri($uri)
5757
throw new UnresolvableReferenceException('Can not resolve references for a specification given as a relative path.');
5858
}
5959

60-
/**
61-
* @return mixed
62-
*/
6360
public function getBaseSpec(): ?SpecObjectInterface
6461
{
6562
return $this->_baseSpec;
6663
}
6764

68-
/**
69-
* @return mixed
70-
*/
7165
public function getUri(): string
7266
{
7367
return $this->_uri;

src/Writer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Writer
1919
{
2020
/**
2121
* Convert OpenAPI spec object to JSON data.
22-
* @param SpecObjectInterface|OpenApi the OpenApi object instance.
22+
* @param SpecObjectInterface|OpenApi $object the OpenApi object instance.
2323
* @return string JSON string.
2424
*/
2525
public static function writeToJson(SpecObjectInterface $object): string
@@ -29,7 +29,7 @@ public static function writeToJson(SpecObjectInterface $object): string
2929

3030
/**
3131
* Convert OpenAPI spec object to YAML data.
32-
* @param SpecObjectInterface|OpenApi the OpenApi object instance.
32+
* @param SpecObjectInterface|OpenApi $object the OpenApi object instance.
3333
* @return string YAML string.
3434
*/
3535
public static function writeToYaml(SpecObjectInterface $object): string
@@ -39,7 +39,7 @@ public static function writeToYaml(SpecObjectInterface $object): string
3939

4040
/**
4141
* Write OpenAPI spec object to JSON file.
42-
* @param SpecObjectInterface|OpenApi the OpenApi object instance.
42+
* @param SpecObjectInterface|OpenApi $object the OpenApi object instance.
4343
* @param string $fileName file name to write to.
4444
* @throws IOException when writing the file fails.
4545
*/
@@ -52,7 +52,7 @@ public static function writeToJsonFile(SpecObjectInterface $object, string $file
5252

5353
/**
5454
* Write OpenAPI spec object to YAML file.
55-
* @param SpecObjectInterface|OpenApi the OpenApi object instance.
55+
* @param SpecObjectInterface|OpenApi $object the OpenApi object instance.
5656
* @param string $fileName file name to write to.
5757
* @throws IOException when writing the file fails.
5858
*/

src/json/JsonReference.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ public function __clone()
9090
}
9191

9292

93-
/**
94-
* @return string returns the JSON Pointer.
95-
*/
9693
public function getJsonPointer(): JsonPointer
9794
{
9895
return $this->_pointer;

src/spec/PathItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function attributes(): array
6666
/**
6767
* Create an object from spec data.
6868
* @param array $data spec data read from YAML or JSON
69-
* @throws TypeErrorException in case invalid data is supplied.
69+
* @throws \cebe\openapi\exceptions\TypeErrorException in case invalid data is supplied.
7070
*/
7171
public function __construct(array $data)
7272
{
@@ -142,7 +142,7 @@ public function setReferenceContext(ReferenceContext $context)
142142

143143
/**
144144
* Resolves all Reference Objects in this object and replaces them with their resolution.
145-
* @throws exceptions\UnresolvableReferenceException in case resolving a reference fails.
145+
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException in case resolving a reference fails.
146146
*/
147147
public function resolveReferences(ReferenceContext $context = null)
148148
{

src/spec/Paths.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class Paths implements SpecObjectInterface, DocumentContextInterface, ArrayAccess, Countable, IteratorAggregate
3232
{
3333
/**
34-
* @var PathItem[]
34+
* @var (PathItem|null)[]
3535
*/
3636
private $_paths = [];
3737

@@ -43,7 +43,7 @@ class Paths implements SpecObjectInterface, DocumentContextInterface, ArrayAcces
4343

4444
/**
4545
* Create an object from spec data.
46-
* @param PathItem[]|array[] $data spec data read from YAML or JSON
46+
* @param (PathItem|array|null)[] $data spec data read from YAML or JSON
4747
* @throws TypeErrorException in case invalid data is supplied.
4848
*/
4949
public function __construct(array $data)

src/spec/Reference.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function getContext() : ?ReferenceContext
145145
* @param ReferenceContext $context the reference context to use for resolution.
146146
* If not specified, `getContext()` will be called to determine the context, if
147147
* that does not return a context, the UnresolvableReferenceException will be thrown.
148-
* @return SpecObjectInterface the resolved spec type.
148+
* @return SpecObjectInterface|array|null the resolved spec type.
149149
* You might want to call resolveReferences() on the resolved object to recursively resolve recursive references.
150150
* This is not done automatically to avoid recursion to run into the same function again.
151151
* If you call resolveReferences() make sure to replace the Reference with the resolved object first.
@@ -172,7 +172,7 @@ public function resolve(ReferenceContext $context = null)
172172
$baseSpec = $context->getBaseSpec();
173173
if ($baseSpec !== null) {
174174
// TODO type error if resolved object does not match $this->_to ?
175-
/** @var $referencedObject SpecObjectInterface */
175+
/** @var SpecObjectInterface $referencedObject */
176176
$referencedObject = $jsonReference->getJsonPointer()->evaluate($baseSpec);
177177
if ($referencedObject instanceof SpecObjectInterface) {
178178
$referencedObject->setReferenceContext($context);
@@ -199,7 +199,7 @@ public function resolve(ReferenceContext $context = null)
199199
if (isset($referencedData['$ref'])) {
200200
return (new Reference($referencedData, $this->_to))->resolve(new ReferenceContext(null, $file));
201201
}
202-
/** @var $referencedObject SpecObjectInterface|array */
202+
/** @var SpecObjectInterface|array $referencedObject */
203203
$referencedObject = $this->_to !== null ? new $this->_to($referencedData) : $referencedData;
204204

205205
if ($jsonReference->getJsonPointer()->getPointer() === '') {

src/spec/Responses.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class Responses implements SpecObjectInterface, DocumentContextInterface, ArrayAccess, Countable, IteratorAggregate
2828
{
2929
/**
30-
* @var Response[]|Reference[]
30+
* @var (Response|Reference|null)[]
3131
*/
3232
private $_responses = [];
3333
private $_errors = [];
@@ -90,7 +90,7 @@ public function hasResponse($statusCode): bool
9090

9191
/**
9292
* @param string $statusCode HTTP status code
93-
* @return Response|Reference
93+
* @return Response|Reference|null
9494
*/
9595
public function getResponse($statusCode)
9696
{
@@ -115,7 +115,7 @@ public function removeResponse($statusCode)
115115
}
116116

117117
/**
118-
* @return Response[]|Reference[]
118+
* @return (Response|Reference|null)[]
119119
*/
120120
public function getResponses(): array
121121
{
@@ -239,6 +239,7 @@ public function resolveReferences(ReferenceContext $context = null)
239239
{
240240
foreach ($this->_responses as $key => $response) {
241241
if ($response instanceof Reference) {
242+
/** @var Response|Reference|null $referencedObject */
242243
$referencedObject = $response->resolve($context);
243244
$this->_responses[$key] = $referencedObject;
244245
if (!$referencedObject instanceof Reference && $referencedObject !== null) {

0 commit comments

Comments
 (0)