Skip to content

Commit

Permalink
Add Deprecated attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 21, 2024
1 parent 188df29 commit 56af0aa
Show file tree
Hide file tree
Showing 3 changed files with 45 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.7 || dev-main"
"php-static-analysis/attributes": "^0.1.8 || dev-main"
},
"require-dev": {
"php-static-analysis/phpstan-extension": "dev-main",
Expand Down
15 changes: 15 additions & 0 deletions src/AttributeNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
use PhpStaticAnalysis\Attributes\Deprecated;
use PhpStaticAnalysis\Attributes\IsReadOnly;
use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\Param;
Expand Down Expand Up @@ -43,6 +44,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract

private const ALLOWED_ATTRIBUTES_PER_NODE_TYPE = [
Stmt\Class_::class => [
Deprecated::class,
Method::class,
Property::class,
PropertyRead::class,
Expand All @@ -52,21 +54,25 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
TemplateCovariant::class,
],
Stmt\ClassConst::class => [
Deprecated::class,
Type::class,
],
Stmt\ClassMethod::class => [
Deprecated::class,
Param::class,
Returns::class,
Template::class,
Type::class,
],
Stmt\Function_::class => [
Deprecated::class,
Param::class,
Returns::class,
Template::class,
Type::class,
],
Stmt\Interface_::class => [
Deprecated::class,
Method::class,
Property::class,
PropertyRead::class,
Expand All @@ -76,11 +82,13 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
TemplateCovariant::class,
],
Stmt\Property::class => [
Deprecated::class,
IsReadOnly::class,
Property::class,
Type::class,
],
Stmt\Trait_::class => [
Deprecated::class,
Method::class,
Property::class,
PropertyRead::class,
Expand All @@ -92,6 +100,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
];

private const SHORT_NAME_TO_FQN = [
'Deprecated' => Deprecated::class,
'IsReadOnly' => IsReadOnly::class,
'Method' => Method::class,
'Param' => Param::class,
Expand All @@ -106,6 +115,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
];

private const ANNOTATION_PER_ATTRIBUTE = [
Deprecated::class => [
'all' => 'deprecated',
],
IsReadOnly::class => [
'all' => 'readonly',
],
Expand Down Expand Up @@ -146,6 +158,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
];

private const ARGUMENTS_PER_ATTRIBUTE = [
Deprecated::class => [
'all' => self::ARGS_NONE,
],
IsReadOnly::class => [
'all' => self::ARGS_NONE,
],
Expand Down
29 changes: 29 additions & 0 deletions tests/DeprecatedAttributeNodeVisitorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace test\PhpStaticAnalysis\NodeVisitor;

use Exception;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use PhpStaticAnalysis\Attributes\Deprecated;

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

private function addDeprecatedAttributeToNode(Node\Stmt\Class_ $node, bool $addType = false): void
{
$attributeName = new FullyQualified(Deprecated::class);
$attribute = new Attribute($attributeName);
$node->attrGroups = [new AttributeGroup([$attribute])];
}
}

0 comments on commit 56af0aa

Please sign in to comment.