From e90ba72236b723f28db29e199bcdc3976c6a4111 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 25 Oct 2023 16:40:08 +0200 Subject: [PATCH 1/7] Add parameter types in AccessKey, fix deprecates PHPUnit method --- src/Helper/AccessKey.php | 25 ++++-------- src/Helper/AccessableTrait.php | 19 ++++++--- tests/Fixtures/AccessableTraitMock.php | 16 ++++++++ tests/Fixtures/HelperTrait.php | 15 ++++++++ tests/Unit/Helper/AccessableTraitTest.php | 47 +++++++++++++++-------- tests/Unit/V1/ErrorCollectionTest.php | 6 +-- tests/Unit/V1/ResourceCollectionTest.php | 5 ++- 7 files changed, 90 insertions(+), 43 deletions(-) create mode 100644 tests/Fixtures/AccessableTraitMock.php diff --git a/src/Helper/AccessKey.php b/src/Helper/AccessKey.php index bed0120..fbc7a31 100644 --- a/src/Helper/AccessKey.php +++ b/src/Helper/AccessKey.php @@ -22,31 +22,20 @@ final class AccessKey extends SplStack /** * Transforms the Key to a string * - * @param int|string $key - * * @return AccessKey */ - public static function create($key): AccessKey + public static function create(string $key): AccessKey { - // Ignore arrays and objects - if (is_object($key) or is_array($key)) { - $key = ''; - } - - $key_string = strval($key); - - $key = new self(); - $key->raw = $key_string; - - $keys = explode('.', $key_string); + $accessKey = new self(); + $accessKey->raw = $key; - foreach ($keys as $value) { - $key->push($value); + foreach (explode('.', $key) as $value) { + $accessKey->push($value); } - $key->rewind(); + $accessKey->rewind(); - return $key; + return $accessKey; } /** diff --git a/src/Helper/AccessableTrait.php b/src/Helper/AccessableTrait.php index 6cee963..95a78af 100644 --- a/src/Helper/AccessableTrait.php +++ b/src/Helper/AccessableTrait.php @@ -62,6 +62,8 @@ final public function has($key): bool gettype($key), AccessKey::class ), \E_USER_DEPRECATED); + + $key = ''; } $key = $this->parseKey($key); @@ -97,6 +99,17 @@ final public function has($key): bool */ public function get($key) { + if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { + trigger_error(sprintf( + '%s::get(): 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 = ''; + } + $key = $this->parseKey($key); $string = $key->shift(); @@ -136,8 +149,6 @@ private function getValue(string $key) * Parse a dot.notated.key to an object * * @param int|string|AccessKey $key The key - * - * @return AccessKey The parsed key */ private function parseKey($key): AccessKey { @@ -145,8 +156,6 @@ private function parseKey($key): AccessKey return $key; } - $key = AccessKey::create($key); - - return $key; + return AccessKey::create(strval($key)); } } diff --git a/tests/Fixtures/AccessableTraitMock.php b/tests/Fixtures/AccessableTraitMock.php new file mode 100644 index 0000000..3a1c797 --- /dev/null +++ b/tests/Fixtures/AccessableTraitMock.php @@ -0,0 +1,16 @@ +> + */ + public static function jsonValuesProviderWithoutStringAndInt(): array + { + $data = static::jsonValuesProvider(); + + unset($data[2]); + unset($data[3]); + + return array_values($data); + } + /** * Json Values as string Provider * diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php index cda7d40..9083e20 100644 --- a/tests/Unit/Helper/AccessableTraitTest.php +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -8,22 +8,29 @@ namespace Art4\JsonApiClient\Tests\Unit\Helper; -use Art4\JsonApiClient\Helper\AccessableTrait; -use Art4\JsonApiClient\Accessable; +use Art4\JsonApiClient\Exception\AccessException; +use Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock; +use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use PHPUnit\Framework\TestCase; class AccessableTraitTest extends TestCase { - public function testHasWithObjectAsKeyTriggersException(): void + use HelperTrait; + + /** + * @dataProvider jsonValuesProviderWithoutStringAndInt + * + * @param mixed $key + */ + public function testHasWithInvalidKeyTypeTriggersDeprecationError($key): void { - /** @var Accessable */ - $resource = $this->getMockForTrait(AccessableTrait::class); + $resource = new AccessableTraitMock(); // 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.', + function ($errno, $errstr) use ($key): bool { + $this->assertSame( + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as '.gettype($key).' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', $errstr ); @@ -33,19 +40,23 @@ function ($errno, $errstr): bool { E_USER_DEPRECATED ); - $resource->has(new \stdClass()); + $resource->has($key); } - public function testHasWithArrayAsKeyTriggersException(): void + /** + * @dataProvider jsonValuesProviderWithoutStringAndInt + * + * @param mixed $key + */ + public function testGetWithInvalidKeyTypeTriggersDeprecationError($key): void { - /** @var Accessable */ - $resource = $this->getMockForTrait(AccessableTrait::class); + $resource = new AccessableTraitMock(); // 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.', + function ($errno, $errstr) use ($key): bool { + $this->assertSame( + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as '.gettype($key).' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', $errstr ); @@ -55,6 +66,10 @@ function ($errno, $errstr): bool { E_USER_DEPRECATED ); - $resource->has([]); + try { + $resource->get($key); + } catch (AccessException $th) { + // ignore AccessException + } } } diff --git a/tests/Unit/V1/ErrorCollectionTest.php b/tests/Unit/V1/ErrorCollectionTest.php index eadff7d..e6fc4a1 100644 --- a/tests/Unit/V1/ErrorCollectionTest.php +++ b/tests/Unit/V1/ErrorCollectionTest.php @@ -49,15 +49,15 @@ public function testCreate(): void $this->assertSame($collection->getKeys(), [0, 1]); $this->assertFalse($collection->has('string')); - - $this->assertTrue($collection->has(AccessKey::create(0))); - + $this->assertTrue($collection->has(AccessKey::create('0'))); $this->assertTrue($collection->has(0)); + $error = $collection->get(0); $this->assertInstanceOf(Accessable::class, $error); $this->assertTrue($collection->has(1)); + $error = $collection->get(1); $this->assertInstanceOf(Accessable::class, $error); diff --git a/tests/Unit/V1/ResourceCollectionTest.php b/tests/Unit/V1/ResourceCollectionTest.php index aed1f05..9955a9d 100644 --- a/tests/Unit/V1/ResourceCollectionTest.php +++ b/tests/Unit/V1/ResourceCollectionTest.php @@ -45,7 +45,7 @@ public function testCreateWithEmptyArray(): void // Test get() with various key types $this->assertFalse($collection->has(0)); - $this->assertFalse($collection->has(AccessKey::create(0))); + $this->assertFalse($collection->has(AccessKey::create('0'))); $this->assertFalse($collection->has('string')); } @@ -95,6 +95,9 @@ public function testCreateWithIdentifierAndMeta(): void $this->assertInstanceOf(Accessable::class, $resource); + $this->assertSame($resource, $collection->get('0')); + $this->assertSame($resource, $collection->get(AccessKey::create('0'))); + $this->assertTrue($collection->has(1)); $resource = $collection->get(1); From 548e037538a58d07012bbde5ad169a3a3e0ceefe Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Oct 2023 13:13:34 +0200 Subject: [PATCH 2/7] PHPStan: ignore false positive errors --- .phpstan.neon | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.phpstan.neon b/.phpstan.neon index 5567202..5199950 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -76,3 +76,15 @@ parameters: 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\\\\Tests\\\\Fixtures\\\\AccessableTraitMock\\:\\:get\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, mixed given\\.$#" + count: 1 + path: tests/Unit/Helper/AccessableTraitTest.php + # We are providing an invalid parameter to test the deprecation message + + - + message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\Tests\\\\Fixtures\\\\AccessableTraitMock\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, mixed given\\.$#" + count: 1 + path: tests/Unit/Helper/AccessableTraitTest.php + # We are providing an invalid parameter to test the deprecation message From 11ce078aadbf862c4679012b8a4a3aeebb23679f Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Oct 2023 13:46:48 +0200 Subject: [PATCH 3/7] Fix code style --- tests/Fixtures/AccessableTraitMock.php | 3 ++- tests/Unit/Helper/AccessableTraitTest.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Fixtures/AccessableTraitMock.php b/tests/Fixtures/AccessableTraitMock.php index 3a1c797..c789b86 100644 --- a/tests/Fixtures/AccessableTraitMock.php +++ b/tests/Fixtures/AccessableTraitMock.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Helper\AccessableTrait; -class AccessableTraitMock implements Accessable { +class AccessableTraitMock implements Accessable +{ use AccessableTrait; } diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php index 9083e20..9953cf6 100644 --- a/tests/Unit/Helper/AccessableTraitTest.php +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -30,7 +30,7 @@ public function testHasWithInvalidKeyTypeTriggersDeprecationError($key): void set_error_handler( function ($errno, $errstr) use ($key): bool { $this->assertSame( - 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as '.gettype($key).' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as ' . gettype($key) . ' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', $errstr ); @@ -56,7 +56,7 @@ public function testGetWithInvalidKeyTypeTriggersDeprecationError($key): void set_error_handler( function ($errno, $errstr) use ($key): bool { $this->assertSame( - 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as '.gettype($key).' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as ' . gettype($key) . ' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', $errstr ); From ac25355a340c05131a8a8ad9b2484a2a101d53ca Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Oct 2023 14:02:39 +0200 Subject: [PATCH 4/7] Add comments about BC classes --- tests/BC/AccessableTest.php | 7 +++++-- tests/BC/ElementTest.php | 7 +++++-- tests/BC/Exception/ExceptionTest.php | 7 +++++-- tests/BC/FactoryTest.php | 7 +++++-- tests/BC/Input/InputTest.php | 7 +++++-- tests/BC/Input/RequestInputTest.php | 7 +++++-- tests/BC/ManagerTest.php | 7 +++++-- tests/BC/Serializer/SerializerTest.php | 7 +++++-- 8 files changed, 40 insertions(+), 16 deletions(-) diff --git a/tests/BC/AccessableTest.php b/tests/BC/AccessableTest.php index 7ee74b8..2a0d12c 100644 --- a/tests/BC/AccessableTest.php +++ b/tests/BC/AccessableTest.php @@ -15,11 +15,14 @@ class AccessableTest extends TestCase { /** * This test will test a custom implementation of the Accessable interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForAccessableInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Accessable { /** * Get a value by a key diff --git a/tests/BC/ElementTest.php b/tests/BC/ElementTest.php index 73c28e3..c48e5ca 100644 --- a/tests/BC/ElementTest.php +++ b/tests/BC/ElementTest.php @@ -17,8 +17,6 @@ class ElementTest extends TestCase { /** * This test will test a custom implementation of the Element interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForElementInterface(): void { @@ -26,6 +24,11 @@ public function testBcForElementInterface(): void $manager = $this->createMock(Manager::class); $parent = $this->createMock(Accessable::class); + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class ($data, $manager, $parent) implements Element { /** * Sets the manager and parent diff --git a/tests/BC/Exception/ExceptionTest.php b/tests/BC/Exception/ExceptionTest.php index c84d828..495427c 100644 --- a/tests/BC/Exception/ExceptionTest.php +++ b/tests/BC/Exception/ExceptionTest.php @@ -15,11 +15,14 @@ class ExceptionTest extends TestCase { /** * This test will test a custom implementation of the Exception interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForExceptionInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Exception {}; $this->assertInstanceOf(Exception::class, $class); diff --git a/tests/BC/FactoryTest.php b/tests/BC/FactoryTest.php index beb1ecd..253983f 100644 --- a/tests/BC/FactoryTest.php +++ b/tests/BC/FactoryTest.php @@ -15,11 +15,14 @@ class FactoryTest extends TestCase { /** * This test will test a custom implementation of the Factory interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForFactoryInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Factory { /** * Create a new instance of a class diff --git a/tests/BC/Input/InputTest.php b/tests/BC/Input/InputTest.php index 1e0d0c7..16664cb 100644 --- a/tests/BC/Input/InputTest.php +++ b/tests/BC/Input/InputTest.php @@ -15,11 +15,14 @@ class InputTest extends TestCase { /** * This test will test a custom implementation of the Input interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForInputInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Input { /** * Get the input as simple object diff --git a/tests/BC/Input/RequestInputTest.php b/tests/BC/Input/RequestInputTest.php index c52b939..ffc76b9 100644 --- a/tests/BC/Input/RequestInputTest.php +++ b/tests/BC/Input/RequestInputTest.php @@ -15,11 +15,14 @@ class RequestInputTest extends TestCase { /** * This test will test a custom implementation of the RequestInput interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForRequestInputInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements RequestInput {}; $this->assertInstanceOf(RequestInput::class, $class); diff --git a/tests/BC/ManagerTest.php b/tests/BC/ManagerTest.php index 34005a8..573dfd1 100644 --- a/tests/BC/ManagerTest.php +++ b/tests/BC/ManagerTest.php @@ -16,11 +16,14 @@ class ManagerTest extends TestCase { /** * This test will test a custom implementation of the Manager interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForManagerInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Manager { /** * Parse the input diff --git a/tests/BC/Serializer/SerializerTest.php b/tests/BC/Serializer/SerializerTest.php index 6ce99fd..ee82bca 100644 --- a/tests/BC/Serializer/SerializerTest.php +++ b/tests/BC/Serializer/SerializerTest.php @@ -16,11 +16,14 @@ class SerializerTest extends TestCase { /** * This test will test a custom implementation of the Serializer interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForSerializerInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Serializer { /** * Serialize data From 7790b3b592876a114b7669ba83f39654247082c0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Oct 2023 14:41:08 +0200 Subject: [PATCH 5/7] Add types for parameter and return in final classes --- src/Factory.php | 6 ++---- src/Helper/RootAccessable.php | 2 -- src/Manager.php | 7 ++----- src/Manager/ErrorAbortManager.php | 5 ++--- src/V1/Factory.php | 5 ++--- src/V1/ResourceItemLink.php | 1 - src/V1/ResourceNull.php | 4 ++-- 7 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 7dd1ed9..5d7ab7b 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -16,14 +16,12 @@ interface Factory /** * Create a new instance of a class * - * @deprecated The `\Art4\JsonApiClient\Factory::make()` methods first parameter signature will be `string` in v2.0. * @deprecated `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * - * @param string $name - * @param array $args + * @param array $args * * @return \Art4\JsonApiClient\Accessable */ - public function make($name, array $args = []); + public function make(string $name, array $args = []); // public function make(string $name, array $args = []): Accessable; } diff --git a/src/Helper/RootAccessable.php b/src/Helper/RootAccessable.php index 78c46ba..2983ed9 100644 --- a/src/Helper/RootAccessable.php +++ b/src/Helper/RootAccessable.php @@ -9,8 +9,6 @@ namespace Art4\JsonApiClient\Helper; use Art4\JsonApiClient\Accessable; -use Art4\JsonApiClient\Element; -use Art4\JsonApiClient\Manager; /** * RootAccessable diff --git a/src/Manager.php b/src/Manager.php index b22dde4..eba6d26 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -41,13 +41,10 @@ public function getFactory(); /** * Get a param by key * - * @deprecated The `\Art4\JsonApiClient\Manager::getParam()` methods first parameter signature will be `string` in v2.0. - * - * @param string $key - * @param mixed $default + * @param mixed $default * * @return mixed */ - public function getParam($key, $default); + public function getParam(string $key, $default); // public function getParam(string $key, $default); } diff --git a/src/Manager/ErrorAbortManager.php b/src/Manager/ErrorAbortManager.php index 996d3c5..db8567c 100644 --- a/src/Manager/ErrorAbortManager.php +++ b/src/Manager/ErrorAbortManager.php @@ -79,12 +79,11 @@ public function getFactory(): Factory /** * Get a param by key * - * @param string $key - * @param mixed $default + * @param mixed $default * * @return mixed */ - public function getParam($key, $default) + public function getParam(string $key, $default) { if (array_key_exists($key, $this->config)) { return $this->config[$key]; diff --git a/src/V1/Factory.php b/src/V1/Factory.php index 78dc1dc..33efadf 100644 --- a/src/V1/Factory.php +++ b/src/V1/Factory.php @@ -55,10 +55,9 @@ public function __construct(array $overload = []) /** * Create a new instance of a class * - * @param string $name - * @param array $args + * @param array $args */ - public function make($name, array $args = []): Accessable + public function make(string $name, array $args = []): Accessable { if (! isset($this->classes[$name])) { throw new FactoryException('"' . $name . '" is not a registered class'); diff --git a/src/V1/ResourceItemLink.php b/src/V1/ResourceItemLink.php index 5315593..cf31ef6 100644 --- a/src/V1/ResourceItemLink.php +++ b/src/V1/ResourceItemLink.php @@ -57,7 +57,6 @@ public function get($key) /** * Set a link * - * @param string $name The Name * @param string|object|mixed $link The Link */ private function setLink(string $name, $link): void diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index 3d48c84..935cf5a 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -35,7 +35,7 @@ public function __construct($data, Manager $manager, Accessable $parent) {} * * @return bool false */ - public function has($key) + public function has($key): bool { if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { trigger_error(sprintf( @@ -54,7 +54,7 @@ public function has($key) * * @return array Keys of all setted values */ - public function getKeys() + public function getKeys(): array { return []; } From 4b14fe01a92dfdbd55eb71d63086760faed4ba33 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Oct 2023 15:39:21 +0200 Subject: [PATCH 6/7] Fix missleading deprecated phpdoc attributes --- src/Accessable.php | 10 ++++------ src/Exception/AccessException.php | 2 +- src/Exception/Exception.php | 6 +++--- src/Exception/FactoryException.php | 2 +- src/Exception/InputException.php | 2 +- src/Exception/ValidationException.php | 2 +- src/Factory.php | 5 ++--- src/Input/Input.php | 5 ++--- src/Manager.php | 11 ++++------- src/Serializer/Serializer.php | 4 ++-- 10 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/Accessable.php b/src/Accessable.php index cf63145..8df8d91 100644 --- a/src/Accessable.php +++ b/src/Accessable.php @@ -27,22 +27,20 @@ public function get($key); /** * Check if a value exists * - * @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. + * @return-type-will-change bool `\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 * * @return bool */ - public function has($key); - // public function has($key): bool; + public function has($key)/*: bool */; /** * Returns the keys of all setted values * - * @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-type-will-change array `\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 */ - public function getKeys(); - // public function getKeys(): array; + public function getKeys()/*: array */; } diff --git a/src/Exception/AccessException.php b/src/Exception/AccessException.php index 9edf22e..ee57d2e 100644 --- a/src/Exception/AccessException.php +++ b/src/Exception/AccessException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class AccessException extends \RuntimeException implements Exception, \Throwable {} +class AccessException extends \RuntimeException implements Exception {} diff --git a/src/Exception/Exception.php b/src/Exception/Exception.php index 0e88f54..87f6be5 100644 --- a/src/Exception/Exception.php +++ b/src/Exception/Exception.php @@ -9,7 +9,7 @@ namespace Art4\JsonApiClient\Exception; /** - * @deprecated `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in v2.0. Do the same in your implementation now to avoid errors. + * Please note: `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in v2.0. Do the same in your implementation now to avoid errors. */ -// interface Exception extends \Throwable -interface Exception {} +interface Exception /*extends \Throwable */ +{} diff --git a/src/Exception/FactoryException.php b/src/Exception/FactoryException.php index 3b8ffbb..be7cb9c 100644 --- a/src/Exception/FactoryException.php +++ b/src/Exception/FactoryException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class FactoryException extends \Exception implements Exception, \Throwable {} +class FactoryException extends \Exception implements Exception {} diff --git a/src/Exception/InputException.php b/src/Exception/InputException.php index b59fe12..5ec8cc6 100644 --- a/src/Exception/InputException.php +++ b/src/Exception/InputException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class InputException extends \Exception implements Exception, \Throwable {} +class InputException extends \Exception implements Exception {} diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php index 771e20a..4273774 100644 --- a/src/Exception/ValidationException.php +++ b/src/Exception/ValidationException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class ValidationException extends \InvalidArgumentException implements Exception, \Throwable {} +class ValidationException extends \InvalidArgumentException implements Exception {} diff --git a/src/Factory.php b/src/Factory.php index 5d7ab7b..c3fa021 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -16,12 +16,11 @@ interface Factory /** * Create a new instance of a class * - * @deprecated `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Accessable `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * * @param array $args * * @return \Art4\JsonApiClient\Accessable */ - public function make(string $name, array $args = []); - // public function make(string $name, array $args = []): Accessable; + public function make(string $name, array $args = [])/*: Accessable */; } diff --git a/src/Input/Input.php b/src/Input/Input.php index 457a534..163b53a 100644 --- a/src/Input/Input.php +++ b/src/Input/Input.php @@ -16,7 +16,7 @@ interface Input /** * Get the input as simple object * - * @deprecated `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change \stdClass `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * * This should be a native PHP stdClass object, so Manager could * iterate over all public attributes @@ -25,6 +25,5 @@ interface Input * * @return \stdClass */ - public function getAsObject(); - // public function getAsObject(): \stdClass; + public function getAsObject()/*: \stdClass */; } diff --git a/src/Manager.php b/src/Manager.php index eba6d26..b9df215 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -18,25 +18,23 @@ interface Manager /** * Parse the input * - * @deprecated `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Accessable `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * * @throws \Art4\JsonApiClient\Exception\InputException If $input contains invalid JSON API * @throws \Art4\JsonApiClient\Exception\ValidationException If $input contains invalid JSON API * * @return \Art4\JsonApiClient\Accessable */ - public function parse(Input $input); - // public function parse(Input $input): Accessable; + public function parse(Input $input)/*: Accessable */; /** * Get a factory from the manager * - * @deprecated `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Factory `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * * @return \Art4\JsonApiClient\Factory */ - public function getFactory(); - // public function getFactory(): Factory; + public function getFactory()/*: Factory */; /** * Get a param by key @@ -46,5 +44,4 @@ public function getFactory(); * @return mixed */ public function getParam(string $key, $default); - // public function getParam(string $key, $default); } diff --git a/src/Serializer/Serializer.php b/src/Serializer/Serializer.php index 823ebe1..f6a4324 100644 --- a/src/Serializer/Serializer.php +++ b/src/Serializer/Serializer.php @@ -15,9 +15,9 @@ interface Serializer /** * Serialize data * - * @deprecated `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change ?array `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * * @return array|null */ - public function serialize(Accessable $data); + public function serialize(Accessable $data)/*: ?array */; } From f015aa7e0e0089a954850e65b1e7f4494c86b27c Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 26 Oct 2023 15:59:01 +0200 Subject: [PATCH 7/7] Update CHANGELOG.md, fix deprecation error messages --- CHANGELOG.md | 25 ++++++++++++++--------- src/Accessable.php | 4 ++-- src/Exception/Exception.php | 2 +- src/Factory.php | 2 +- src/Helper/AccessableTrait.php | 10 ++++----- src/Input/Input.php | 2 +- src/Manager.php | 4 ++-- src/Serializer/Serializer.php | 2 +- src/V1/ResourceNull.php | 5 ++--- tests/Unit/Helper/AccessableTraitTest.php | 4 ++-- tests/Unit/V1/ResourceNullTest.php | 4 ++-- 11 files changed, 33 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fae9c7d..30cab1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add support for PHP 8.2 +### Deprecated + +- Providing Argument #1 ($key) in `\Art4\JsonApiClient\Accessable::has()` not as `int|string` is deprecated and will throw an Exception in 2.0.0. +- Providing Argument #1 ($key) in `\Art4\JsonApiClient\Accessable::get()` not as `int|string` is deprecated and will throw an Exception in 2.0.0. + ## [1.1.0 - 2021-10-05](https://github.com/Art4/json-api-client/compare/1.0.0...1.1.0) ### Added @@ -25,16 +30,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### 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. -- `\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. -- `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Factory::make()` methods first parameter signature will be `string` in v2.0. -- `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Manager::getParam()` methods first parameter signature will be `string` in v2.0. -- `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Accessable::getKeys()` will add `array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Factory::make()` methods first parameter signature will be `string` in 2.0.0. +- `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Manager::getParam()` methods first parameter signature will be `string` in 2.0.0. +- `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. ## [1.0.0 - 2021-03-05](https://github.com/Art4/json-api-client/compare/0.10.2...1.0.0) diff --git a/src/Accessable.php b/src/Accessable.php index 8df8d91..11f3c93 100644 --- a/src/Accessable.php +++ b/src/Accessable.php @@ -27,7 +27,7 @@ public function get($key); /** * Check if a value exists * - * @return-type-will-change bool `\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. + * @return-type-will-change bool `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @param mixed $key The key * @@ -38,7 +38,7 @@ public function has($key)/*: bool */; /** * Returns the keys of all setted values * - * @return-type-will-change array `\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-type-will-change array `\Art4\JsonApiClient\Accessable::getKeys()` will add `array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @return array Keys of all setted values */ diff --git a/src/Exception/Exception.php b/src/Exception/Exception.php index 87f6be5..ffdeb7c 100644 --- a/src/Exception/Exception.php +++ b/src/Exception/Exception.php @@ -9,7 +9,7 @@ namespace Art4\JsonApiClient\Exception; /** - * Please note: `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in v2.0. Do the same in your implementation now to avoid errors. + * Please note: `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in 2.0.0, do the same in your implementation now to avoid errors. */ interface Exception /*extends \Throwable */ {} diff --git a/src/Factory.php b/src/Factory.php index c3fa021..d25a5c1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -16,7 +16,7 @@ interface Factory /** * Create a new instance of a class * - * @return-type-will-change Accessable `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Accessable `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @param array $args * diff --git a/src/Helper/AccessableTrait.php b/src/Helper/AccessableTrait.php index 95a78af..4a6a6ce 100644 --- a/src/Helper/AccessableTrait.php +++ b/src/Helper/AccessableTrait.php @@ -57,10 +57,9 @@ 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.', + '%s::has(): Providing Argument #1 ($key) as `%s` is deprecated since 1.2.0, please provide as `int|string` instead.', get_class($this), - gettype($key), - AccessKey::class + gettype($key) ), \E_USER_DEPRECATED); $key = ''; @@ -101,10 +100,9 @@ public function get($key) { if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { trigger_error(sprintf( - '%s::get(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.', + '%s::get(): Providing Argument #1 ($key) as `%s` is deprecated since 1.2.0, please provide as `int|string` instead.', get_class($this), - gettype($key), - AccessKey::class + gettype($key) ), \E_USER_DEPRECATED); $key = ''; diff --git a/src/Input/Input.php b/src/Input/Input.php index 163b53a..bb168c4 100644 --- a/src/Input/Input.php +++ b/src/Input/Input.php @@ -16,7 +16,7 @@ interface Input /** * Get the input as simple object * - * @return-type-will-change \stdClass `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change \stdClass `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * This should be a native PHP stdClass object, so Manager could * iterate over all public attributes diff --git a/src/Manager.php b/src/Manager.php index b9df215..0610da0 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -18,7 +18,7 @@ interface Manager /** * Parse the input * - * @return-type-will-change Accessable `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Accessable `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @throws \Art4\JsonApiClient\Exception\InputException If $input contains invalid JSON API * @throws \Art4\JsonApiClient\Exception\ValidationException If $input contains invalid JSON API @@ -30,7 +30,7 @@ public function parse(Input $input)/*: Accessable */; /** * Get a factory from the manager * - * @return-type-will-change Factory `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Factory `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @return \Art4\JsonApiClient\Factory */ diff --git a/src/Serializer/Serializer.php b/src/Serializer/Serializer.php index f6a4324..3801cbd 100644 --- a/src/Serializer/Serializer.php +++ b/src/Serializer/Serializer.php @@ -15,7 +15,7 @@ interface Serializer /** * Serialize data * - * @return-type-will-change ?array `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change ?array `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @return array|null */ diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index 935cf5a..d687293 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -39,10 +39,9 @@ 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.', + '%s::has(): Providing Argument #1 ($key) as `%s` is deprecated since 1.2.0, please provide as `int|string` instead.', get_class($this), - gettype($key), - AccessKey::class + gettype($key) ), \E_USER_DEPRECATED); } diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php index 9953cf6..f52ef81 100644 --- a/tests/Unit/Helper/AccessableTraitTest.php +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -30,7 +30,7 @@ public function testHasWithInvalidKeyTypeTriggersDeprecationError($key): void set_error_handler( function ($errno, $errstr) use ($key): bool { $this->assertSame( - 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as ' . gettype($key) . ' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as `' . gettype($key) . '` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr ); @@ -56,7 +56,7 @@ public function testGetWithInvalidKeyTypeTriggersDeprecationError($key): void set_error_handler( function ($errno, $errstr) use ($key): bool { $this->assertSame( - 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as ' . gettype($key) . ' is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as `' . gettype($key) . '` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr ); diff --git a/tests/Unit/V1/ResourceNullTest.php b/tests/Unit/V1/ResourceNullTest.php index b59c69e..b476e3d 100644 --- a/tests/Unit/V1/ResourceNullTest.php +++ b/tests/Unit/V1/ResourceNullTest.php @@ -77,7 +77,7 @@ public function testHasWithObjectAsKeyTriggersException(): void 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.', + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as `object` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr ); @@ -102,7 +102,7 @@ public function testHasWithArrayAsKeyTriggersException(): void 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.', + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as `array` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr );