-
-
Notifications
You must be signed in to change notification settings - Fork 393
[PHP84] Deprecated annotation to Deprecated attribute #6923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TomasVotruba
merged 11 commits into
rectorphp:main
from
peterfox:feature/dep-ann-to-dep-attr
May 28, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eef019f
working rule
peterfox aee21bd
fixes
peterfox 05d2330
improvements + tests
peterfox 09c99b5
clean up
peterfox 30aa94a
doc change
peterfox 7dafaeb
Update rules/Php84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttr…
peterfox 75264f7
changes
peterfox 5dec904
improvements on feedback
peterfox 11bf1ef
better link
peterfox fe65d0f
Update rules/Php84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttr…
peterfox 0f6e0ca
fixes
peterfox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...tationToDeprecatedAttributeRector/DeprecatedAnnotationToDeprecatedAttributeRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class DeprecatedAnnotationToDeprecatedAttributeRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...p84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttributeRector/Fixture/fixture.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
final class Fixture | ||
{ | ||
/** | ||
* @deprecated use new constant. | ||
*/ | ||
public const CONSTANT = 'some reason.'; | ||
|
||
/** | ||
* @deprecated 1.0.1 use new method. | ||
*/ | ||
public function run() | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
final class Fixture | ||
{ | ||
#[\Deprecated(message: 'use new constant.')] | ||
public const CONSTANT = 'some reason.'; | ||
|
||
#[\Deprecated(message: 'use new method.', since: '1.0.1')] | ||
public function run() | ||
{ | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...ecatedAnnotationToDeprecatedAttributeRector/Fixture/gracefully_removes_annotation.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
final class GracefullyRemovesAnnotation | ||
{ | ||
/** | ||
* @deprecated some reason. | ||
* @see https://getrector.com | ||
*/ | ||
public const FOO = 'foo'; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
final class GracefullyRemovesAnnotation | ||
{ | ||
/** | ||
* @see https://getrector.com | ||
8000 | */ | |
#[\Deprecated(message: 'some reason.')] | ||
public const FOO = 'foo'; | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
...ass_/DeprecatedAnnotationToDeprecatedAttributeRector/Fixture/works_with_functions.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
/** | ||
* @deprecated some reason. | ||
*/ | ||
function works_with_functions() | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
|
||
#[\Deprecated(message: 'some reason.')] | ||
function works_with_functions() | ||
{ | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
.../Rector/Class_/DeprecatedAnnotationToDeprecatedAttributeRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; | ||
use Rector\ValueObject\PhpVersion; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(DeprecatedAnnotationToDeprecatedAttributeRector::class); | ||
|
||
$rectorConfig->phpVersion(PhpVersion::PHP_84); | ||
}; |
162 changes: 162 additions & 0 deletions
162
rules/Php84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttributeRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php84\Rector\Class_; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover; | ||
use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector\DeprecatedAnnotationToDeprecatedAttributeRectorTest | ||
*/ | ||
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
/** | ||
* @see https://regex101.com/r/qNytVk/1 | ||
* @var string | ||
*/ | ||
private const VERSION_MATCH_REGEX = '/^(?:(\d+\.\d+\.\d+)\s+)?(.*)$/'; | ||
|
||
public function __construct( | ||
private readonly PhpDocTagRemover $phpDocTagRemover, | ||
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory, | ||
private readonly DocBlockUpdater $docBlockUpdater, | ||
private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change @deprecated annotation to Deprecated attribute', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
/** | ||
* @deprecated 1.0.0 Use SomeOtherClass instead | ||
*/ | ||
class SomeClass | ||
{ | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
#[\Deprecated(message: 'Use SomeOtherClass instead', since: '1.0.0')] | ||
class SomeClass | ||
{ | ||
} | ||
CODE_SAMPLE | ||
), | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
/** | ||
* @deprecated 1.0.0 Use SomeOtherFunction instead | ||
*/ | ||
function someFunction() | ||
{ | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
#[\Deprecated(message: 'Use SomeOtherFunction instead', since: '1.0.0')] | ||
function someFunction() | ||
{ | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Function_::class, ClassMethod::class, ClassConst::class]; | ||
} | ||
|
||
/** | ||
* @param ClassConst|Function_|ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$hasChanged = false; | ||
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); | ||
if ($phpDocInfo instanceof PhpDocInfo) { | ||
$deprecatedAttributeGroup = $this->handleDeprecated($phpDocInfo); | ||
if ($deprecatedAttributeGroup instanceof AttributeGroup) { | ||
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); | ||
$node->attrGroups = array_merge($node->attrGroups, [$deprecatedAttributeGroup]); | ||
$this->removeDeprecatedAnnotations($phpDocInfo); | ||
$hasChanged = true; | ||
} | ||
} | ||
|
||
return $hasChanged ? $node : null; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATED_ATTRIBUTE; | ||
} | ||
|
||
private function handleDeprecated(PhpDocInfo $phpDocInfo): ?AttributeGroup | ||
{ | ||
$attributeGroup = null; | ||
$desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated'); | ||
foreach ($desiredTagValueNodes as $desiredTagValueNode) { | ||
if (! $desiredTagValueNode->value instanceof DeprecatedTagValueNode) { | ||
continue; | ||
} | ||
|
||
$attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->description); | ||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); | ||
|
||
break; | ||
} | ||
|
||
return $attributeGroup; | ||
} | ||
|
||
private function createAttributeGroup(string $annotationValue): AttributeGroup | ||
{ | ||
$matches = Strings::match($annotationValue, self::VERSION_MATCH_REGEX); | ||
|
||
$since = $matches[1] ?? null; | ||
$message = $matches[2] ?? null; | ||
|
||
return $this->phpAttributeGroupFactory->createFromClassWithItems('Deprecated', array_filter([ | ||
'message' => $message, | ||
'since' => $since, | ||
])); | ||
} | ||
|
||
private function removeDeprecatedAnnotations(PhpDocInfo $phpDocInfo): bool | ||
{ | ||
$hasChanged = false; | ||
|
||
$desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated'); | ||
foreach ($desiredTagValueNodes as $desiredTagValueNode) { | ||
if (! $desiredTagValueNode->value instanceof GenericTagValueNode) { | ||
continue; | ||
} | ||
|
||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); | ||
$hasChanged = true; | ||
} | ||
|
||
return $hasChanged; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.