Skip to content

Commit cae6644

Browse files
Add Pure and Impure attributes
1 parent 1eaf5e0 commit cae6644

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require": {
2626
"php": ">=8.0",
2727
"nikic/php-parser": "^4 || ^5",
28-
"php-static-analysis/attributes": "^0.1.14 || dev-main"
28+
"php-static-analysis/attributes": "^0.1.15 || dev-main"
2929
},
3030
"require-dev": {
3131
"php-static-analysis/phpstan-extension": "dev-main",

src/AttributeNodeVisitor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Stmt;
1313
use PhpParser\NodeVisitorAbstract;
1414
use PhpStaticAnalysis\Attributes\Deprecated;
15+
use PhpStaticAnalysis\Attributes\Impure;
1516
use PhpStaticAnalysis\Attributes\Internal;
1617
use PhpStaticAnalysis\Attributes\IsReadOnly;
1718
use PhpStaticAnalysis\Attributes\Method;
@@ -21,6 +22,7 @@
2122
use PhpStaticAnalysis\Attributes\Property;
2223
use PhpStaticAnalysis\Attributes\PropertyRead;
2324
use PhpStaticAnalysis\Attributes\PropertyWrite;
25+
use PhpStaticAnalysis\Attributes\Pure;
2426
use PhpStaticAnalysis\Attributes\RequireExtends;
2527
use PhpStaticAnalysis\Attributes\RequireImplements;
2628
use PhpStaticAnalysis\Attributes\Returns;
@@ -36,6 +38,7 @@
3638
class AttributeNodeVisitor extends NodeVisitorAbstract
3739
{
3840
private const ARGS_NONE = 'none';
41+
private const ARGS_NONE_WITH_PREFIX = 'none with prefix';
3942
private const ARGS_ONE = 'one';
4043
private const ARGS_ONE_OPTIONAL = 'one optional';
4144
private const ARGS_ONE_WITH_PREFIX = 'one with prefix';
@@ -78,19 +81,23 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
7881
],
7982
Stmt\ClassMethod::class => [
8083
Deprecated::class,
84+
Impure::class,
8185
Internal::class,
8286
Param::class,
8387
ParamOut::class,
88+
Pure::class,
8489
Returns::class,
8590
SelfOut::class,
8691
Template::class,
8792
Type::class,
8893
],
8994
Stmt\Function_::class => [
9095
Deprecated::class,
96+
Impure::class,
9197
Internal::class,
9298
Param::class,
9399
ParamOut::class,
100+
Pure::class,
94101
Returns::class,
95102
Template::class,
96103
Type::class,
@@ -132,6 +139,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
132139

133140
private const SHORT_NAME_TO_FQN = [
134141
'Deprecated' => Deprecated::class,
142+
'Impure' => Impure::class,
135143
'Internal' => Internal::class,
136144
'IsReadOnly' => IsReadOnly::class,
137145
'Method' => Method::class,
@@ -141,6 +149,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
141149
'Property' => Property::class,
142150
'PropertyRead' => PropertyRead::class,
143151
'PropertyWrite' => PropertyWrite::class,
152+
'Pure' => Pure::class,
144153
'RequireExtends' => RequireExtends::class,
145154
'RequireImplements' => RequireImplements::class,
146155
'Returns' => Returns::class,
@@ -158,6 +167,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
158167
Deprecated::class => [
159168
'all' => 'deprecated',
160169
],
170+
Impure::class => [
171+
'all' => 'impure',
172+
],
161173
Internal::class => [
162174
'all' => 'internal',
163175
],
@@ -186,6 +198,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
186198
PropertyWrite::class => [
187199
'all' => 'property-write',
188200
],
201+
Pure::class => [
202+
'all' => 'pure',
203+
],
189204
RequireExtends::class => [
190205
'all' => 'require-extends',
191206
],
@@ -228,6 +243,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
228243
Deprecated::class => [
229244
'all' => self::ARGS_NONE,
230245
],
246+
Impure::class => [
247+
'all' => self::ARGS_NONE_WITH_PREFIX,
248+
],
231249
Internal::class => [
232250
'all' => self::ARGS_ONE_OPTIONAL,
233251
],
@@ -256,6 +274,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
256274
PropertyWrite::class => [
257275
'all' => self::ARGS_MANY_WITH_NAME,
258276
],
277+
Pure::class => [
278+
'all' => self::ARGS_NONE_WITH_PREFIX,
279+
],
259280
RequireExtends::class => [
260281
'all' => self::ARGS_ONE_WITH_PREFIX,
261282
],
@@ -341,6 +362,10 @@ public function enterNode(Node $node)
341362
$tagsToAdd[] = $this->createTag($nodeType, $attributeName);
342363
$tagCreated = true;
343364
break;
365+
case self::ARGS_NONE_WITH_PREFIX:
366+
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, prefix: $this->toolType);
367+
$tagCreated = true;
368+
break;
344369
case self::ARGS_ONE:
345370
if (isset($args[0])) {
346371
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, $args[0]);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\NodeVisitor;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Attribute;
7+
use PhpParser\Node\AttributeGroup;
8+
use PhpParser\Node\Name\FullyQualified;
9+
use PhpStaticAnalysis\Attributes\Impure;
10+
11+
class ImpureAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
12+
{
13+
public function testAddsImpurePHPDoc(): void
14+
{
15+
$node = new Node\Stmt\ClassMethod('Test');
16+
$this->addImpureAttributeToNode($node);
17+
$this->nodeVisitor->enterNode($node);
18+
$docText = $this->getDocText($node);
19+
$this->assertEquals("/**\n * @impure\n */", $docText);
20+
}
21+
22+
private function addImpureAttributeToNode(Node\Stmt\ClassMethod $node): void
23+
{
24+
$attributeName = new FullyQualified(Impure::class);
25+
$attribute = new Attribute($attributeName);
26+
$node->attrGroups = [new AttributeGroup([$attribute])];
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\NodeVisitor;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Attribute;
7+
use PhpParser\Node\AttributeGroup;
8+
use PhpParser\Node\Name\FullyQualified;
9+
use PhpStaticAnalysis\Attributes\Pure;
10+
11+
class PureAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
12+
{
13+
public function testAddsPurePHPDoc(): void
14+
{
15+
$node = new Node\Stmt\ClassMethod('Test');
16+
$this->addPureAttributeToNode($node);
17+
$this->nodeVisitor->enterNode($node);
18+
$docText = $this->getDocText($node);
19+
$this->assertEquals("/**\n * @pure\n */", $docText);
20+
}
21+
22+
private function addPureAttributeToNode(Node\Stmt\ClassMethod $node): void
23+
{
24+
$attributeName = new FullyQualified(Pure::class);
25+
$attribute = new Attribute($attributeName);
26+
$node->attrGroups = [new AttributeGroup([$attribute])];
27+
}
28+
}

0 commit comments

Comments
 (0)