Skip to content

Commit 1d7e8ed

Browse files
authored
Merge pull request #85 from Art4/74-phpstan-level-8
Fix test code for PHPStan level 9
2 parents 936faba + 901f121 commit 1d7e8ed

36 files changed

+312
-175
lines changed

.phpstan.neon

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,78 @@
11
# SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt
22
# SPDX-License-Identifier: GPL-3.0-or-later
33

4+
includes:
5+
- vendor/phpstan/phpstan-phpunit/extension.neon
6+
47
parameters:
5-
level: 8
8+
level: 9
69

710
paths:
811
- src/
12+
- tests/
913

1014
scanDirectories:
1115
- vendor
1216

17+
treatPhpDocTypesAsCertain: false
18+
1319
ignoreErrors:
1420
-
15-
message: "#^Class Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey extends generic class SplStack but does not specify its types\\: TValue$#"
21+
message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$data\\.$#"
1622
count: 1
17-
path: src/Helper/AccessKey.php
23+
path: src/V1/ResourceNull.php
24+
# parameter is required by Art4\JsonApiClient\Element
1825

1926
-
20-
message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$data is never read, only written\\.$#"
27+
message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$manager\\.$#"
2128
count: 1
2229
path: src/V1/ResourceNull.php
30+
# parameter is required by Art4\JsonApiClient\Element
2331

2432
-
25-
message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$manager is never read, only written\\.$#"
33+
message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$parent\\.$#"
2634
count: 1
2735
path: src/V1/ResourceNull.php
36+
# parameter is required by Art4\JsonApiClient\Element
2837

2938
-
30-
message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$parent is never read, only written\\.$#"
39+
message: "#^Constructor of an anonymous class has an unused parameter \\$data\\.$#"
3140
count: 1
32-
path: src/V1/ResourceNull.php
41+
path: tests/BC/ElementTest.php
42+
# parameter is required by Art4\JsonApiClient\Element
43+
44+
-
45+
message: "#^Constructor of an anonymous class has an unused parameter \\$manager\\.$#"
46+
count: 1
47+
path: tests/BC/ElementTest.php
48+
# parameter is required by Art4\JsonApiClient\Element
49+
50+
-
51+
message: "#^Constructor of an anonymous class has an unused parameter \\$parent\\.$#"
52+
count: 1
53+
path: tests/BC/ElementTest.php
54+
# parameter is required by Art4\JsonApiClient\Element
55+
56+
-
57+
message: "#^Parameter \\#1 \\$string of class Art4\\\\JsonApiClient\\\\Input\\\\RequestStringInput constructor expects string, mixed given\\.$#"
58+
count: 1
59+
path: tests/Unit/Input/RequestStringInputTest.php
60+
# We are providing an invalid parameter to test the exception message
61+
62+
-
63+
message: "#^Parameter \\#1 \\$string of class Art4\\\\JsonApiClient\\\\Input\\\\ResponseStringInput constructor expects string, mixed given\\.$#"
64+
count: 1
65+
path: tests/Unit/Input/ResponseStringInputTest.php
66+
# We are providing an invalid parameter to test the exception message
67+
68+
-
69+
message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, array given\\.$#"
70+
count: 1
71+
path: tests/Unit/V1/ResourceNullTest.php
72+
# We are providing an invalid parameter to test the deprecation message
73+
74+
-
75+
message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, stdClass given\\.$#"
76+
count: 1
77+
path: tests/Unit/V1/ResourceNullTest.php
78+
# We are providing an invalid parameter to test the deprecation message

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require-dev": {
1919
"friendsofphp/php-cs-fixer": "^3.35",
2020
"phpstan/phpstan": "^1.10",
21+
"phpstan/phpstan-phpunit": "^1.3",
2122
"phpunit/phpunit": "^9 || ^10"
2223
},
2324
"autoload": {

src/Accessable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Art4\JsonApiClient;
1010

11+
use Art4\JsonApiClient\Helper\AccessKey;
12+
1113
/**
1214
* Accessable Interface
1315
*/
@@ -39,7 +41,7 @@ public function has($key);
3941
*
4042
* @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.
4143
*
42-
* @return array<string> Keys of all setted values
44+
* @return array<string|int> Keys of all setted values
4345
*/
4446
public function getKeys();
4547
// public function getKeys(): array;

src/Helper/AccessKey.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
/**
1414
* AccessKey
1515
*
16+
* @extends SplStack<string>
17+
*
1618
* @internal
1719
*/
1820
final class AccessKey extends SplStack
1921
{
2022
/**
2123
* Transforms the Key to a string
2224
*
23-
* @param mixed $key
25+
* @param int|string $key
2426
*
2527
* @return AccessKey<string>
2628
*/

src/Helper/AccessableTrait.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final protected function set(string $key, $value): void
4141
/**
4242
* Returns the keys of all setted values
4343
*
44-
* @return array<string> Keys of all setted values
44+
* @return array<int|string> Keys of all setted values
4545
*/
4646
final public function getKeys(): array
4747
{
@@ -51,10 +51,19 @@ final public function getKeys(): array
5151
/**
5252
* Check if a value exists
5353
*
54-
* @param mixed $key The key
54+
* @param int|string|AccessKey<string> $key The key
5555
*/
5656
final public function has($key): bool
5757
{
58+
if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) {
59+
trigger_error(sprintf(
60+
'%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.',
61+
get_class($this),
62+
gettype($key),
63+
AccessKey::class
64+
), \E_USER_DEPRECATED);
65+
}
66+
5867
$key = $this->parseKey($key);
5968

6069
$string = $key->shift();
@@ -82,7 +91,7 @@ final public function has($key): bool
8291
/**
8392
* Get a value by a key
8493
*
85-
* @param mixed $key The key
94+
* @param int|string|AccessKey<string> $key The key
8695
*
8796
* @return mixed
8897
*/
@@ -126,7 +135,7 @@ private function getValue(string $key)
126135
/**
127136
* Parse a dot.notated.key to an object
128137
*
129-
* @param string|AccessKey<string> $key The key
138+
* @param int|string|AccessKey<string> $key The key
130139
*
131140
* @return AccessKey<string> The parsed key
132141
*/

src/Serializer/ArraySerializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(array $params = [])
3535
/**
3636
* Convert data in an array
3737
*
38-
* @return array<string, mixed>|null
38+
* @return array<int|string, mixed>|null
3939
*/
4040
public function serialize(Accessable $data): ?array
4141
{

src/V1/Attributes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Attributes Object
@@ -50,7 +51,7 @@ protected function parse($object): void
5051
/**
5152
* Get a value by the key of this object
5253
*
53-
* @param string $key The key of the value
54+
* @param int|string|AccessKey<string> $key The key of the value
5455
*
5556
* @return mixed The value
5657
*/

src/V1/Document.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
namespace Art4\JsonApiClient\V1;
1010

1111
use Art4\JsonApiClient\Accessable;
12-
use Art4\JsonApiClient\Helper\AbstractElement;
1312
use Art4\JsonApiClient\Exception\AccessException;
1413
use Art4\JsonApiClient\Exception\ValidationException;
14+
use Art4\JsonApiClient\Helper\AbstractElement;
15+
use Art4\JsonApiClient\Helper\AccessKey;
1516

1617
/**
1718
* Document Top Level Object
@@ -73,7 +74,7 @@ protected function parse($object): void
7374
/**
7475
* Get a value by the key of this object
7576
*
76-
* @param string $key The key of the value
77+
* @param int|string|AccessKey<string> $key The key of the value
7778
*
7879
* @return mixed The value
7980
*/

src/V1/DocumentLink.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Document Link Object
@@ -103,7 +104,7 @@ protected function parse($object): void
103104
/**
104105
* Get a value by the key of this object
105106
*
106-
* @param string $key The key of the value
107+
* @param int|string|AccessKey<string> $key The key of the value
107108
*
108109
* @return mixed The value
109110
*/

src/V1/Error.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Error Object
@@ -105,7 +106,7 @@ protected function parse($object): void
105106
/**
106107
* Get a value by the key of this object
107108
*
108-
* @param string $key The key of the value
109+
* @param int|string|AccessKey<string> $key The key of the value
109110
*
110111
* @return mixed The value
111112
*/

src/V1/ErrorCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Error Collection Object
@@ -44,7 +45,7 @@ protected function parse($object): void
4445
/**
4546
* Get a value by the key of this document
4647
*
47-
* @param string $key The key of the value
48+
* @param int|string|AccessKey<string> $key The key of the value
4849
*
4950
* @return mixed The value
5051
*/

src/V1/ErrorLink.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Error Link Object
@@ -63,7 +64,7 @@ protected function parse($object): void
6364
/**
6465
* Get a value by the key of this object
6566
*
66-
* @param string $key The key of the value
67+
* @param int|string|AccessKey<string> $key The key of the value
6768
*
6869
* @return mixed The value
6970
*/

src/V1/ErrorSource.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Error Source Object
@@ -52,7 +53,7 @@ protected function parse($object): void
5253
/**
5354
* Get a value by the key of this document
5455
*
55-
* @param string $key The key of the value
56+
* @param int|string|AccessKey<string> $key The key of the value
5657
*
5758
* @return mixed The value
5859
*/

src/V1/Jsonapi.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* JSON API Object
@@ -48,7 +49,7 @@ protected function parse($object): void
4849
/**
4950
* Get a value by the key of this object
5051
*
51-
* @param string $key The key of the value
52+
* @param int|string|AccessKey<string> $key The key of the value
5253
*
5354
* @return mixed The value
5455
*/

src/V1/Link.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Art4\JsonApiClient\V1;
1010

11-
use Art4\JsonApiClient\Helper\AbstractElement;
1211
use Art4\JsonApiClient\Exception\AccessException;
1312
use Art4\JsonApiClient\Exception\ValidationException;
13+
use Art4\JsonApiClient\Helper\AbstractElement;
14+
use Art4\JsonApiClient\Helper\AccessKey;
1415

1516
/**
1617
* Link Object
@@ -44,7 +45,7 @@ protected function parse($object): void
4445
/**
4546
* Get a value by the key of this object
4647
*
47-
* @param string $key The key of the value
48+
* @param int|string|AccessKey<string> $key The key of the value
4849
*
4950
* @return mixed The value
5051
*/
@@ -60,8 +61,7 @@ public function get($key)
6061
/**
6162
* Set a link
6263
*
63-
* @param string $name The Name
64-
* @param string|object $link The Link
64+
* @param mixed $link The Link
6565
*/
6666
private function setAsLink(string $name, $link): void
6767
{

0 commit comments

Comments
 (0)