From a686670c5635df248d66dd784034639a45275524 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Thu, 22 Feb 2024 08:18:41 +0100 Subject: [PATCH] Add Internal attribute --- README.md | 1 + composer.json | 4 +- src/Provider/AttributeStatementProvider.php | 2 +- tests/InternalAttributeTest.php | 59 +++++++++++++++++++ .../data/Internal/ClassInternalAttribute.php | 38 ++++++++++++ .../Internal/FunctionInternalAttribute.php | 10 ++++ .../Internal/InterfaceInternalAttribute.php | 10 ++++ .../InvalidMethodInternalAttribute.php | 26 ++++++++ .../data/Internal/MethodInternalAttribute.php | 28 +++++++++ .../Internal/PropertyInternalAttribute.php | 14 +++++ .../data/Internal/TraitInternalAttribute.php | 10 ++++ 11 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 tests/InternalAttributeTest.php create mode 100644 tests/data/Internal/ClassInternalAttribute.php create mode 100644 tests/data/Internal/FunctionInternalAttribute.php create mode 100644 tests/data/Internal/InterfaceInternalAttribute.php create mode 100644 tests/data/Internal/InvalidMethodInternalAttribute.php create mode 100644 tests/data/Internal/MethodInternalAttribute.php create mode 100644 tests/data/Internal/PropertyInternalAttribute.php create mode 100644 tests/data/Internal/TraitInternalAttribute.php diff --git a/README.md b/README.md index b6738a4..d7892e3 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,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 691a196..ea9ea58 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ "require": { "php": ">=8.0", "ext-simplexml": "*", - "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", "vimeo/psalm": "^5" }, "require-dev": { diff --git a/src/Provider/AttributeStatementProvider.php b/src/Provider/AttributeStatementProvider.php index b33341b..364d638 100644 --- a/src/Provider/AttributeStatementProvider.php +++ b/src/Provider/AttributeStatementProvider.php @@ -67,7 +67,7 @@ public function __set(string $property, mixed $value) private function traverseAst(array $ast): array { $traverser = new NodeTraverser(); - $nodeVisitor = new AttributeNodeVisitor(); + $nodeVisitor = new AttributeNodeVisitor('psalm'); $traverser->addVisitor($nodeVisitor); $ast = $traverser->traverse($ast); diff --git a/tests/InternalAttributeTest.php b/tests/InternalAttributeTest.php new file mode 100644 index 0000000..a575e0e --- /dev/null +++ b/tests/InternalAttributeTest.php @@ -0,0 +1,59 @@ +analyzeTestFile('/data/Internal/ClassInternalAttribute.php'); + $expectedErrors = [ + 'test\PhpStaticAnalysis\PsalmPlugin\data\Internal\ClassInternalAttribute is internal to newNamespace\test but called from newNamespace\other\otherClass' => 34, + 'The method test\PhpStaticAnalysis\PsalmPlugin\data\Internal\ClassInternalAttribute::myFunction is internal to newNamespace\test and test but called from newNamespace\other\otherClass::otherFunction' => 36, + ]; + $this->checkExpectedErrors($errors, $expectedErrors); + } + + public function testTraitInternalAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Internal/TraitInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInterfaceInternalAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Internal/InterfaceInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testMethodInternalAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Internal/MethodInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testFunctionInternalAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Internal/FunctionInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testProperyInternalAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Internal/PropertyInternalAttribute.php'); + $this->assertCount(0, $errors); + } + + public function testInvalidMethodInternalAttribute(): void + { + $errors = $this->analyzeTestFile('/data/Internal/InvalidMethodInternalAttribute.php'); + $expectedErrors = [ + 'psalm-internal annotation used without specifying namespace in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\Internal\InvalidMethodInternalAttribute::getName' => 9, + 'Argument 1 of PhpStaticAnalysis\Attributes\Internal::__construct expects null|string, but 0 provided' => 9, + 'Attribute Internal cannot be used on a function/method parameter' => 15, + 'Attribute Internal is not repeatable' => 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..39d4c09 --- /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\PsalmPlugin\data\Internal\ClassInternalAttribute(); + + $class->myFunction(); + } +} \ No newline at end of file diff --git a/tests/data/Internal/FunctionInternalAttribute.php b/tests/data/Internal/FunctionInternalAttribute.php new file mode 100644 index 0000000..8b83cc6 --- /dev/null +++ b/tests/data/Internal/FunctionInternalAttribute.php @@ -0,0 +1,10 @@ +