Skip to content

Commit

Permalink
Add Pure and Impure attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 25, 2024
1 parent 1eaf5e0 commit cae6644
Show file tree
Hide file tree
Showing 4 changed files with 82 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.14 || dev-main"
"php-static-analysis/attributes": "^0.1.15 || dev-main"
},
"require-dev": {
"php-static-analysis/phpstan-extension": "dev-main",
Expand Down
25 changes: 25 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\Impure;
use PhpStaticAnalysis\Attributes\Internal;
use PhpStaticAnalysis\Attributes\IsReadOnly;
use PhpStaticAnalysis\Attributes\Method;
Expand All @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -78,19 +81,23 @@ 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,
Type::class,
],
Stmt\Function_::class => [
Deprecated::class,
Impure::class,
Internal::class,
Param::class,
ParamOut::class,
Pure::class,
Returns::class,
Template::class,
Type::class,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -158,6 +167,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
Deprecated::class => [
'all' => 'deprecated',
],
Impure::class => [
'all' => 'impure',
],
Internal::class => [
'all' => 'internal',
],
Expand Down Expand Up @@ -186,6 +198,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
PropertyWrite::class => [
'all' => 'property-write',
],
Pure::class => [
'all' => 'pure',
],
RequireExtends::class => [
'all' => 'require-extends',
],
Expand Down Expand Up @@ -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,
],
Expand Down Expand Up @@ -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,
],
Expand Down Expand Up @@ -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]);
Expand Down
28 changes: 28 additions & 0 deletions tests/ImpureAttributeNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace test\PhpStaticAnalysis\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use PhpStaticAnalysis\Attributes\Impure;

class ImpureAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
{
public function testAddsImpurePHPDoc(): void
{
$node = new Node\Stmt\ClassMethod('Test');
$this->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])];
}
}
28 changes: 28 additions & 0 deletions tests/PureAttributeNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace test\PhpStaticAnalysis\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use PhpStaticAnalysis\Attributes\Pure;

class PureAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
{
public function testAddsPurePHPDoc(): void
{
$node = new Node\Stmt\ClassMethod('Test');
$this->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])];
}
}

0 comments on commit cae6644

Please sign in to comment.