Skip to content

Commit 6e87788

Browse files
Update to PHPStan 2.0
1 parent 08d2039 commit 6e87788

28 files changed

+422
-172
lines changed

Diff for: composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
"prefer-stable": true,
2525
"require": {
2626
"php": ">=8.0",
27-
"php-static-analysis/attributes": "^0.3.1 || dev-main",
28-
"php-static-analysis/node-visitor": "^0.3.1 || dev-main",
27+
"php-static-analysis/attributes": "^0.3.2 || dev-main",
28+
"php-static-analysis/node-visitor": "^0.3.2 || dev-main",
2929
"phpstan/phpstan": "^1.8 | ^2.0",
3030
"webmozart/assert": "^1.11"
3131
},
3232
"require-dev": {
33-
"php-static-analysis/psalm-plugin": "dev-main",
34-
"phpstan/phpstan-deprecation-rules": "^1.1",
33+
"php-static-analysis/psalm-plugin": "^0.3.2 || dev-main",
34+
"phpstan/phpstan-deprecation-rules": "^1.1 | ^2.0",
3535
"phpunit/phpunit": "^9.0",
3636
"symplify/easy-coding-standard": "^12.1",
3737
"vimeo/psalm": "^5"

Diff for: phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
includes:
22
- extension.neon
3-
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
44

55
parameters:
66
level: max

Diff for: tests/BaseAttributeTestCase.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Analyser\Analyser;
66
use PHPStan\Analyser\Error;
77
use PHPStan\File\FileHelper;
8+
use PHPStan\Internal\ComposerHelper;
89
use PHPStan\Testing\PHPStanTestCase;
910
use PhpStaticAnalysis\Attributes\Param;
1011
use PhpStaticAnalysis\Attributes\Returns;
@@ -49,4 +50,9 @@ public static function getAdditionalConfigFiles(): array
4950
__DIR__ . '/../extension.neon',
5051
];
5152
}
53+
54+
public static function getPhpStanVersion(): string
55+
{
56+
return ComposerHelper::getPhpStanVersion();
57+
}
5258
}

Diff for: tests/DefineTypeAttributeTest.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@ public function testInvalidClassDefineTypeAttribute(): void
2626
{
2727
$errors = $this->analyse(__DIR__ . '/data/DefineType/InvalidClassDefineTypeAttribute.php');
2828

29-
$expectedErrors = [
30-
'PHPDoc tag @phpstan-type has invalid value (): Unexpected token "\n * ", expected type at offset 20' => 7,
31-
'PHPDoc tag @phpstan-type name has invalid value: Unexpected token "(", expected TOKEN_PHPDOC_EOL at offset 72' => 7,
32-
'PHPDoc tag @phpstan-type string has invalid value: Unexpected token "\n * ", expected type at offset 44' => 7,
33-
'Parameter #1 ...$types of attribute class PhpStaticAnalysis\Attributes\DefineType constructor expects string, int given.' => 7,
34-
'Type alias has an invalid name: string.' => 7,
35-
'Attribute class PhpStaticAnalysis\Attributes\DefineType does not have the method target.' => 12,
36-
];
29+
if (self::getPhpStanVersion() < '2') {
30+
$expectedErrors = [
31+
'PHPDoc tag @phpstan-type has invalid value (): Unexpected token "\n * ", expected type at offset 20' => 7,
32+
'PHPDoc tag @phpstan-type name has invalid value: Unexpected token "(", expected TOKEN_PHPDOC_EOL at offset 72' => 7,
33+
'PHPDoc tag @phpstan-type string has invalid value: Unexpected token "\n * ", expected type at offset 44' => 7,
34+
'Parameter #1 ...$types of attribute class PhpStaticAnalysis\Attributes\DefineType constructor expects string, int given.' => 7,
35+
'Type alias has an invalid name: string.' => 7,
36+
'Attribute class PhpStaticAnalysis\Attributes\DefineType does not have the method target.' => 12,
37+
];
38+
} else {
39+
$expectedErrors = [
40+
'Parameter #1 ...$types of attribute class PhpStaticAnalysis\Attributes\DefineType constructor expects string, int given.' => 7,
41+
'Type alias has an invalid name: string.' => 7,
42+
'PHPDoc tag @phpstan-type has invalid value (): Unexpected token "\n * ", expected type at offset 20 on line 2' => 8,
43+
'PHPDoc tag @phpstan-type string has invalid value: Unexpected token "\n * ", expected type at offset 44 on line 3' => 9,
44+
'PHPDoc tag @phpstan-type name has invalid value: Unexpected token "(", expected TOKEN_PHPDOC_EOL at offset 72 on line 4' => 10,
45+
'Attribute class PhpStaticAnalysis\Attributes\DefineType does not have the method target.' => 12,
46+
];
47+
}
3748

3849
$this->checkExpectedErrors($errors, $expectedErrors);
3950
}

Diff for: tests/ImmutableAttributeTest.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ class ImmutableAttributeTest extends BaseAttributeTestCase
88
public function testClassImmutableAttribute(): void
99
{
1010
$errors = $this->analyse(__DIR__ . '/data/Immutable/ClassImmutableAttribute.php');
11-
$expectedErrors = [
12-
];
11+
if (self::getPhpStanVersion() < '2') {
12+
$expectedErrors = [];
13+
} else {
14+
$expectedErrors = [
15+
'@readonly property cannot have a default value.' => 10,
16+
'@readonly property test\PhpStaticAnalysis\PHPStanExtension\data\Immutable\ClassImmutableAttribute::$name is assigned outside of its declaring class.' => 14,
17+
];
18+
}
1319

1420
$this->checkExpectedErrors($errors, $expectedErrors);
1521
}
@@ -30,10 +36,18 @@ public function testInvalidClassImmutableAttribute(): void
3036
{
3137
$errors = $this->analyse(__DIR__ . '/data/Immutable/InvalidClassImmutableAttribute.php');
3238

33-
$expectedErrors = [
34-
'Attribute class PhpStaticAnalysis\Attributes\Immutable is not repeatable but is already present above the class.' => 10,
35-
'Attribute class PhpStaticAnalysis\Attributes\Immutable does not have the property target.' => 13,
36-
];
39+
if (self::getPhpStanVersion() < '2') {
40+
$expectedErrors = [
41+
'Attribute class PhpStaticAnalysis\Attributes\Immutable is not repeatable but is already present above the class.' => 10,
42+
'Attribute class PhpStaticAnalysis\Attributes\Immutable does not have the property target.' => 13,
43+
];
44+
} else {
45+
$expectedErrors = [
46+
'Attribute class PhpStaticAnalysis\Attributes\Immutable is not repeatable but is already present above the class.' => 10,
47+
'Attribute class PhpStaticAnalysis\Attributes\Immutable does not have the property target.' => 13,
48+
'@readonly property cannot have a default value.' => 14,
49+
];
50+
}
3751

3852
$this->checkExpectedErrors($errors, $expectedErrors);
3953
}

Diff for: tests/ImportTypeAttributeTest.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ public function testInvalidClassImportTypeAttribute(): void
2626
{
2727
$errors = $this->analyse(__DIR__ . '/data/ImportType/InvalidClassImportTypeAttribute.php');
2828

29-
$expectedErrors = [
30-
'PHPDoc tag @phpstan-import-type has invalid value (Unexpected token "(", expected \'*/\' at offset 98 on line 4): Unexpected token "(", expected \'*/\' at offset 98' => 8,
31-
'Parameter #1 ...$from of attribute class PhpStaticAnalysis\Attributes\ImportType constructor expects string, int given.' => 8,
32-
'Attribute class PhpStaticAnalysis\Attributes\ImportType does not have the method target.' => 13,
33-
];
29+
if (self::getPhpStanVersion() < '2') {
30+
$expectedErrors = [
31+
'PHPDoc tag @phpstan-import-type has invalid value (Unexpected token "(", expected \'*/\' at offset 98 on line 4): Unexpected token "(", expected \'*/\' at offset 98' => 8,
32+
'Parameter #1 ...$from of attribute class PhpStaticAnalysis\Attributes\ImportType constructor expects string, int given.' => 8,
33+
'Attribute class PhpStaticAnalysis\Attributes\ImportType does not have the method target.' => 13,
34+
];
35+
} else {
36+
$expectedErrors = [
37+
'Parameter #1 ...$from of attribute class PhpStaticAnalysis\Attributes\ImportType constructor expects string, int given.' => 8,
38+
'PHPDoc tag @phpstan-import-type has invalid value (Unexpected token "(", expected \'*/\' at offset 98 on line 4): Unexpected token "(", expected \'*/\' at offset 98 on line 4' => 11,
39+
'Attribute class PhpStaticAnalysis\Attributes\ImportType does not have the method target.' => 13,
40+
];
41+
}
3442

3543
$this->checkExpectedErrors($errors, $expectedErrors);
3644
}

Diff for: tests/ImpureAttributeTest.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ public function testInvalidMethodImpureAttribute(): void
2020
{
2121
$errors = $this->analyse(__DIR__ . '/data/Impure/InvalidMethodImpureAttribute.php');
2222

23-
$expectedErrors = [
24-
'Attribute class PhpStaticAnalysis\Attributes\Impure does not have the property target.' => 11,
25-
'Attribute class PhpStaticAnalysis\Attributes\Impure is not repeatable but is already present above the method.' => 15,
26-
];
23+
if (self::getPhpStanVersion() < '2') {
24+
$expectedErrors = [
25+
'Attribute class PhpStaticAnalysis\Attributes\Impure does not have the property target.' => 11,
26+
'Attribute class PhpStaticAnalysis\Attributes\Impure is not repeatable but is already present above the method.' => 15,
27+
];
28+
} else {
29+
$expectedErrors = [
30+
'Attribute class PhpStaticAnalysis\Attributes\Impure does not have the property target.' => 11,
31+
'Method test\PhpStaticAnalysis\PHPStanExtension\data\Impure\InvalidMethodImpureAttribute::getMoreName() is marked as impure but does not have any side effects.' => 14,
32+
'Attribute class PhpStaticAnalysis\Attributes\Impure is not repeatable but is already present above the method.' => 15,
33+
];
34+
}
2735

2836
$this->checkExpectedErrors($errors, $expectedErrors);
2937
}

Diff for: tests/IsReadOnlyAttributeTest.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ public function testInvalidPropertyIsReadOnlyAttribute(): void
2929

3030
public static function getAdditionalConfigFiles(): array
3131
{
32-
return array_merge(
33-
parent::getAdditionalConfigFiles(),
34-
[
35-
__DIR__ . '/conf/readonly.neon',
36-
]
37-
);
32+
if (self::getPhpStanVersion() < '2') {
33+
return array_merge(
34+
parent::getAdditionalConfigFiles(),
35+
[
36+
__DIR__ . '/conf/readonly.neon',
37+
]
38+
);
39+
} else {
40+
return parent::getAdditionalConfigFiles();
41+
}
3842
}
3943
}

Diff for: tests/MethodAttributeTest.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,23 @@ public function testInvalidClassMethodAttribute(): void
2626
{
2727
$errors = $this->analyse(__DIR__ . '/data/Method/InvalidClassMethodAttribute.php');
2828

29-
$expectedErrors = [
30-
'PHPDoc tag @method has invalid value (): Unexpected token "\n * ", expected type at offset 14' => 8,
31-
'PHPDoc tag @method has invalid value (string): Unexpected token "\n * ", expected \'(\' at offset 32' => 8,
32-
'Parameter #1 ...$methods of attribute class PhpStaticAnalysis\Attributes\Method constructor expects string, int given.' => 8,
33-
'Attribute class PhpStaticAnalysis\Attributes\Method does not have the method target.' => 13,
34-
'Call to an undefined method test\PhpStaticAnalysis\PHPStanExtension\data\Method\InvalidClassMethodAttribute::badFunction().' => 31,
35-
];
29+
if (self::getPhpStanVersion() < '2') {
30+
$expectedErrors = [
31+
'PHPDoc tag @method has invalid value (): Unexpected token "\n * ", expected type at offset 14' => 8,
32+
'PHPDoc tag @method has invalid value (string): Unexpected token "\n * ", expected \'(\' at offset 32' => 8,
33+
'Parameter #1 ...$methods of attribute class PhpStaticAnalysis\Attributes\Method constructor expects string, int given.' => 8,
34+
'Attribute class PhpStaticAnalysis\Attributes\Method does not have the method target.' => 13,
35+
'Call to an undefined method test\PhpStaticAnalysis\PHPStanExtension\data\Method\InvalidClassMethodAttribute::badFunction().' => 31,
36+
];
37+
} else {
38+
$expectedErrors = [
39+
'Parameter #1 ...$methods of attribute class PhpStaticAnalysis\Attributes\Method constructor expects string, int given.' => 8,
40+
'PHPDoc tag @method has invalid value (): Unexpected token "\n * ", expected type at offset 14 on line 2' => 9,
41+
'PHPDoc tag @method has invalid value (string): Unexpected token "\n * ", expected \'(\' at offset 32 on line 3' => 10,
42+
'Attribute class PhpStaticAnalysis\Attributes\Method does not have the method target.' => 13,
43+
'Call to an undefined method test\PhpStaticAnalysis\PHPStanExtension\data\Method\InvalidClassMethodAttribute::badFunction().' => 31,
44+
];
45+
}
3646

3747
$this->checkExpectedErrors($errors, $expectedErrors);
3848
}

Diff for: tests/MixinAttributeTest.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,21 @@ public function testInvalidClassMixinAttribute(): void
2626
{
2727
$errors = $this->analyse(__DIR__ . '/data/Mixin/InvalidClassMixinAttribute.php');
2828

29-
$expectedErrors = [
30-
'PHPDoc tag @mixin contains unknown class test\PhpStaticAnalysis\PHPStanExtension\data\Mixin\count.' => 7,
31-
'PHPDoc tag @mixin has invalid value (): Unexpected token "\n * ", expected type at offset 13' => 7,
32-
'Parameter #1 ...$classes of attribute class PhpStaticAnalysis\Attributes\Mixin constructor expects string, int given.' => 7,
33-
'Attribute class PhpStaticAnalysis\Attributes\Mixin does not have the method target.' => 11,
34-
];
29+
if (self::getPhpStanVersion() < '2') {
30+
$expectedErrors = [
31+
'PHPDoc tag @mixin contains unknown class test\PhpStaticAnalysis\PHPStanExtension\data\Mixin\count.' => 7,
32+
'PHPDoc tag @mixin has invalid value (): Unexpected token "\n * ", expected type at offset 13' => 7,
33+
'Parameter #1 ...$classes of attribute class PhpStaticAnalysis\Attributes\Mixin constructor expects string, int given.' => 7,
34+
'Attribute class PhpStaticAnalysis\Attributes\Mixin does not have the method target.' => 11,
35+
];
36+
} else {
37+
$expectedErrors = [
38+
'Parameter #1 ...$classes of attribute class PhpStaticAnalysis\Attributes\Mixin constructor expects string, int given.' => 7,
39+
'PHPDoc tag @mixin has invalid value (): Unexpected token "\n * ", expected type at offset 13 on line 2' => 8,
40+
'PHPDoc tag @mixin has invalid value (count($a)): Unexpected token "(", expected TOKEN_HORIZONTAL_WS at offset 29 on line 3' => 9,
41+
'Attribute class PhpStaticAnalysis\Attributes\Mixin does not have the method target.' => 11,
42+
];
43+
}
3544

3645
$this->checkExpectedErrors($errors, $expectedErrors);
3746
}

Diff for: tests/ParamAttributeTest.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ public function testInvalidMethodParamAttribute(): void
2020
{
2121
$errors = $this->analyse(__DIR__ . '/data/Param/InvalidMethodParamAttribute.php');
2222

23-
$expectedErrors = [
24-
'PHPDoc tag @param has invalid value (): Unexpected token "\n ", expected type at offset 13' => 9,
25-
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Param constructor expects string, int given.' => 9,
26-
'PHPDoc tag @param has invalid value (string): Unexpected token "\n ", expected variable at offset 20' => 15,
27-
'PHPDoc tag @param has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 19' => 21,
28-
'Attribute class PhpStaticAnalysis\Attributes\Param does not have the property target.' => 27,
29-
];
23+
if (self::getPhpStanVersion() < '2') {
24+
$expectedErrors = [
25+
'PHPDoc tag @param has invalid value (): Unexpected token "\n ", expected type at offset 13' => 9,
26+
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Param constructor expects string, int given.' => 9,
27+
'PHPDoc tag @param has invalid value (string): Unexpected token "\n ", expected variable at offset 20' => 15,
28+
'PHPDoc tag @param has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 19' => 21,
29+
'Attribute class PhpStaticAnalysis\Attributes\Param does not have the property target.' => 27,
30+
];
31+
} else {
32+
$expectedErrors = [
33+
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Param constructor expects string, int given.' => 9,
34+
'PHPDoc tag @param has invalid value (): Unexpected token "\n ", expected type at offset 13 on line 2' => 10,
35+
'PHPDoc tag @param has invalid value (string): Unexpected token "\n ", expected variable at offset 20 on line 2' => 16,
36+
'PHPDoc tag @param has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 19 on line 2' => 22,
37+
'Attribute class PhpStaticAnalysis\Attributes\Param does not have the property target.' => 27,
38+
];
39+
}
3040

3141
$this->checkExpectedErrors($errors, $expectedErrors);
3242
}

Diff for: tests/ParamOutAttributeTest.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ public function testInvalidMethodParamOutAttribute(): void
2020
{
2121
$errors = $this->analyse(__DIR__ . '/data/ParamOut/InvalidMethodParamOutAttribute.php');
2222

23-
$expectedErrors = [
24-
'PHPDoc tag @param-out has invalid value (): Unexpected token "\n ", expected type at offset 17' => 9,
25-
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\ParamOut constructor expects string, int given.' => 9,
26-
'PHPDoc tag @param-out has invalid value (string): Unexpected token "\n ", expected variable at offset 24' => 15,
27-
'PHPDoc tag @param-out has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 23' => 21,
28-
'Attribute class PhpStaticAnalysis\Attributes\ParamOut does not have the property target.' => 27,
29-
];
23+
if (self::getPhpStanVersion() < '2') {
24+
$expectedErrors = [
25+
'PHPDoc tag @param-out has invalid value (): Unexpected token "\n ", expected type at offset 17' => 9,
26+
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\ParamOut constructor expects string, int given.' => 9,
27+
'PHPDoc tag @param-out has invalid value (string): Unexpected token "\n ", expected variable at offset 24' => 15,
28+
'PHPDoc tag @param-out has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 23' => 21,
29+
'Attribute class PhpStaticAnalysis\Attributes\ParamOut does not have the property target.' => 27,
30+
];
31+
} else {
32+
$expectedErrors = [
33+
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\ParamOut constructor expects string, int given.' => 9,
34+
'PHPDoc tag @param-out has invalid value (): Unexpected token "\n ", expected type at offset 17 on line 2' => 10,
35+
'PHPDoc tag @param-out has invalid value (string): Unexpected token "\n ", expected variable at offset 24 on line 2' => 16,
36+
'PHPDoc tag @param-out has invalid value (count($a) $name): Unexpected token "(", expected variable at offset 23 on line 2' => 22,
37+
'Attribute class PhpStaticAnalysis\Attributes\ParamOut does not have the property target.' => 27,
38+
];
39+
}
3040

3141
$this->checkExpectedErrors($errors, $expectedErrors);
3242
}

0 commit comments

Comments
 (0)