diff --git a/README.md b/README.md index 115f5fd..d5551d1 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ These are the available attributes and their corresponding PHPDoc annotations: | Attribute | PHPDoc Annotations | |-------------------------------------------------------------------------------------------------------------------|---------------------------| | [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` | +| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` | | [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` | | [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` | | [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` | diff --git a/composer.json b/composer.json index a2bebf0..697320b 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,8 @@ "prefer-stable": true, "require": { "php": ">=8.0", - "php-static-analysis/attributes": "^0.1.8 || dev-main", - "php-static-analysis/node-visitor": "^0.1.8 || dev-main", + "php-static-analysis/attributes": "^0.1.9 || dev-main", + "php-static-analysis/node-visitor": "^0.1.9 || dev-main", "phpstan/phpstan": "^1.8" }, "require-dev": { diff --git a/src/Parser/AttributeParser.php b/src/Parser/AttributeParser.php index 7b49a23..65119e6 100644 --- a/src/Parser/AttributeParser.php +++ b/src/Parser/AttributeParser.php @@ -35,7 +35,7 @@ public function parseString(string $sourceCode): array private function traverseAst(array $ast): array { $traverser = new NodeTraverser(); - $nodeVisitor = new AttributeNodeVisitor(); + $nodeVisitor = new AttributeNodeVisitor('phpstan'); $traverser->addVisitor($nodeVisitor); $ast = $traverser->traverse($ast); diff --git a/tests/InternalAttributeTest.php b/tests/InternalAttributeTest.php new file mode 100644 index 0000000..b1158d3 --- /dev/null +++ b/tests/InternalAttributeTest.php @@ -0,0 +1,54 @@ +analyse(__DIR__ . '/data/Internal/ClassInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testTraitInternalAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Internal/TraitInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceInternalAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Internal/InterfaceInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testMethodInternalAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Internal/MethodInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testFunctionInternalAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Internal/FunctionInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testProperyInternalAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Internal/PropertyInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodInternalAttribute(): void + { + $errors = $this->analyse(__DIR__ . '/data/Internal/InvalidMethodInternalAttribute.php'); + $expectedErrors = [ + 'Parameter #1 $namespace of attribute class PhpStaticAnalysis\Attributes\Internal constructor expects string|null, int given.' => 9, + 'Attribute class PhpStaticAnalysis\Attributes\Internal does not have the parameter target.' => 15, + 'Attribute class PhpStaticAnalysis\Attributes\Internal is not repeatable but is already present above the method.' => 22, + ]; + + $this->checkExpectedErrors($errors, $expectedErrors); + } +} diff --git a/tests/data/Internal/ClassInternalAttribute.php b/tests/data/Internal/ClassInternalAttribute.php new file mode 100644 index 0000000..6619622 --- /dev/null +++ b/tests/data/Internal/ClassInternalAttribute.php @@ -0,0 +1,38 @@ +myFunction(); + } +} + +namespace newNamespace\other; + +class otherClass +{ + public function otherFunction(): void + { + $class = new \test\PhpStaticAnalysis\PHPStanExtension\data\Internal\ClassInternalAttribute(); + + $class->myFunction(); + } +} diff --git a/tests/data/Internal/FunctionInternalAttribute.php b/tests/data/Internal/FunctionInternalAttribute.php new file mode 100644 index 0000000..04d8405 --- /dev/null +++ b/tests/data/Internal/FunctionInternalAttribute.php @@ -0,0 +1,10 @@ +