From cae6644acf18d84e367edd796e25f71edf01ba84 Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Sun, 25 Feb 2024 15:11:59 +0100 Subject: [PATCH] Add Pure and Impure attributes --- composer.json | 2 +- src/AttributeNodeVisitor.php | 25 +++++++++++++++++++++ tests/ImpureAttributeNodeVisitorTest.php | 28 ++++++++++++++++++++++++ tests/PureAttributeNodeVisitorTest.php | 28 ++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tests/ImpureAttributeNodeVisitorTest.php create mode 100644 tests/PureAttributeNodeVisitorTest.php diff --git a/composer.json b/composer.json index e82ae76..da3e0db 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "require": { "php": ">=8.0", "nikic/php-parser": "^4 || ^5", - "php-static-analysis/attributes": "^0.1.14 || dev-main" + "php-static-analysis/attributes": "^0.1.15 || dev-main" }, "require-dev": { "php-static-analysis/phpstan-extension": "dev-main", diff --git a/src/AttributeNodeVisitor.php b/src/AttributeNodeVisitor.php index 5e9fada..2762030 100644 --- a/src/AttributeNodeVisitor.php +++ b/src/AttributeNodeVisitor.php @@ -12,6 +12,7 @@ use PhpParser\Node\Stmt; use PhpParser\NodeVisitorAbstract; use PhpStaticAnalysis\Attributes\Deprecated; +use PhpStaticAnalysis\Attributes\Impure; use PhpStaticAnalysis\Attributes\Internal; use PhpStaticAnalysis\Attributes\IsReadOnly; use PhpStaticAnalysis\Attributes\Method; @@ -21,6 +22,7 @@ use PhpStaticAnalysis\Attributes\Property; use PhpStaticAnalysis\Attributes\PropertyRead; use PhpStaticAnalysis\Attributes\PropertyWrite; +use PhpStaticAnalysis\Attributes\Pure; use PhpStaticAnalysis\Attributes\RequireExtends; use PhpStaticAnalysis\Attributes\RequireImplements; use PhpStaticAnalysis\Attributes\Returns; @@ -36,6 +38,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract { private const ARGS_NONE = 'none'; + private const ARGS_NONE_WITH_PREFIX = 'none with prefix'; private const ARGS_ONE = 'one'; private const ARGS_ONE_OPTIONAL = 'one optional'; private const ARGS_ONE_WITH_PREFIX = 'one with prefix'; @@ -78,9 +81,11 @@ class AttributeNodeVisitor extends NodeVisitorAbstract ], Stmt\ClassMethod::class => [ Deprecated::class, + Impure::class, Internal::class, Param::class, ParamOut::class, + Pure::class, Returns::class, SelfOut::class, Template::class, @@ -88,9 +93,11 @@ class AttributeNodeVisitor extends NodeVisitorAbstract ], Stmt\Function_::class => [ Deprecated::class, + Impure::class, Internal::class, Param::class, ParamOut::class, + Pure::class, Returns::class, Template::class, Type::class, @@ -132,6 +139,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract private const SHORT_NAME_TO_FQN = [ 'Deprecated' => Deprecated::class, + 'Impure' => Impure::class, 'Internal' => Internal::class, 'IsReadOnly' => IsReadOnly::class, 'Method' => Method::class, @@ -141,6 +149,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract 'Property' => Property::class, 'PropertyRead' => PropertyRead::class, 'PropertyWrite' => PropertyWrite::class, + 'Pure' => Pure::class, 'RequireExtends' => RequireExtends::class, 'RequireImplements' => RequireImplements::class, 'Returns' => Returns::class, @@ -158,6 +167,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract Deprecated::class => [ 'all' => 'deprecated', ], + Impure::class => [ + 'all' => 'impure', + ], Internal::class => [ 'all' => 'internal', ], @@ -186,6 +198,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract PropertyWrite::class => [ 'all' => 'property-write', ], + Pure::class => [ + 'all' => 'pure', + ], RequireExtends::class => [ 'all' => 'require-extends', ], @@ -228,6 +243,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract Deprecated::class => [ 'all' => self::ARGS_NONE, ], + Impure::class => [ + 'all' => self::ARGS_NONE_WITH_PREFIX, + ], Internal::class => [ 'all' => self::ARGS_ONE_OPTIONAL, ], @@ -256,6 +274,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract PropertyWrite::class => [ 'all' => self::ARGS_MANY_WITH_NAME, ], + Pure::class => [ + 'all' => self::ARGS_NONE_WITH_PREFIX, + ], RequireExtends::class => [ 'all' => self::ARGS_ONE_WITH_PREFIX, ], @@ -341,6 +362,10 @@ public function enterNode(Node $node) $tagsToAdd[] = $this->createTag($nodeType, $attributeName); $tagCreated = true; break; + case self::ARGS_NONE_WITH_PREFIX: + $tagsToAdd[] = $this->createTag($nodeType, $attributeName, prefix: $this->toolType); + $tagCreated = true; + break; case self::ARGS_ONE: if (isset($args[0])) { $tagsToAdd[] = $this->createTag($nodeType, $attributeName, $args[0]); diff --git a/tests/ImpureAttributeNodeVisitorTest.php b/tests/ImpureAttributeNodeVisitorTest.php new file mode 100644 index 0000000..bc4dafc --- /dev/null +++ b/tests/ImpureAttributeNodeVisitorTest.php @@ -0,0 +1,28 @@ +addImpureAttributeToNode($node); + $this->nodeVisitor->enterNode($node); + $docText = $this->getDocText($node); + $this->assertEquals("/**\n * @impure\n */", $docText); + } + + private function addImpureAttributeToNode(Node\Stmt\ClassMethod $node): void + { + $attributeName = new FullyQualified(Impure::class); + $attribute = new Attribute($attributeName); + $node->attrGroups = [new AttributeGroup([$attribute])]; + } +} diff --git a/tests/PureAttributeNodeVisitorTest.php b/tests/PureAttributeNodeVisitorTest.php new file mode 100644 index 0000000..20d3c34 --- /dev/null +++ b/tests/PureAttributeNodeVisitorTest.php @@ -0,0 +1,28 @@ +addPureAttributeToNode($node); + $this->nodeVisitor->enterNode($node); + $docText = $this->getDocText($node); + $this->assertEquals("/**\n * @pure\n */", $docText); + } + + private function addPureAttributeToNode(Node\Stmt\ClassMethod $node): void + { + $attributeName = new FullyQualified(Pure::class); + $attribute = new Attribute($attributeName); + $node->attrGroups = [new AttributeGroup([$attribute])]; + } +}