Skip to content

Commit 176cc97

Browse files
Add Immutable attribute
1 parent e16bdc9 commit 176cc97

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-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.16 || dev-main"
28+
"php-static-analysis/attributes": "^0.1.17 || dev-main"
2929
},
3030
"require-dev": {
3131
"php-static-analysis/phpstan-extension": "dev-main",

src/AttributeNodeVisitor.php

Lines changed: 11 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\Immutable;
1516
use PhpStaticAnalysis\Attributes\Impure;
1617
use PhpStaticAnalysis\Attributes\Internal;
1718
use PhpStaticAnalysis\Attributes\IsReadOnly;
@@ -65,6 +66,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
6566
private const ALLOWED_ATTRIBUTES_PER_NODE_TYPE = [
6667
Stmt\Class_::class => [
6768
Deprecated::class,
69+
Immutable::class,
6870
Internal::class,
6971
Method::class,
7072
Mixin::class,
@@ -110,6 +112,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
110112
],
111113
Stmt\Interface_::class => [
112114
Deprecated::class,
115+
Immutable::class,
113116
Internal::class,
114117
Method::class,
115118
Mixin::class,
@@ -129,6 +132,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
129132
],
130133
Stmt\Trait_::class => [
131134
Deprecated::class,
135+
Immutable::class,
132136
Internal::class,
133137
Method::class,
134138
Mixin::class,
@@ -145,6 +149,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
145149

146150
private const SHORT_NAME_TO_FQN = [
147151
'Deprecated' => Deprecated::class,
152+
'Immutable' => Immutable::class,
148153
'Impure' => Impure::class,
149154
'Internal' => Internal::class,
150155
'IsReadOnly' => IsReadOnly::class,
@@ -174,6 +179,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
174179
Deprecated::class => [
175180
'all' => 'deprecated',
176181
],
182+
Immutable::class => [
183+
'all' => 'immutable',
184+
],
177185
Impure::class => [
178186
'all' => 'impure',
179187
],
@@ -253,6 +261,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
253261
Deprecated::class => [
254262
'all' => self::ARGS_NONE,
255263
],
264+
Immutable::class => [
265+
'all' => self::ARGS_NONE_WITH_PREFIX,
266+
],
256267
Impure::class => [
257268
'all' => self::ARGS_NONE_WITH_PREFIX,
258269
],
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
4+
use PhpParser\Node;
5+
use PhpParser\Node\Attribute;
6+
use PhpParser\Node\AttributeGroup;
7+
use PhpParser\Node\Name\FullyQualified;
8+
use PhpStaticAnalysis\Attributes\Immutable;
9+
use test\PhpStaticAnalysis\NodeVisitor\AttributeNodeVisitorTestBase;
10+
11+
class ImmutableAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
12+
{
13+
public function testAddsImmutablePHPDoc(): void
14+
{
15+
$node = new Node\Stmt\Class_('Test');
16+
$this->addImmutableAttributeToNode($node);
17+
$this->nodeVisitor->enterNode($node);
18+
$docText = $this->getDocText($node);
19+
$this->assertEquals("/**\n * @immutable\n */", $docText);
20+
}
21+
22+
private function addImmutableAttributeToNode(Node\Stmt\Class_ $node): void
23+
{
24+
$attributeName = new FullyQualified(Immutable::class);
25+
$attribute = new Attribute($attributeName);
26+
$node->attrGroups = [new AttributeGroup([$attribute])];
27+
}
28+
}

0 commit comments

Comments
 (0)