Skip to content

Commit

Permalink
Add Immutable attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 28, 2024
1 parent e16bdc9 commit 176cc97
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": ">=8.0",
"nikic/php-parser": "^4 || ^5",
"php-static-analysis/attributes": "^0.1.16 || dev-main"
"php-static-analysis/attributes": "^0.1.17 || dev-main"
},
"require-dev": {
"php-static-analysis/phpstan-extension": "dev-main",
Expand Down
11 changes: 11 additions & 0 deletions src/AttributeNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
use PhpStaticAnalysis\Attributes\Deprecated;
use PhpStaticAnalysis\Attributes\Immutable;
use PhpStaticAnalysis\Attributes\Impure;
use PhpStaticAnalysis\Attributes\Internal;
use PhpStaticAnalysis\Attributes\IsReadOnly;
Expand Down Expand Up @@ -65,6 +66,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
private const ALLOWED_ATTRIBUTES_PER_NODE_TYPE = [
Stmt\Class_::class => [
Deprecated::class,
Immutable::class,
Internal::class,
Method::class,
Mixin::class,
Expand Down Expand Up @@ -110,6 +112,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
],
Stmt\Interface_::class => [
Deprecated::class,
Immutable::class,
Internal::class,
Method::class,
Mixin::class,
Expand All @@ -129,6 +132,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
],
Stmt\Trait_::class => [
Deprecated::class,
Immutable::class,
Internal::class,
Method::class,
Mixin::class,
Expand All @@ -145,6 +149,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract

private const SHORT_NAME_TO_FQN = [
'Deprecated' => Deprecated::class,
'Immutable' => Immutable::class,
'Impure' => Impure::class,
'Internal' => Internal::class,
'IsReadOnly' => IsReadOnly::class,
Expand Down Expand Up @@ -174,6 +179,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
Deprecated::class => [
'all' => 'deprecated',
],
Immutable::class => [
'all' => 'immutable',
],
Impure::class => [
'all' => 'impure',
],
Expand Down Expand Up @@ -253,6 +261,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
Deprecated::class => [
'all' => self::ARGS_NONE,
],
Immutable::class => [
'all' => self::ARGS_NONE_WITH_PREFIX,
],
Impure::class => [
'all' => self::ARGS_NONE_WITH_PREFIX,
],
Expand Down
28 changes: 28 additions & 0 deletions tests/ImmutableAttributeNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use PhpStaticAnalysis\Attributes\Immutable;
use test\PhpStaticAnalysis\NodeVisitor\AttributeNodeVisitorTestBase;

class ImmutableAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
{
public function testAddsImmutablePHPDoc(): void
{
$node = new Node\Stmt\Class_('Test');
$this->addImmutableAttributeToNode($node);
$this->nodeVisitor->enterNode($node);
$docText = $this->getDocText($node);
$this->assertEquals("/**\n * @immutable\n */", $docText);
}

private function addImmutableAttributeToNode(Node\Stmt\Class_ $node): void
{
$attributeName = new FullyQualified(Immutable::class);
$attribute = new Attribute($attributeName);
$node->attrGroups = [new AttributeGroup([$attribute])];
}
}

0 comments on commit 176cc97

Please sign in to comment.