Skip to content

Commit

Permalink
Merge pull request #85 from Art4/74-phpstan-level-8
Browse files Browse the repository at this point in the history
Fix test code for PHPStan level 9
  • Loading branch information
Art4 committed Oct 20, 2023
2 parents 936faba + 901f121 commit 1d7e8ed
Show file tree
Hide file tree
Showing 36 changed files with 312 additions and 175 deletions.
60 changes: 53 additions & 7 deletions .phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,32 +1,78 @@
# 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
level: 9

paths:
- src/
- tests/

scanDirectories:
- vendor

treatPhpDocTypesAsCertain: false

ignoreErrors:
-
message: "#^Class Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey extends generic class SplStack but does not specify its types\\: TValue$#"
message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$data\\.$#"
count: 1
path: src/Helper/AccessKey.php
path: src/V1/ResourceNull.php
# parameter is required by Art4\JsonApiClient\Element

-
message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$data 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\\:\\:\\$manager 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: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$parent is never read, only written\\.$#"
message: "#^Constructor of an anonymous class has an unused parameter \\$data\\.$#"
count: 1
path: src/V1/ResourceNull.php
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 \\$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
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion src/Accessable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Art4\JsonApiClient;

use Art4\JsonApiClient\Helper\AccessKey;

/**
* Accessable Interface
*/
Expand Down Expand Up @@ -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<string> Keys of all setted values
* @return array<string|int> Keys of all setted values
*/
public function getKeys();
// public function getKeys(): array;
Expand Down
4 changes: 3 additions & 1 deletion src/Helper/AccessKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
/**
* AccessKey
*
* @extends SplStack<string>
*
* @internal
*/
final class AccessKey extends SplStack
{
/**
* Transforms the Key to a string
*
* @param mixed $key
* @param int|string $key
*
* @return AccessKey<string>
*/
Expand Down
17 changes: 13 additions & 4 deletions src/Helper/AccessableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final protected function set(string $key, $value): void
/**
* Returns the keys of all setted values
*
* @return array<string> Keys of all setted values
* @return array<int|string> Keys of all setted values
*/
final public function getKeys(): array
{
Expand All @@ -51,10 +51,19 @@ final public function getKeys(): array
/**
* Check if a value exists
*
* @param mixed $key The key
* @param int|string|AccessKey<string> $key The key
*/
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();
Expand Down Expand Up @@ -82,7 +91,7 @@ final public function has($key): bool
/**
* Get a value by a key
*
* @param mixed $key The key
* @param int|string|AccessKey<string> $key The key
*
* @return mixed
*/
Expand Down Expand Up @@ -126,7 +135,7 @@ private function getValue(string $key)
/**
* Parse a dot.notated.key to an object
*
* @param string|AccessKey<string> $key The key
* @param int|string|AccessKey<string> $key The key
*
* @return AccessKey<string> The parsed key
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(array $params = [])
/**
* Convert data in an array
*
* @return array<string, mixed>|null
* @return array<int|string, mixed>|null
*/
public function serialize(Accessable $data): ?array
{
Expand Down
5 changes: 3 additions & 2 deletions src/V1/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* Attributes Object
Expand Down Expand Up @@ -50,7 +51,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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/DocumentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/ErrorCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/ErrorLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/ErrorSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
5 changes: 3 additions & 2 deletions src/V1/Jsonapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand Down
8 changes: 4 additions & 4 deletions src/V1/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> $key The key of the value
*
* @return mixed The value
*/
Expand All @@ -60,8 +61,7 @@ public function get($key)
/**
* Set a link
*
* @param string $name The Name
* @param string|object $link The Link
* @param mixed $link The Link
*/
private function setAsLink(string $name, $link): void
{
Expand Down
Loading

0 comments on commit 1d7e8ed

Please sign in to comment.