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 dbf405d commit de0afce
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ These are the available attributes and their corresponding PHPDoc annotations:

| Attribute | PHPDoc Annotations |
|---------------------------------------------------------------------------------------------------|--------------------|
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=8.0",
"cweagans/composer-patches": "^1.7",
"php-static-analysis/attributes": "^0.1.7 || dev-main",
"php-static-analysis/attributes": "^0.1.8 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use PhpStaticAnalysis\Attributes\Deprecated;
use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
Expand All @@ -21,8 +22,9 @@
$rectorConfig->ruleWithConfiguration(
AnnotationsToAttributesRector::class,
[
new AnnotationToAttribute('param', Param::class),
new AnnotationToAttribute('deprecated', Deprecated::class),
new AnnotationToAttribute('method', Method::class),
new AnnotationToAttribute('param', Param::class),
new AnnotationToAttribute('property', Property::class),
new AnnotationToAttribute('property_read', PropertyRead::class),
new AnnotationToAttribute('property_write', PropertyWrite::class),
Expand Down
3 changes: 2 additions & 1 deletion src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace PhpStaticAnalysis\RectorRule;

use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand Down Expand Up @@ -273,6 +273,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
}
$attributeComment = $tagValueNode->description;
break;
case $tagValueNode instanceof DeprecatedTagValueNode:
case $tagValueNode instanceof GenericTagValueNode:
$args = [];
$attributeComment = (string)$tagValueNode;
Expand Down
133 changes: 133 additions & 0 deletions tests/Fixture/DeprecatedAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

/**
* @deprecated
*/
class DeprecatedAttributeTest
{
/**
* @deprecated
*/
public const NAME = 'name';

/**
* @deprecated
*/
public string $name = '';

/**
* @deprecated
*/
public function getName(): void
{
}

/**
* @codeCoverageIgnore
* @deprecated
*/
public function getMoreNames(): void
{
}

/**
* @deprecated
*/
#[Param(name:'string')]
public function getAnotherName($name)
{
return "Hello " . $name;
}

/**
* @deprecated use getNewUserName() instead
*/
public function getUserName(): void
{
}

/**
* @psalm-deprecated
*/
public function getPsalmName(): void
{
}

/**
* @phpstan-deprecated
*/
public function getPHPStanName(): void
{
}
}

/**
* @deprecated
*/
function getName(): void
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

#[\PhpStaticAnalysis\Attributes\Deprecated]
class DeprecatedAttributeTest
{
#[\PhpStaticAnalysis\Attributes\Deprecated]
public const NAME = 'name';

#[\PhpStaticAnalysis\Attributes\Deprecated]
public string $name = '';

#[\PhpStaticAnalysis\Attributes\Deprecated]
public function getName(): void
{
}

/**
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Deprecated]
public function getMoreNames(): void
{
}

#[Param(name:'string')]
#[\PhpStaticAnalysis\Attributes\Deprecated]
public function getAnotherName($name)
{
return "Hello " . $name;
}

#[\PhpStaticAnalysis\Attributes\Deprecated] // use getNewUserName() instead
public function getUserName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Deprecated]
public function getPsalmName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Deprecated]
public function getPHPStanName(): void
{
}
}

#[\PhpStaticAnalysis\Attributes\Deprecated]
function getName(): void
{
}

?>
4 changes: 2 additions & 2 deletions tests/Fixture/IsReadOnlyAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class IsReadOnlyAttributeTest
public $name;

/**
* @deprecated
* @codeCoverageIgnore
* @readonly
*/
public $anotherName;
Expand Down Expand Up @@ -53,7 +53,7 @@ class IsReadOnlyAttributeTest
public $name;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\IsReadOnly]
public $anotherName;
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/MethodAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
* @method string getString()
* @method string getName() used to get the name
* @psalm-method void setString(string $text)
Expand All @@ -25,7 +25,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\Method('string getString()')]
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/ParamAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ParamAttributeTest
}

/**
* @deprecated
* @codeCoverageIgnore
* @param string $name
*/
public function getMoreNames($name)
Expand Down Expand Up @@ -102,7 +102,7 @@ class ParamAttributeTest
}

/**
* @deprecated
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Param(name: 'string')]
public function getMoreNames($name)
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/PropertyAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
* @property string $name
* @property string $userName the name of the user
* @psalm-property int $num
Expand All @@ -25,7 +25,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\Property(name: 'string')]
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/PropertyReadAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
* @property-read string $name
* @property-read string $userName the name of the user
* @psalm-property-read int $num
Expand All @@ -25,7 +25,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\PropertyRead(name: 'string')]
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/PropertyWriteAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
* @property-write string $name
* @property-write string $userName the name of the user
* @psalm-property-write int $num
Expand All @@ -25,7 +25,7 @@ namespace test\PhpStaticAnalysis\RectorRule\Fixture;
use PhpStaticAnalysis\Attributes\Template;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[Template('T')]
#[\PhpStaticAnalysis\Attributes\PropertyWrite(name: 'string')]
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/ReturnsAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ReturnsAttributeTest
}

/**
* @deprecated
* @codeCoverageIgnore
* @return string
*/
public function getMoreNames()
Expand Down Expand Up @@ -82,7 +82,7 @@ class ReturnsAttributeTest
}

/**
* @deprecated
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Returns('string')]
public function getMoreNames()
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/TemplateAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TemplateAttributeTest
}

/**
* @deprecated
* @codeCoverageIgnore
* @template T
*/
public function getMoreNames()
Expand Down Expand Up @@ -108,7 +108,7 @@ class TemplateAttributeTest
}

/**
* @deprecated
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Template('T')]
public function getMoreNames()
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/TemplateContravariantAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
* @codeCoverageIgnore
* @template-contravariant T1
* @template-contravariant T2 of Exception
* @template-contravariant T3 this is contravariant
Expand All @@ -28,7 +28,7 @@ use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[Property(name:'string')]
#[\PhpStaticAnalysis\Attributes\TemplateContravariant('T1')]
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/TemplateCovariantAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
* @codeCoverageIgnore
* @template-covariant T1
* @template-covariant T2 of Exception
* @template-covariant T3 this is covariant
Expand All @@ -29,7 +29,7 @@ use Exception;
use PhpStaticAnalysis\Attributes\Property;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[Property(name:'string')]
#[\PhpStaticAnalysis\Attributes\TemplateCovariant('T1')]
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/TypeAttributeTest.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TypeAttributeTest
public $name;

/**
* @deprecated
* @codeCoverageIgnore
* @var string
*/
public $anotherName;
Expand Down Expand Up @@ -58,7 +58,7 @@ class TypeAttributeTest
public $name;

/**
* @deprecated
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Type('string')]
public $anotherName;
Expand Down
Loading

0 comments on commit de0afce

Please sign in to comment.