Skip to content

Commit

Permalink
Update to PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Dec 23, 2024
1 parent 919f57c commit b092beb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/all_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
fail-fast: false
matrix:
php-version:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
- "8.4"

steps:
- name: "Checkout"
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.0",
"nikic/php-parser": "^4 || ^5",
"php-static-analysis/attributes": "^0.3.1 || dev-main"
"php": ">=8.1",
"nikic/php-parser": "^5",
"php-static-analysis/attributes": "^0.4.0 || dev-main"
},
"require-dev": {
"php-static-analysis/phpstan-extension": "dev-main",
"php-static-analysis/psalm-plugin": "dev-main",
"php-static-analysis/phpstan-extension": "^0.4.0 || dev-main",
"php-static-analysis/psalm-plugin": "^0.4.0 || dev-main",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^9.0",
"psalm/plugin-phpunit": "^0.18.4",
"symplify/easy-coding-standard": "^12.1",
"vimeo/psalm": "^5"
"vimeo/psalm": "dev-master"
},
"scripts": {
"phpstan": "phpstan analyse",
Expand All @@ -52,7 +52,8 @@
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"php-static-analysis/psalm-plugin": true
},
"sort-packages": true
}
Expand Down
39 changes: 29 additions & 10 deletions src/AttributeNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
private const ARGS_TWO_WITH_TYPE = 'two with type';
private const ARGS_MANY_IN_USE = "many in use";
private const ARGS_MANY_WITH_NAME = "many with name";
private const ARGS_MANY_WITH_NAME_WITH_PREFIX = "many with name with prefix";
private const ARGS_MANY_IN_TYPE = "many in type";
private const ARGS_MANY_WITHOUT_NAME = "many without name";
private const ARGS_MANY_WITHOUT_NAME_AND_PREFIX = "many without name and prexif";
Expand Down Expand Up @@ -246,6 +247,8 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
],
Property::class => [
Stmt\Class_::class => 'property',
Stmt\Interface_::class => 'property',
Stmt\Trait_::class => 'property',
Stmt\Property::class => 'var',
],
PropertyRead::class => [
Expand Down Expand Up @@ -301,13 +304,13 @@ class AttributeNodeVisitor extends NodeVisitorAbstract

private const ARGUMENTS_PER_ATTRIBUTE = [
Assert::class => [
'all' => self::ARGS_MANY_WITH_NAME,
'all' => self::ARGS_MANY_WITH_NAME_WITH_PREFIX,
],
AssertIfFalse::class => [
'all' => self::ARGS_MANY_WITH_NAME,
'all' => self::ARGS_MANY_WITH_NAME_WITH_PREFIX,
],
AssertIfTrue::class => [
'all' => self::ARGS_MANY_WITH_NAME,
'all' => self::ARGS_MANY_WITH_NAME_WITH_PREFIX,
],
DefineType::class => [
'all' => self::ARGS_MANY_IN_TYPE,
Expand Down Expand Up @@ -344,6 +347,8 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
],
Property::class => [
Stmt\Class_::class => self::ARGS_MANY_WITH_NAME,
Stmt\Interface_::class => self::ARGS_MANY_WITH_NAME,
Stmt\Trait_::class => self::ARGS_MANY_WITH_NAME,
Stmt\Property::class => self::ARGS_ONE,
],
PropertyRead::class => [
Expand Down Expand Up @@ -425,9 +430,9 @@ public function enterNode(Node $node)

$this->initPositions();

foreach ($attributeGroups as $attributeGroup) {
foreach ($attributeGroups as $groupKey => $attributeGroup) {
$attributes = $attributeGroup->attrs;
foreach ($attributes as $attribute) {
foreach ($attributes as $key => $attribute) {
$attributeName = $attribute->name->toString();
$attributeName = self::SHORT_NAME_TO_FQN[$attributeName] ?? $attributeName;
if (
Expand Down Expand Up @@ -473,7 +478,7 @@ public function enterNode(Node $node)
$nodeType,
$attributeName,
$args[0],
prefix: $this->toolType === self::TOOL_PSALM ? $this->toolType : null
prefix: self::TOOL_PSALM
);
} else {
$tagsToAdd[] = $this->createTag($nodeType, $attributeName);
Expand All @@ -492,6 +497,12 @@ public function enterNode(Node $node)
$tagCreated = true;
}
break;
case self::ARGS_MANY_WITH_NAME_WITH_PREFIX:
foreach ($args as $arg) {
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, $arg, useName: true, prefix: $this->toolType);
$tagCreated = true;
}
break;
case self::ARGS_MANY_WITHOUT_NAME:
foreach ($args as $arg) {
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, $arg);
Expand All @@ -509,6 +520,7 @@ public function enterNode(Node $node)
if ($arg->value instanceof String_) {
$useValue = $arg->value->value;
$useTagsToAdd[$useValue] = $this->createTag($nodeType, $attributeName, $arg);
$tagCreated = true;
}
}
break;
Expand All @@ -521,10 +533,17 @@ public function enterNode(Node $node)
}
if ($tagCreated) {
$this->updatePositions($attribute);
unset($attributes[$key]);
}
}
}
if ($attributes === []) {
unset($attributeGroups[$groupKey]);
} else {
$attributeGroup->attrs = $attributes;
}
}
$node->attrGroups = $attributeGroups;
if ($node instanceof Stmt\ClassMethod || $node instanceof Stmt\Function_) {
$tagsToAdd = array_merge($tagsToAdd, $this->getParamTagsFromParams($node));
}
Expand All @@ -541,11 +560,11 @@ public function enterNode(Node $node)
private function createTag(
string $nodeType,
string $attributeName,
Arg $argument = null,
Arg $of = null,
?Arg $argument = null,
?Arg $of = null,
bool $useName = false,
string $nameToUse = null,
string $prefix = null,
?string $nameToUse = null,
?string $prefix = null,
bool $prefixWithName = false
): string {
if (array_key_exists($nodeType, self::ANNOTATION_PER_ATTRIBUTE[$attributeName])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/InternalAttributeNodeVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testAddsInternalPHPDocWithNamespace(): void
$this->addInternalAttributeToNode($node, true);
$this->nodeVisitor->enterNode($node);
$docText = $this->getDocText($node);
$this->assertEquals("/**\n * @internal A\B\n */", $docText);
$this->assertEquals("/**\n * @psalm-internal A\B\n */", $docText);
}

public function testDoesNotAddToolPrefixToAnnotationIfNotPsalm(): void
Expand Down

0 comments on commit b092beb

Please sign in to comment.