Skip to content

Commit 6cf0c77

Browse files
committed
phpstan generics are so buggy :(
1 parent e1c98de commit 6cf0c77

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"pocketmine/binaryutils": "^0.2.0"
99
},
1010
"require-dev": {
11-
"phpstan/phpstan": "2.1.0",
11+
"phpstan/phpstan": "2.1.27",
1212
"phpunit/phpunit": "^9.5",
1313
"phpstan/extension-installer": "^1.0",
1414
"phpstan/phpstan-strict-rules": "^2.0",

src/tag/ListTag.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ public function getAllValues() : array{
9191
* @phpstan-this-out self<TTarget> $this
9292
*/
9393
private function checkTagClass(string $tagClass) : bool{
94-
return $this->value->isEmpty() ?
95-
$this->tagType === NBT::TAG_End :
96-
$this->first() instanceof $tagClass;
94+
return count($this->value) === 0 || $this->first() instanceof $tagClass;
9795
}
9896

9997
/**
@@ -124,7 +122,9 @@ public function getCount() : int{
124122
/**
125123
* Appends the specified tag to the end of the list.
126124
*
127-
* @phpstan-param TValue $tag
125+
* @phpstan-template TNewValue of TValue
126+
* @phpstan-param TNewValue $tag
127+
* @phpstan-this-out self<TNewValue>
128128
*/
129129
public function push(Tag $tag) : void{
130130
$this->checkTagType($tag);
@@ -145,7 +145,9 @@ public function pop() : Tag{
145145
/**
146146
* Adds the specified tag to the start of the list.
147147
*
148-
* @phpstan-param TValue $tag
148+
* @phpstan-template TNewValue of TValue
149+
* @phpstan-param TNewValue $tag
150+
* @phpstan-this-out self<TNewValue>
149151
*/
150152
public function unshift(Tag $tag) : void{
151153
$this->checkTagType($tag);
@@ -167,7 +169,9 @@ public function shift() : Tag{
167169
* Inserts a tag into the list between existing tags, at the specified offset. Later values in the list are moved up
168170
* by 1 position.
169171
*
170-
* @phpstan-param TValue $tag
172+
* @phpstan-template TNewValue of TValue
173+
* @phpstan-param TNewValue $tag
174+
* @phpstan-this-out self<TNewValue>
171175
*
172176
* @return void
173177
* @throws \OutOfRangeException if the offset is not within the bounds of the list
@@ -232,7 +236,9 @@ public function last() : Tag{
232236
/**
233237
* Overwrites the tag at the specified offset.
234238
*
235-
* @phpstan-param TValue $tag
239+
* @phpstan-template TNewValue of TValue
240+
* @phpstan-param TNewValue $tag
241+
* @phpstan-this-out self<TNewValue>
236242
*
237243
* @throws \OutOfRangeException if the offset is not within the bounds of the list
238244
*/

src/tag/Tag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ abstract protected function stringifyValue(int $indentation) : string;
5757
* Used for cloning tags in tags that have children.
5858
*
5959
* @throws \RuntimeException if a recursive dependency was detected
60+
* @return static
6061
*/
6162
public function safeClone() : Tag{
6263
if($this->cloning){

tests/phpstan/configs/phpstan-bugs.neon

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,37 @@ parameters:
1313
path: ../../../src/tag/IntArrayTag.php
1414

1515
-
16-
message: '#^Property pocketmine\\nbt\\tag\\ListTag\:\:\$value \(list\<pocketmine\\nbt\\tag\\Tag\>\) does not accept non\-empty\-array\<int\<0, max\>, pocketmine\\nbt\\tag\\Tag\>\.$#'
16+
message: '#^Property pocketmine\\nbt\\tag\\ListTag\<TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\>\:\:\$value \(list\<TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\>\) does not accept non\-empty\-array\<int\<0, max\>, \(TNewValue of TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\)\|TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\>\.$#'
1717
identifier: assign.propertyType
1818
count: 1
1919
path: ../../../src/tag/ListTag.php
2020

21+
-
22+
message: '#^Property pocketmine\\nbt\\tag\\ListTag\<TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\>\:\:\$value \(list\<TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\>\) does not accept non\-empty\-list\<\(TNewValue of TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\)\|TValue of pocketmine\\nbt\\tag\\Tag \= pocketmine\\nbt\\tag\\Tag\>\.$#'
23+
identifier: assign.propertyType
24+
count: 3
25+
path: ../../../src/tag/ListTag.php
26+
2127
-
2228
message: '#^Call to function is_string\(\) with string will always evaluate to true\.$#'
2329
identifier: function.alreadyNarrowedType
2430
count: 1
2531
path: ../../phpunit/tag/CompoundTagTest.php
32+
33+
-
34+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertNotSame\(\) with \*NEVER\* and pocketmine\\nbt\\tag\\Tag will always evaluate to true\.$#'
35+
identifier: staticMethod.alreadyNarrowedType
36+
count: 1
37+
path: ../../phpunit/tag/ListTagTest.php
38+
39+
-
40+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertSame\(\) with \*NEVER\* and pocketmine\\nbt\\tag\\Tag will always evaluate to false\.$#'
41+
identifier: staticMethod.impossibleType
42+
count: 1
43+
path: ../../phpunit/tag/ListTagTest.php
44+
45+
-
46+
message: '#^Instanceof between \*NEVER\* and pocketmine\\nbt\\tag\\ImmutableTag will always evaluate to false\.$#'
47+
identifier: instanceof.alwaysFalse
48+
count: 1
49+
path: ../../phpunit/tag/ListTagTest.php

0 commit comments

Comments
 (0)