Skip to content

Commit b092beb

Browse files
Update to PHPStan 2.0
1 parent 919f57c commit b092beb

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

.github/workflows/all_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
php-version:
17-
- "8.0"
1817
- "8.1"
1918
- "8.2"
2019
- "8.3"
20+
- "8.4"
2121

2222
steps:
2323
- name: "Checkout"

composer.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@
2323
"minimum-stability": "dev",
2424
"prefer-stable": true,
2525
"require": {
26-
"php": ">=8.0",
27-
"nikic/php-parser": "^4 || ^5",
28-
"php-static-analysis/attributes": "^0.3.1 || dev-main"
26+
"php": ">=8.1",
27+
"nikic/php-parser": "^5",
28+
"php-static-analysis/attributes": "^0.4.0 || dev-main"
2929
},
3030
"require-dev": {
31-
"php-static-analysis/phpstan-extension": "dev-main",
32-
"php-static-analysis/psalm-plugin": "dev-main",
31+
"php-static-analysis/phpstan-extension": "^0.4.0 || dev-main",
32+
"php-static-analysis/psalm-plugin": "^0.4.0 || dev-main",
3333
"phpstan/extension-installer": "^1.3",
34-
"phpstan/phpstan": "^1.8",
34+
"phpstan/phpstan": "^2.0",
3535
"phpunit/phpunit": "^9.0",
3636
"psalm/plugin-phpunit": "^0.18.4",
3737
"symplify/easy-coding-standard": "^12.1",
38-
"vimeo/psalm": "^5"
38+
"vimeo/psalm": "dev-master"
3939
},
4040
"scripts": {
4141
"phpstan": "phpstan analyse",
@@ -52,7 +52,8 @@
5252
},
5353
"config": {
5454
"allow-plugins": {
55-
"phpstan/extension-installer": true
55+
"phpstan/extension-installer": true,
56+
"php-static-analysis/psalm-plugin": true
5657
},
5758
"sort-packages": true
5859
}

src/AttributeNodeVisitor.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
5555
private const ARGS_TWO_WITH_TYPE = 'two with type';
5656
private const ARGS_MANY_IN_USE = "many in use";
5757
private const ARGS_MANY_WITH_NAME = "many with name";
58+
private const ARGS_MANY_WITH_NAME_WITH_PREFIX = "many with name with prefix";
5859
private const ARGS_MANY_IN_TYPE = "many in type";
5960
private const ARGS_MANY_WITHOUT_NAME = "many without name";
6061
private const ARGS_MANY_WITHOUT_NAME_AND_PREFIX = "many without name and prexif";
@@ -246,6 +247,8 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
246247
],
247248
Property::class => [
248249
Stmt\Class_::class => 'property',
250+
Stmt\Interface_::class => 'property',
251+
Stmt\Trait_::class => 'property',
249252
Stmt\Property::class => 'var',
250253
],
251254
PropertyRead::class => [
@@ -301,13 +304,13 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
301304

302305
private const ARGUMENTS_PER_ATTRIBUTE = [
303306
Assert::class => [
304-
'all' => self::ARGS_MANY_WITH_NAME,
307+
'all' => self::ARGS_MANY_WITH_NAME_WITH_PREFIX,
305308
],
306309
AssertIfFalse::class => [
307-
'all' => self::ARGS_MANY_WITH_NAME,
310+
'all' => self::ARGS_MANY_WITH_NAME_WITH_PREFIX,
308311
],
309312
AssertIfTrue::class => [
310-
'all' => self::ARGS_MANY_WITH_NAME,
313+
'all' => self::ARGS_MANY_WITH_NAME_WITH_PREFIX,
311314
],
312315
DefineType::class => [
313316
'all' => self::ARGS_MANY_IN_TYPE,
@@ -344,6 +347,8 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
344347
],
345348
Property::class => [
346349
Stmt\Class_::class => self::ARGS_MANY_WITH_NAME,
350+
Stmt\Interface_::class => self::ARGS_MANY_WITH_NAME,
351+
Stmt\Trait_::class => self::ARGS_MANY_WITH_NAME,
347352
Stmt\Property::class => self::ARGS_ONE,
348353
],
349354
PropertyRead::class => [
@@ -425,9 +430,9 @@ public function enterNode(Node $node)
425430

426431
$this->initPositions();
427432

428-
foreach ($attributeGroups as $attributeGroup) {
433+
foreach ($attributeGroups as $groupKey => $attributeGroup) {
429434
$attributes = $attributeGroup->attrs;
430-
foreach ($attributes as $attribute) {
435+
foreach ($attributes as $key => $attribute) {
431436
$attributeName = $attribute->name->toString();
432437
$attributeName = self::SHORT_NAME_TO_FQN[$attributeName] ?? $attributeName;
433438
if (
@@ -473,7 +478,7 @@ public function enterNode(Node $node)
473478
$nodeType,
474479
$attributeName,
475480
$args[0],
476-
prefix: $this->toolType === self::TOOL_PSALM ? $this->toolType : null
481+
prefix: self::TOOL_PSALM
477482
);
478483
} else {
479484
$tagsToAdd[] = $this->createTag($nodeType, $attributeName);
@@ -492,6 +497,12 @@ public function enterNode(Node $node)
492497
$tagCreated = true;
493498
}
494499
break;
500+
case self::ARGS_MANY_WITH_NAME_WITH_PREFIX:
501+
foreach ($args as $arg) {
502+
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, $arg, useName: true, prefix: $this->toolType);
503+
$tagCreated = true;
504+
}
505+
break;
495506
case self::ARGS_MANY_WITHOUT_NAME:
496507
foreach ($args as $arg) {
497508
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, $arg);
@@ -509,6 +520,7 @@ public function enterNode(Node $node)
509520
if ($arg->value instanceof String_) {
510521
$useValue = $arg->value->value;
511522
$useTagsToAdd[$useValue] = $this->createTag($nodeType, $attributeName, $arg);
523+
$tagCreated = true;
512524
}
513525
}
514526
break;
@@ -521,10 +533,17 @@ public function enterNode(Node $node)
521533
}
522534
if ($tagCreated) {
523535
$this->updatePositions($attribute);
536+
unset($attributes[$key]);
524537
}
525538
}
526539
}
540+
if ($attributes === []) {
541+
unset($attributeGroups[$groupKey]);
542+
} else {
543+
$attributeGroup->attrs = $attributes;
544+
}
527545
}
546+
$node->attrGroups = $attributeGroups;
528547
if ($node instanceof Stmt\ClassMethod || $node instanceof Stmt\Function_) {
529548
$tagsToAdd = array_merge($tagsToAdd, $this->getParamTagsFromParams($node));
530549
}
@@ -541,11 +560,11 @@ public function enterNode(Node $node)
541560
private function createTag(
542561
string $nodeType,
543562
string $attributeName,
544-
Arg $argument = null,
545-
Arg $of = null,
563+
?Arg $argument = null,
564+
?Arg $of = null,
546565
bool $useName = false,
547-
string $nameToUse = null,
548-
string $prefix = null,
566+
?string $nameToUse = null,
567+
?string $prefix = null,
549568
bool $prefixWithName = false
550569
): string {
551570
if (array_key_exists($nodeType, self::ANNOTATION_PER_ATTRIBUTE[$attributeName])) {

tests/InternalAttributeNodeVisitorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testAddsInternalPHPDocWithNamespace(): void
2626
$this->addInternalAttributeToNode($node, true);
2727
$this->nodeVisitor->enterNode($node);
2828
$docText = $this->getDocText($node);
29-
$this->assertEquals("/**\n * @internal A\B\n */", $docText);
29+
$this->assertEquals("/**\n * @psalm-internal A\B\n */", $docText);
3030
}
3131

3232
public function testDoesNotAddToolPrefixToAnnotationIfNotPsalm(): void

0 commit comments

Comments
 (0)