From 1f0af9558934eda9cc5ff4dfa0a5327e83734702 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 17 Oct 2023 15:39:20 +0200 Subject: [PATCH 01/11] Check tests with PHPStan --- .phpstan.neon | 4 ++++ composer.json | 1 + tests/Fixtures/Factory.php | 2 +- tests/Fixtures/HelperTrait.php | 20 ++++++++++++-------- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/.phpstan.neon b/.phpstan.neon index 8136274..c8c69be 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -1,11 +1,15 @@ # SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt # SPDX-License-Identifier: GPL-3.0-or-later +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon + parameters: level: 8 paths: - src/ + - tests/ scanDirectories: - vendor diff --git a/composer.json b/composer.json index 82738e1..48f9de4 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.35", "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", "phpunit/phpunit": "^9 || ^10" }, "autoload": { diff --git a/tests/Fixtures/Factory.php b/tests/Fixtures/Factory.php index b6d916c..a37e838 100644 --- a/tests/Fixtures/Factory.php +++ b/tests/Fixtures/Factory.php @@ -8,7 +8,7 @@ namespace Art4\JsonApiClient\Tests\Fixtures; -use Art4\JsonApiClient\Utils\FactoryInterface; +use Art4\JsonApiClient\Factory as FactoryInterface; final class Factory implements FactoryInterface { diff --git a/tests/Fixtures/HelperTrait.php b/tests/Fixtures/HelperTrait.php index 94cc0d1..aefbb70 100644 --- a/tests/Fixtures/HelperTrait.php +++ b/tests/Fixtures/HelperTrait.php @@ -12,22 +12,26 @@ use Art4\JsonApiClient\Factory; use Art4\JsonApiClient\Manager; use Art4\JsonApiClient\Tests\Fixtures\Factory as FixtureFactory; +use PHPUnit\Framework\MockObject\MockObject; /** * Helper Trait */ trait HelperTrait { + /** @var Manager&MockObject */ protected Manager $manager; + /** @var Factory&MockObject */ protected Factory $factory; + /** @var Accessable&MockObject */ protected Accessable $parent; /** * Json Values Provider * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProvider(): array { @@ -47,7 +51,7 @@ public static function jsonValuesProvider(): array /** * Json Values Provider but without the object * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutObject(): array { @@ -61,7 +65,7 @@ public static function jsonValuesProviderWithoutObject(): array /** * Json Values Provider but without the array * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutArray(): array { @@ -75,7 +79,7 @@ public static function jsonValuesProviderWithoutArray(): array /** * Json Values Provider but without the string * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutString(): array { @@ -89,7 +93,7 @@ public static function jsonValuesProviderWithoutString(): array /** * Json Values Provider but without the object and string * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutObjectAndString(): array { @@ -104,7 +108,7 @@ public static function jsonValuesProviderWithoutObjectAndString(): array /** * Json Values as string Provider * - * @see http://json.org/ + * @return array> */ public static function jsonValuesAsStringProvider(): array { @@ -124,7 +128,7 @@ public static function jsonValuesAsStringProvider(): array /** * Json Values as string Provider but without the object * - * @see http://json.org/ + * @return array> */ public static function jsonValuesAsStringProviderWithoutObject(): array { @@ -145,7 +149,7 @@ public function buildManagerMock() $factory->testcase = $this; // Mock Manager - $manager = $this->createMock('Art4\JsonApiClient\Utils\FactoryManagerInterface'); + $manager = $this->createMock(Manager::class); $manager->expects($this->any()) ->method('getFactory') From 01ccd18f0458284d2583dff466e587266ee2092d Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 18 Oct 2023 16:15:58 +0200 Subject: [PATCH 02/11] remove unused code in tests --- tests/Fixtures/Factory.php | 35 ---------------------------------- tests/Fixtures/HelperTrait.php | 25 ------------------------ 2 files changed, 60 deletions(-) delete mode 100644 tests/Fixtures/Factory.php diff --git a/tests/Fixtures/Factory.php b/tests/Fixtures/Factory.php deleted file mode 100644 index a37e838..0000000 --- a/tests/Fixtures/Factory.php +++ /dev/null @@ -1,35 +0,0 @@ -testcase - ->getMockBuilder('Art4\JsonApiClient\\' . $name . 'Interface') // Mock only the interfaces - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->getMock(); - } -} diff --git a/tests/Fixtures/HelperTrait.php b/tests/Fixtures/HelperTrait.php index aefbb70..2949bad 100644 --- a/tests/Fixtures/HelperTrait.php +++ b/tests/Fixtures/HelperTrait.php @@ -11,7 +11,6 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Factory; use Art4\JsonApiClient\Manager; -use Art4\JsonApiClient\Tests\Fixtures\Factory as FixtureFactory; use PHPUnit\Framework\MockObject\MockObject; /** @@ -139,30 +138,6 @@ public static function jsonValuesAsStringProviderWithoutObject(): array return array_values($data); } - /** - * Builds a Manager Mock - */ - public function buildManagerMock() - { - // Mock factory - $factory = new FixtureFactory(); - $factory->testcase = $this; - - // Mock Manager - $manager = $this->createMock(Manager::class); - - $manager->expects($this->any()) - ->method('getFactory') - ->will($this->returnValue($factory)); - - $manager->expects($this->any()) - ->method('getConfig') - ->with('optional_item_id') - ->willReturn(false); - - return $manager; - } - /** * Builds a Manager Mock and set it into the TestCase */ From a46ee529a414b03fc9aa8e31bcca59c984807526 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 18 Oct 2023 17:10:06 +0200 Subject: [PATCH 03/11] Prezise the type of keys --- src/Accessable.php | 8 +++++--- src/Helper/AccessableTrait.php | 8 ++++---- src/Serializer/ArraySerializer.php | 2 +- src/V1/Attributes.php | 6 ++++-- src/V1/Document.php | 5 +++-- src/V1/DocumentLink.php | 5 +++-- src/V1/Error.php | 5 +++-- src/V1/ErrorCollection.php | 5 +++-- src/V1/ErrorLink.php | 5 +++-- src/V1/ErrorSource.php | 5 +++-- src/V1/Jsonapi.php | 5 +++-- src/V1/Link.php | 5 +++-- src/V1/Meta.php | 5 +++-- src/V1/Relationship.php | 5 +++-- src/V1/RelationshipCollection.php | 5 +++-- src/V1/RelationshipLink.php | 3 ++- src/V1/ResourceCollection.php | 5 +++-- src/V1/ResourceIdentifier.php | 5 +++-- src/V1/ResourceIdentifierCollection.php | 5 +++-- src/V1/ResourceItem.php | 5 +++-- src/V1/ResourceItemLink.php | 5 +++-- src/V1/ResourceNull.php | 5 +++-- tests/Fixtures/HelperTrait.php | 4 ++-- tests/Fixtures/V1Factory.php | 15 +++++---------- tests/Functional/ParsingTest.php | 2 ++ tests/Functional/SerializerTest.php | 22 +++++++++++++--------- 26 files changed, 89 insertions(+), 66 deletions(-) diff --git a/src/Accessable.php b/src/Accessable.php index ec46ab1..069da12 100644 --- a/src/Accessable.php +++ b/src/Accessable.php @@ -8,6 +8,8 @@ namespace Art4\JsonApiClient; +use Art4\JsonApiClient\Helper\AccessKey; + /** * Accessable Interface */ @@ -16,7 +18,7 @@ interface Accessable /** * Get a value by a key * - * @param mixed $key The key + * @param int|string|AccessKey $key The key * * @return mixed */ @@ -27,7 +29,7 @@ public function get($key); * * @deprecated `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * - * @param mixed $key The key + * @param int|string|AccessKey $key The key * * @return bool */ @@ -39,7 +41,7 @@ public function has($key); * * @deprecated `\Art4\JsonApiClient\Accessable::getKeys()` will add `array` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * - * @return array Keys of all setted values + * @return array Keys of all setted values */ public function getKeys(); // public function getKeys(): array; diff --git a/src/Helper/AccessableTrait.php b/src/Helper/AccessableTrait.php index 81eb8bf..d9d690a 100644 --- a/src/Helper/AccessableTrait.php +++ b/src/Helper/AccessableTrait.php @@ -41,7 +41,7 @@ final protected function set(string $key, $value): void /** * Returns the keys of all setted values * - * @return array Keys of all setted values + * @return array Keys of all setted values */ final public function getKeys(): array { @@ -51,7 +51,7 @@ final public function getKeys(): array /** * Check if a value exists * - * @param mixed $key The key + * @param int|string|AccessKey $key The key */ final public function has($key): bool { @@ -82,7 +82,7 @@ final public function has($key): bool /** * Get a value by a key * - * @param mixed $key The key + * @param int|string|AccessKey $key The key * * @return mixed */ @@ -126,7 +126,7 @@ private function getValue(string $key) /** * Parse a dot.notated.key to an object * - * @param string|AccessKey $key The key + * @param int|string|AccessKey $key The key * * @return AccessKey The parsed key */ diff --git a/src/Serializer/ArraySerializer.php b/src/Serializer/ArraySerializer.php index 42d1491..422037f 100644 --- a/src/Serializer/ArraySerializer.php +++ b/src/Serializer/ArraySerializer.php @@ -35,7 +35,7 @@ public function __construct(array $params = []) /** * Convert data in an array * - * @return array|null + * @return array|null */ public function serialize(Accessable $data): ?array { diff --git a/src/V1/Attributes.php b/src/V1/Attributes.php index 918dcf3..9306362 100644 --- a/src/V1/Attributes.php +++ b/src/V1/Attributes.php @@ -8,9 +8,11 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; + /** * Attributes Object @@ -50,7 +52,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Document.php b/src/V1/Document.php index ff3194d..4e971cb 100644 --- a/src/V1/Document.php +++ b/src/V1/Document.php @@ -9,9 +9,10 @@ namespace Art4\JsonApiClient\V1; use Art4\JsonApiClient\Accessable; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Document Top Level Object @@ -73,7 +74,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/DocumentLink.php b/src/V1/DocumentLink.php index 30ab3c3..4d0dc0b 100644 --- a/src/V1/DocumentLink.php +++ b/src/V1/DocumentLink.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Document Link Object @@ -103,7 +104,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Error.php b/src/V1/Error.php index 69c2f7b..346cd55 100644 --- a/src/V1/Error.php +++ b/src/V1/Error.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Object @@ -105,7 +106,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ErrorCollection.php b/src/V1/ErrorCollection.php index 42def2f..b61e0f2 100644 --- a/src/V1/ErrorCollection.php +++ b/src/V1/ErrorCollection.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Collection Object @@ -44,7 +45,7 @@ protected function parse($object): void /** * Get a value by the key of this document * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ErrorLink.php b/src/V1/ErrorLink.php index 112d09b..6c08772 100644 --- a/src/V1/ErrorLink.php +++ b/src/V1/ErrorLink.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Link Object @@ -63,7 +64,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ErrorSource.php b/src/V1/ErrorSource.php index 3c70b99..e8136e2 100644 --- a/src/V1/ErrorSource.php +++ b/src/V1/ErrorSource.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Source Object @@ -52,7 +53,7 @@ protected function parse($object): void /** * Get a value by the key of this document * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Jsonapi.php b/src/V1/Jsonapi.php index db56bef..6a5acbb 100644 --- a/src/V1/Jsonapi.php +++ b/src/V1/Jsonapi.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * JSON API Object @@ -48,7 +49,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Link.php b/src/V1/Link.php index 1c07bae..6489924 100644 --- a/src/V1/Link.php +++ b/src/V1/Link.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Link Object @@ -44,7 +45,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Meta.php b/src/V1/Meta.php index 5836bab..db1dcc1 100644 --- a/src/V1/Meta.php +++ b/src/V1/Meta.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Meta Object @@ -46,7 +47,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Relationship.php b/src/V1/Relationship.php index 5a842cf..ec6b0a4 100644 --- a/src/V1/Relationship.php +++ b/src/V1/Relationship.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Identifier Object @@ -59,7 +60,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/RelationshipCollection.php b/src/V1/RelationshipCollection.php index 49680f0..fd9c9dc 100644 --- a/src/V1/RelationshipCollection.php +++ b/src/V1/RelationshipCollection.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Relationship Collection Object @@ -54,7 +55,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/RelationshipLink.php b/src/V1/RelationshipLink.php index 84b1c59..3cb303f 100644 --- a/src/V1/RelationshipLink.php +++ b/src/V1/RelationshipLink.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Relationship Link Object @@ -105,7 +106,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceCollection.php b/src/V1/ResourceCollection.php index 4577e2b..2b515e0 100644 --- a/src/V1/ResourceCollection.php +++ b/src/V1/ResourceCollection.php @@ -9,9 +9,10 @@ namespace Art4\JsonApiClient\V1; use Art4\JsonApiClient\Accessable; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Object @@ -47,7 +48,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceIdentifier.php b/src/V1/ResourceIdentifier.php index 26e6b2c..df39116 100644 --- a/src/V1/ResourceIdentifier.php +++ b/src/V1/ResourceIdentifier.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Identifier Object @@ -59,7 +60,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceIdentifierCollection.php b/src/V1/ResourceIdentifierCollection.php index a887070..e820d37 100644 --- a/src/V1/ResourceIdentifierCollection.php +++ b/src/V1/ResourceIdentifierCollection.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Object @@ -42,7 +43,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceItem.php b/src/V1/ResourceItem.php index caae85a..35c43b0 100644 --- a/src/V1/ResourceItem.php +++ b/src/V1/ResourceItem.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Identifier Object @@ -77,7 +78,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceItemLink.php b/src/V1/ResourceItemLink.php index 69ce850..5315593 100644 --- a/src/V1/ResourceItemLink.php +++ b/src/V1/ResourceItemLink.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * ItemLink Object @@ -40,7 +41,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index c7df660..7876367 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -12,6 +12,7 @@ use Art4\JsonApiClient\Element; use Art4\JsonApiClient\Manager; use Art4\JsonApiClient\Exception\AccessException; +use Art4\JsonApiClient\Helper\AccessKey; /** * Null Resource @@ -40,7 +41,7 @@ public function __construct($data, Manager $manager, Accessable $parent) /** * Check if a value exists in this resource * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return bool false */ @@ -62,7 +63,7 @@ public function getKeys() /** * Get a value by the key of this identifier * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value */ public function get($key): void { diff --git a/tests/Fixtures/HelperTrait.php b/tests/Fixtures/HelperTrait.php index 2949bad..aa0b604 100644 --- a/tests/Fixtures/HelperTrait.php +++ b/tests/Fixtures/HelperTrait.php @@ -164,8 +164,8 @@ public function setUpManagerMock(): void * * @param mixed $filename */ - protected function getJsonString($filename) + protected function getJsonString($filename): string { - return file_get_contents(__DIR__ . '/../files/' . $filename); + return strval(file_get_contents(__DIR__ . '/../files/' . $filename)); } } diff --git a/tests/Fixtures/V1Factory.php b/tests/Fixtures/V1Factory.php index 282ea05..835c047 100644 --- a/tests/Fixtures/V1Factory.php +++ b/tests/Fixtures/V1Factory.php @@ -8,31 +8,26 @@ namespace Art4\JsonApiClient\Tests\Fixtures; -use Art4\JsonApiClient\Element; use Art4\JsonApiClient\Factory as FactoryInterface; +use PHPUnit\Framework\TestCase; final class V1Factory implements FactoryInterface { - public $testcase; + public TestCase $testcase; /** * Create a factory - * - * @param object $testcase - * @param array $args - * - * @return object */ - public function __construct($testcase) + public function __construct(TestCase $testcase) { - return $this->testcase = $testcase; + $this->testcase = $testcase; } /** * Create a new instance of a class * * @param string $name - * @param array $args + * @param array $args * * @return object */ diff --git a/tests/Functional/ParsingTest.php b/tests/Functional/ParsingTest.php index 40565e1..18a67f1 100644 --- a/tests/Functional/ParsingTest.php +++ b/tests/Functional/ParsingTest.php @@ -22,6 +22,8 @@ class ParsingTest extends TestCase /** * Provide Parser + * + * @return array> */ public static function createParserProvider(): array { diff --git a/tests/Functional/SerializerTest.php b/tests/Functional/SerializerTest.php index f96c043..c835969 100644 --- a/tests/Functional/SerializerTest.php +++ b/tests/Functional/SerializerTest.php @@ -14,6 +14,7 @@ use Art4\JsonApiClient\Serializer\ArraySerializer; use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use Art4\JsonApiClient\V1\Factory; +use Exception; use PHPUnit\Framework\TestCase; class SerializerTest extends TestCase @@ -22,6 +23,8 @@ class SerializerTest extends TestCase /** * Provide JSON API data + * + * @return array> */ public static function jsonapiDataProvider(): array { @@ -33,7 +36,13 @@ public static function jsonapiDataProvider(): array '15_create_resource_without_id.json', ]; - foreach (glob($path . '*.json') as $file) { + $filenames = glob($path . '*.json'); + + if ($filenames === false) { + throw new Exception('Error while getting all json files.'); + } + + foreach ($filenames as $file) { $filename = str_replace($path, '', $file); // Ignore files with errors @@ -45,9 +54,7 @@ public static function jsonapiDataProvider(): array $files[] = [ $filename, - [ - 'is_request' => in_array($filename, $requestFiles), - ], + in_array($filename, $requestFiles), ]; } @@ -55,18 +62,15 @@ public static function jsonapiDataProvider(): array } /** - * @test * @dataProvider jsonapiDataProvider - * - * @param mixed $filename */ - public function parseJsonapiDataWithErrorAbortManager($filename, array $meta): void + public function testParseJsonapiDataWithErrorAbortManager(string $filename, bool $isRequest): void { $manager = new ErrorAbortManager(new Factory()); $string = $this->getJsonString($filename); - if ($meta['is_request']) { + if ($isRequest) { $input = new RequestStringInput($string); } else { $input = new ResponseStringInput($string); From 1f165a416ebd51eeeab7ae470448efea1ce3f4ba Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Oct 2023 11:15:56 +0200 Subject: [PATCH 04/11] Ignore unused parameters in constructor --- .phpstan.neon | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.phpstan.neon b/.phpstan.neon index c8c69be..8b7e85a 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -34,3 +34,18 @@ parameters: message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$parent is never read, only written\\.$#" count: 1 path: src/V1/ResourceNull.php + + - + message: "#^Constructor of an anonymous class has an unused parameter \\$data\\.$#" + count: 1 + path: tests/BC/ElementTest.php + + - + message: "#^Constructor of an anonymous class has an unused parameter \\$manager\\.$#" + count: 1 + path: tests/BC/ElementTest.php + + - + message: "#^Constructor of an anonymous class has an unused parameter \\$parent\\.$#" + count: 1 + path: tests/BC/ElementTest.php From a355d59344d4a975124a47a36dc0d43e7e5df639 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Oct 2023 11:41:50 +0200 Subject: [PATCH 05/11] Fix missing type error in AccessKey --- .phpstan.neon | 5 ----- src/Helper/AccessKey.php | 2 ++ 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.phpstan.neon b/.phpstan.neon index 8b7e85a..e4fa0be 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -15,11 +15,6 @@ parameters: - vendor ignoreErrors: - - - message: "#^Class Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey extends generic class SplStack but does not specify its types\\: TValue$#" - count: 1 - path: src/Helper/AccessKey.php - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$data is never read, only written\\.$#" count: 1 diff --git a/src/Helper/AccessKey.php b/src/Helper/AccessKey.php index a57558f..b647d9a 100644 --- a/src/Helper/AccessKey.php +++ b/src/Helper/AccessKey.php @@ -13,6 +13,8 @@ /** * AccessKey * + * @extends SplStack + * * @internal */ final class AccessKey extends SplStack From c92114909f2858366513e7bcbabffe46ae472de4 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Oct 2023 11:52:55 +0200 Subject: [PATCH 06/11] Fix code style --- src/V1/Attributes.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/V1/Attributes.php b/src/V1/Attributes.php index 9306362..2f4a660 100644 --- a/src/V1/Attributes.php +++ b/src/V1/Attributes.php @@ -13,7 +13,6 @@ use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Helper\AccessKey; - /** * Attributes Object * From d3e2710cf309e23ef240b7cf2b798725d2ac5b49 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Oct 2023 16:44:30 +0200 Subject: [PATCH 07/11] Using Accessable::has() with object or array is deprecated --- src/Accessable.php | 4 +- src/Helper/AccessableTrait.php | 9 ++++ src/V1/ResourceNull.php | 9 ++++ tests/Unit/Helper/AccessableTraitTest.php | 57 +++++++++++++++++++++++ tests/Unit/V1/ErrorCollectionTest.php | 5 +- tests/Unit/V1/ResourceCollectionTest.php | 6 +-- tests/Unit/V1/ResourceNullTest.php | 50 ++++++++++++++++++++ 7 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 tests/Unit/Helper/AccessableTraitTest.php diff --git a/src/Accessable.php b/src/Accessable.php index 069da12..cf63145 100644 --- a/src/Accessable.php +++ b/src/Accessable.php @@ -18,7 +18,7 @@ interface Accessable /** * Get a value by a key * - * @param int|string|AccessKey $key The key + * @param mixed $key The key * * @return mixed */ @@ -29,7 +29,7 @@ public function get($key); * * @deprecated `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * - * @param int|string|AccessKey $key The key + * @param mixed $key The key * * @return bool */ diff --git a/src/Helper/AccessableTrait.php b/src/Helper/AccessableTrait.php index d9d690a..6cee963 100644 --- a/src/Helper/AccessableTrait.php +++ b/src/Helper/AccessableTrait.php @@ -55,6 +55,15 @@ final public function getKeys(): array */ final public function has($key): bool { + if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { + trigger_error(sprintf( + '%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.', + get_class($this), + gettype($key), + AccessKey::class + ), \E_USER_DEPRECATED); + } + $key = $this->parseKey($key); $string = $key->shift(); diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index 7876367..3065002 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -47,6 +47,15 @@ public function __construct($data, Manager $manager, Accessable $parent) */ public function has($key) { + if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { + trigger_error(sprintf( + '%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.', + get_class($this), + gettype($key), + AccessKey::class + ), \E_USER_DEPRECATED); + } + return false; } diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php new file mode 100644 index 0000000..af512b3 --- /dev/null +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -0,0 +1,57 @@ +getMockForTrait(AccessableTrait::class); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertStringEndsWith( + '::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has(new \stdClass()); + } + + public function testHasWithArrayAsKeyTriggersException(): void + { + $resource = $this->getMockForTrait(AccessableTrait::class); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertStringEndsWith( + '::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has([]); + } +} diff --git a/tests/Unit/V1/ErrorCollectionTest.php b/tests/Unit/V1/ErrorCollectionTest.php index 31b2731..eadff7d 100644 --- a/tests/Unit/V1/ErrorCollectionTest.php +++ b/tests/Unit/V1/ErrorCollectionTest.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AccessKey; use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use Art4\JsonApiClient\V1\ErrorCollection; use PHPUnit\Framework\TestCase; @@ -47,10 +48,10 @@ public function testCreate(): void $this->assertSame($collection->getKeys(), [0, 1]); - $this->assertFalse($collection->has(new \stdClass())); - $this->assertFalse($collection->has([])); $this->assertFalse($collection->has('string')); + $this->assertTrue($collection->has(AccessKey::create(0))); + $this->assertTrue($collection->has(0)); $error = $collection->get(0); diff --git a/tests/Unit/V1/ResourceCollectionTest.php b/tests/Unit/V1/ResourceCollectionTest.php index 67d00e1..aed1f05 100644 --- a/tests/Unit/V1/ResourceCollectionTest.php +++ b/tests/Unit/V1/ResourceCollectionTest.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AccessKey; use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use Art4\JsonApiClient\V1\ResourceCollection; use PHPUnit\Framework\TestCase; @@ -41,11 +42,10 @@ public function testCreateWithEmptyArray(): void $this->assertInstanceOf(Accessable::class, $collection); $this->assertSame($collection->getKeys(), []); - $this->assertFalse($collection->has(0)); // Test get() with various key types - $this->assertFalse($collection->has(new \stdClass())); - $this->assertFalse($collection->has([])); + $this->assertFalse($collection->has(0)); + $this->assertFalse($collection->has(AccessKey::create(0))); $this->assertFalse($collection->has('string')); } diff --git a/tests/Unit/V1/ResourceNullTest.php b/tests/Unit/V1/ResourceNullTest.php index cbaf377..b59c69e 100644 --- a/tests/Unit/V1/ResourceNullTest.php +++ b/tests/Unit/V1/ResourceNullTest.php @@ -64,4 +64,54 @@ public function testGetThrowsException(): void $resource->get('something'); } + + public function testHasWithObjectAsKeyTriggersException(): void + { + $resource = new ResourceNull( + null, + $this->manager, + $this->parent + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has(new \stdClass()); + } + + public function testHasWithArrayAsKeyTriggersException(): void + { + $resource = new ResourceNull( + null, + $this->manager, + $this->parent + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has([]); + } } From ff3773c28125a2e1ac30a36af4bea3975185e50f Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Oct 2023 16:58:31 +0200 Subject: [PATCH 08/11] Ignore false positives by phpstan --- .phpstan.neon | 26 ++++++++++++++++++++--- src/V1/ResourceNull.php | 8 ------- tests/Unit/Helper/AccessableTraitTest.php | 3 +++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.phpstan.neon b/.phpstan.neon index e4fa0be..497960f 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -14,33 +14,53 @@ parameters: scanDirectories: - vendor + treatPhpDocTypesAsCertain: false + ignoreErrors: - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$data is never read, only written\\.$#" + message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$data\\.$#" count: 1 path: src/V1/ResourceNull.php + # parameter is required by Art4\JsonApiClient\Element - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$manager is never read, only written\\.$#" + message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$manager\\.$#" count: 1 path: src/V1/ResourceNull.php + # parameter is required by Art4\JsonApiClient\Element - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$parent is never read, only written\\.$#" + message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$parent\\.$#" count: 1 path: src/V1/ResourceNull.php + # parameter is required by Art4\JsonApiClient\Element - message: "#^Constructor of an anonymous class has an unused parameter \\$data\\.$#" count: 1 path: tests/BC/ElementTest.php + # parameter is required by Art4\JsonApiClient\Element - message: "#^Constructor of an anonymous class has an unused parameter \\$manager\\.$#" count: 1 path: tests/BC/ElementTest.php + # parameter is required by Art4\JsonApiClient\Element - message: "#^Constructor of an anonymous class has an unused parameter \\$parent\\.$#" count: 1 path: tests/BC/ElementTest.php + # parameter is required by Art4\JsonApiClient\Element + + - + message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, array given\\.$#" + count: 1 + path: tests/Unit/V1/ResourceNullTest.php + # We are providing an invalid parameter to test the deprecation message + + - + message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, stdClass given\\.$#" + count: 1 + path: tests/Unit/V1/ResourceNullTest.php + # We are providing an invalid parameter to test the deprecation message diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index 3065002..f4bd600 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -19,11 +19,6 @@ */ final class ResourceNull implements Accessable, Element { - /** @var mixed */ - private $data; - private Manager $manager; - private Accessable $parent; - /** * Constructor * @@ -33,9 +28,6 @@ final class ResourceNull implements Accessable, Element */ public function __construct($data, Manager $manager, Accessable $parent) { - $this->data = $data; - $this->manager = $manager; - $this->parent = $parent; } /** diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php index af512b3..cda7d40 100644 --- a/tests/Unit/Helper/AccessableTraitTest.php +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -9,12 +9,14 @@ namespace Art4\JsonApiClient\Tests\Unit\Helper; use Art4\JsonApiClient\Helper\AccessableTrait; +use Art4\JsonApiClient\Accessable; use PHPUnit\Framework\TestCase; class AccessableTraitTest extends TestCase { public function testHasWithObjectAsKeyTriggersException(): void { + /** @var Accessable */ $resource = $this->getMockForTrait(AccessableTrait::class); // PHPUnit 10 compatible way to test trigger_error(). @@ -36,6 +38,7 @@ function ($errno, $errstr): bool { public function testHasWithArrayAsKeyTriggersException(): void { + /** @var Accessable */ $resource = $this->getMockForTrait(AccessableTrait::class); // PHPUnit 10 compatible way to test trigger_error(). From 91d4ed948489f58ad95dd992397e5102372b18ef Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Oct 2023 09:26:56 +0200 Subject: [PATCH 09/11] Fix code style --- src/V1/ResourceNull.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index f4bd600..3d48c84 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -26,9 +26,7 @@ final class ResourceNull implements Accessable, Element * @param \Art4\JsonApiClient\Manager $manager The manager * @param \Art4\JsonApiClient\Accessable $parent The parent */ - public function __construct($data, Manager $manager, Accessable $parent) - { - } + public function __construct($data, Manager $manager, Accessable $parent) {} /** * Check if a value exists in this resource From 3ce9d443aef3dcb54d21bf8da1e193ed6b8200d3 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Oct 2023 15:02:17 +0200 Subject: [PATCH 10/11] reach PHPStan level 9 --- .phpstan.neon | 14 ++++++++++- src/Helper/AccessKey.php | 2 +- src/V1/Link.php | 3 +-- tests/Functional/ParsingTest.php | 26 +++++++++++++++++--- tests/Unit/Input/RequestStringInputTest.php | 11 +-------- tests/Unit/Input/ResponseStringInputTest.php | 11 +-------- 6 files changed, 39 insertions(+), 28 deletions(-) diff --git a/.phpstan.neon b/.phpstan.neon index 497960f..5567202 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -5,7 +5,7 @@ includes: - vendor/phpstan/phpstan-phpunit/extension.neon parameters: - level: 8 + level: 9 paths: - src/ @@ -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 diff --git a/src/Helper/AccessKey.php b/src/Helper/AccessKey.php index b647d9a..bed0120 100644 --- a/src/Helper/AccessKey.php +++ b/src/Helper/AccessKey.php @@ -22,7 +22,7 @@ final class AccessKey extends SplStack /** * Transforms the Key to a string * - * @param mixed $key + * @param int|string $key * * @return AccessKey */ diff --git a/src/V1/Link.php b/src/V1/Link.php index 6489924..5e585b1 100644 --- a/src/V1/Link.php +++ b/src/V1/Link.php @@ -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 { diff --git a/tests/Functional/ParsingTest.php b/tests/Functional/ParsingTest.php index 18a67f1..7e4957c 100644 --- a/tests/Functional/ParsingTest.php +++ b/tests/Functional/ParsingTest.php @@ -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); @@ -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); @@ -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')); @@ -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); @@ -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); @@ -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); @@ -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); @@ -288,6 +303,7 @@ 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); @@ -295,6 +311,8 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $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); diff --git a/tests/Unit/Input/RequestStringInputTest.php b/tests/Unit/Input/RequestStringInputTest.php index 5256afa..665d562 100644 --- a/tests/Unit/Input/RequestStringInputTest.php +++ b/tests/Unit/Input/RequestStringInputTest.php @@ -17,9 +17,6 @@ class RequestStringInputTest extends TestCase { use HelperTrait; - /** - * @test - */ public function testGetAsObjectFromStringReturnsObject(): void { $input = new RequestStringInput('{}'); @@ -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( @@ -44,9 +38,6 @@ public function testCreateWithoutStringThrowsException($input): void /** * @dataProvider jsonValuesAsStringProviderWithoutObject - * @test - * - * @param string $input */ public function testGetAsObjectWithInvalidStringsThrowsException(string $input): void { diff --git a/tests/Unit/Input/ResponseStringInputTest.php b/tests/Unit/Input/ResponseStringInputTest.php index c554f1b..d7e0755 100644 --- a/tests/Unit/Input/ResponseStringInputTest.php +++ b/tests/Unit/Input/ResponseStringInputTest.php @@ -17,9 +17,6 @@ class ResponseStringInputTest extends TestCase { use HelperTrait; - /** - * @test - */ public function testGetAsObjectFromStringReturnsObject(): void { $input = new ResponseStringInput('{}'); @@ -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( @@ -44,9 +38,6 @@ public function testCreateWithoutStringThrowsException($input): void /** * @dataProvider jsonValuesAsStringProviderWithoutObject - * @test - * - * @param string $input */ public function testGetAsObjectWithInvalidStringsThrowsException(string $input): void { From 901f1217dd131351ed4788560be3a24c577c73b2 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Oct 2023 15:06:22 +0200 Subject: [PATCH 11/11] fix mixed return type with PHP 7.4 --- tests/Unit/Input/RequestStringInputTest.php | 4 +++- tests/Unit/Input/ResponseStringInputTest.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Input/RequestStringInputTest.php b/tests/Unit/Input/RequestStringInputTest.php index 665d562..f9d49e7 100644 --- a/tests/Unit/Input/RequestStringInputTest.php +++ b/tests/Unit/Input/RequestStringInputTest.php @@ -26,8 +26,10 @@ public function testGetAsObjectFromStringReturnsObject(): void /** * @dataProvider jsonValuesProviderWithoutString + * + * @param mixed $input */ - public function testCreateWithoutStringThrowsException(mixed $input): void + public function testCreateWithoutStringThrowsException($input): void { $this->expectException(InputException::class); $this->expectExceptionMessage( diff --git a/tests/Unit/Input/ResponseStringInputTest.php b/tests/Unit/Input/ResponseStringInputTest.php index d7e0755..8b6f24c 100644 --- a/tests/Unit/Input/ResponseStringInputTest.php +++ b/tests/Unit/Input/ResponseStringInputTest.php @@ -26,8 +26,10 @@ public function testGetAsObjectFromStringReturnsObject(): void /** * @dataProvider jsonValuesProviderWithoutString + * + * @param mixed $input */ - public function testCreateWithoutStringThrowsException(mixed $input): void + public function testCreateWithoutStringThrowsException($input): void { $this->expectException(InputException::class); $this->expectExceptionMessage(