Skip to content

Commit

Permalink
reach PHPStan level 9
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Oct 20, 2023
1 parent 91d4ed9 commit 3ce9d44
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
14 changes: 13 additions & 1 deletion .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ includes:
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
level: 8
level: 9

paths:
- src/
Expand Down Expand Up @@ -53,6 +53,18 @@ parameters:
path: tests/BC/ElementTest.php
# parameter is required by Art4\JsonApiClient\Element

-
message: "#^Parameter \\#1 \\$string of class Art4\\\\JsonApiClient\\\\Input\\\\RequestStringInput constructor expects string, mixed given\\.$#"
count: 1
path: tests/Unit/Input/RequestStringInputTest.php
# We are providing an invalid parameter to test the exception message

-
message: "#^Parameter \\#1 \\$string of class Art4\\\\JsonApiClient\\\\Input\\\\ResponseStringInput constructor expects string, mixed given\\.$#"
count: 1
path: tests/Unit/Input/ResponseStringInputTest.php
# We are providing an invalid parameter to test the exception message

-
message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, array given\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/AccessKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class AccessKey extends SplStack
/**
* Transforms the Key to a string
*
* @param mixed $key
* @param int|string $key
*
* @return AccessKey<string>
*/
Expand Down
3 changes: 1 addition & 2 deletions src/V1/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public function get($key)
/**
* Set a link
*
* @param string $name The Name
* @param string|object $link The Link
* @param mixed $link The Link
*/
private function setAsLink(string $name, $link): void
{
Expand Down
26 changes: 22 additions & 4 deletions tests/Functional/ParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ public static function createParserProvider(): array
}

/**
* @test
* @dataProvider createParserProvider
*
* @param mixed $parser
*/
public function testParseSimpleResourceWithDifferentParser($parser): void
public function testParseSimpleResourceWithDifferentParser(callable $parser): void
{
$string = $this->getJsonString('01_simple_resource.json');
$document = $parser($string);
Expand Down Expand Up @@ -178,6 +175,7 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertTrue($document->has('included'));
$this->assertTrue($document->has('data'));

/** @var Accessable */
$resources = $document->get('data');

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceCollection', $resources);
Expand All @@ -186,6 +184,8 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertSame($resources->getKeys(), [0]);

$this->assertTrue($resources->has(0));

/** @var Accessable */
$resource = $resources->get(0);

$this->assertFalse($resource->has('meta'));
Expand All @@ -195,24 +195,28 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertTrue($resource->has('relationships'));
$this->assertTrue($resource->has('links'));

/** @var Accessable */
$attributes = $resource->get('attributes');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Attributes', $attributes);
$this->assertTrue($attributes->has('title'));
$this->assertSame($attributes->get('title'), 'JSON API paints my bikeshed!');

/** @var Accessable */
$collection = $resource->get('relationships');

$this->assertInstanceOf('Art4\JsonApiClient\V1\RelationshipCollection', $collection);
$this->assertTrue($collection->has('author'));
$this->assertTrue($collection->has('comments'));

/** @var Accessable */
$author = $collection->get('author');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Relationship', $author);
$this->assertTrue($author->has('links'));
$this->assertTrue($author->has('data'));

/** @var Accessable */
$links = $author->get('links');

$this->assertInstanceOf('Art4\JsonApiClient\V1\RelationshipLink', $links);
Expand All @@ -221,18 +225,21 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertTrue($links->has('related'));
$this->assertSame($links->get('related'), 'http://example.com/articles/1/author');

/** @var Accessable */
$data = $author->get('data');

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifier', $data);
$this->assertSame($data->get('type'), 'people');
$this->assertSame($data->get('id'), '9');

/** @var Accessable */
$comments = $collection->get('comments');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Relationship', $comments);
$this->assertTrue($comments->has('links'));
$this->assertTrue($comments->has('data'));

/** @var Accessable */
$links = $comments->get('links');

$this->assertInstanceOf('Art4\JsonApiClient\V1\RelationshipLink', $links);
Expand All @@ -241,35 +248,42 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertTrue($links->has('related'));
$this->assertSame($links->get('related'), 'http://example.com/articles/1/comments');

/** @var Accessable */
$data_array = $comments->get('data');

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifierCollection', $data_array);
$this->assertCount(2, $data_array->getKeys());

/** @var Accessable */
$identifier = $data_array->get(0);

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifier', $identifier);
$this->assertSame($identifier->get('type'), 'comments');
$this->assertSame($identifier->get('id'), '5');

/** @var Accessable */
$identifier = $data_array->get(1);

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifier', $identifier);
$this->assertSame($identifier->get('type'), 'comments');
$this->assertSame($identifier->get('id'), '12');

/** @var Accessable */
$links = $resource->get('links');

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItemLink', $links);
$this->assertTrue($links->has('self'));
$this->assertSame($links->get('self'), 'http://example.com/articles/1');

/** @var Accessable */
$includes = $document->get('included');

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceCollection', $includes);
$this->assertSame($includes->getKeys(), [0, 1, 2]);

$this->assertTrue($includes->has(0));

/** @var Accessable */
$include = $includes->get(0);

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItem', $include);
Expand All @@ -278,6 +292,7 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertTrue($include->has('attributes'));
$this->assertTrue($include->has('links'));

/** @var Accessable */
$attributes = $include->get('attributes');

$this->assertInstanceOf('Art4\JsonApiClient\V1\Attributes', $attributes);
Expand All @@ -288,13 +303,16 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void
$this->assertTrue($attributes->has('twitter'));
$this->assertSame($attributes->get('twitter'), 'dgeb');

/** @var Accessable */
$links = $include->get('links');

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItemLink', $links);
$this->assertTrue($links->has('self'));
$this->assertSame($links->get('self'), 'http://example.com/people/9');

$this->assertTrue($includes->has(1));

/** @var Accessable */
$include = $includes->get(1);

$this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItem', $include);
Expand Down
11 changes: 1 addition & 10 deletions tests/Unit/Input/RequestStringInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class RequestStringInputTest extends TestCase
{
use HelperTrait;

/**
* @test
*/
public function testGetAsObjectFromStringReturnsObject(): void
{
$input = new RequestStringInput('{}');
Expand All @@ -29,11 +26,8 @@ public function testGetAsObjectFromStringReturnsObject(): void

/**
* @dataProvider jsonValuesProviderWithoutString
* @test
*
* @param mixed $input
*/
public function testCreateWithoutStringThrowsException($input): void
public function testCreateWithoutStringThrowsException(mixed $input): void
{
$this->expectException(InputException::class);
$this->expectExceptionMessage(
Expand All @@ -44,9 +38,6 @@ public function testCreateWithoutStringThrowsException($input): void

/**
* @dataProvider jsonValuesAsStringProviderWithoutObject
* @test
*
* @param string $input
*/
public function testGetAsObjectWithInvalidStringsThrowsException(string $input): void
{
Expand Down
11 changes: 1 addition & 10 deletions tests/Unit/Input/ResponseStringInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class ResponseStringInputTest extends TestCase
{
use HelperTrait;

/**
* @test
*/
public function testGetAsObjectFromStringReturnsObject(): void
{
$input = new ResponseStringInput('{}');
Expand All @@ -29,11 +26,8 @@ public function testGetAsObjectFromStringReturnsObject(): void

/**
* @dataProvider jsonValuesProviderWithoutString
* @test
*
* @param mixed $input
*/
public function testCreateWithoutStringThrowsException($input): void
public function testCreateWithoutStringThrowsException(mixed $input): void
{
$this->expectException(InputException::class);
$this->expectExceptionMessage(
Expand All @@ -44,9 +38,6 @@ public function testCreateWithoutStringThrowsException($input): void

/**
* @dataProvider jsonValuesAsStringProviderWithoutObject
* @test
*
* @param string $input
*/
public function testGetAsObjectWithInvalidStringsThrowsException(string $input): void
{
Expand Down

0 comments on commit 3ce9d44

Please sign in to comment.