Skip to content

Commit 3ba0a66

Browse files
committed
Updated Rector to commit 8874702f3aeacf4dfe59e8f57b5d62c63d1dce79
rectorphp/rector-src@8874702 [PHP84] Deprecated annotation to Deprecated attribute (#6923)
1 parent 652df68 commit 3ba0a66

6 files changed

Lines changed: 160 additions & 3 deletions

File tree

config/set/php84.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
namespace RectorPrefix202505;
55

66
use Rector\Config\RectorConfig;
7+
use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;
78
use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector;
89
use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector;
910
use Rector\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector;
1011
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
1112
return static function (RectorConfig $rectorConfig) : void {
12-
$rectorConfig->rules([ExplicitNullableParamTypeRector::class, RoundingModeEnumRector::class, AddEscapeArgumentRector::class, NewMethodCallWithoutParenthesesRector::class]);
13+
$rectorConfig->rules([ExplicitNullableParamTypeRector::class, RoundingModeEnumRector::class, AddEscapeArgumentRector::class, NewMethodCallWithoutParenthesesRector::class, DeprecatedAnnotationToDeprecatedAttributeRector::class]);
1314
};
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Php84\Rector\Class_;
5+
6+
use RectorPrefix202505\Nette\Utils\Strings;
7+
use PhpParser\Node;
8+
use PhpParser\Node\AttributeGroup;
9+
use PhpParser\Node\Stmt\ClassConst;
10+
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\Function_;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
13+
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
14+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
15+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
16+
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
17+
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
18+
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
19+
use Rector\Rector\AbstractRector;
20+
use Rector\ValueObject\PhpVersionFeature;
21+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
22+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
23+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
24+
/**
25+
* @see \Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector\DeprecatedAnnotationToDeprecatedAttributeRectorTest
26+
*/
27+
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
28+
{
29+
/**
30+
* @readonly
31+
*/
32+
private PhpDocTagRemover $phpDocTagRemover;
33+
/**
34+
* @readonly
35+
*/
36+
private PhpAttributeGroupFactory $phpAttributeGroupFactory;
37+
/**
38+
* @readonly
39+
*/
40+
private DocBlockUpdater $docBlockUpdater;
41+
/**
42+
* @readonly
43+
*/
44+
private PhpDocInfoFactory $phpDocInfoFactory;
45+
/**
46+
* @see https://regex101.com/r/qNytVk/1
47+
* @var string
48+
*/
49+
private const VERSION_MATCH_REGEX = '/^(?:(\\d+\\.\\d+\\.\\d+)\\s+)?(.*)$/';
50+
public function __construct(PhpDocTagRemover $phpDocTagRemover, PhpAttributeGroupFactory $phpAttributeGroupFactory, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory)
51+
{
52+
$this->phpDocTagRemover = $phpDocTagRemover;
53+
$this->phpAttributeGroupFactory = $phpAttributeGroupFactory;
54+
$this->docBlockUpdater = $docBlockUpdater;
55+
$this->phpDocInfoFactory = $phpDocInfoFactory;
56+
}
57+
public function getRuleDefinition() : RuleDefinition
58+
{
59+
return new RuleDefinition('Change @deprecated annotation to Deprecated attribute', [new CodeSample(<<<'CODE_SAMPLE'
60+
/**
61+
* @deprecated 1.0.0 Use SomeOtherClass instead
62+
*/
63+
class SomeClass
64+
{
65+
}
66+
CODE_SAMPLE
67+
, <<<'CODE_SAMPLE'
68+
#[\Deprecated(message: 'Use SomeOtherClass instead', since: '1.0.0')]
69+
class SomeClass
70+
{
71+
}
72+
CODE_SAMPLE
73+
), new CodeSample(<<<'CODE_SAMPLE'
74+
/**
75+
* @deprecated 1.0.0 Use SomeOtherFunction instead
76+
*/
77+
function someFunction()
78+
{
79+
}
80+
CODE_SAMPLE
81+
, <<<'CODE_SAMPLE'
82+
#[\Deprecated(message: 'Use SomeOtherFunction instead', since: '1.0.0')]
83+
function someFunction()
84+
{
85+
}
86+
CODE_SAMPLE
87+
)]);
88+
}
89+
public function getNodeTypes() : array
90+
{
91+
return [Function_::class, ClassMethod::class, ClassConst::class];
92+
}
93+
/**
94+
* @param ClassConst|Function_|ClassMethod $node
95+
*/
96+
public function refactor(Node $node) : ?Node
97+
{
98+
$hasChanged = \false;
99+
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
100+
if ($phpDocInfo instanceof PhpDocInfo) {
101+
$deprecatedAttributeGroup = $this->handleDeprecated($phpDocInfo);
102+
if ($deprecatedAttributeGroup instanceof AttributeGroup) {
103+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
104+
$node->attrGroups = \array_merge($node->attrGroups, [$deprecatedAttributeGroup]);
105+
$this->removeDeprecatedAnnotations($phpDocInfo);
106+
$hasChanged = \true;
107+
}
108+
}
109+
return $hasChanged ? $node : null;
110+
}
111+
public function provideMinPhpVersion() : int
112+
{
113+
return PhpVersionFeature::DEPRECATED_ATTRIBUTE;
114+
}
115+
private function handleDeprecated(PhpDocInfo $phpDocInfo) : ?AttributeGroup
116+
{
117+
$attributeGroup = null;
118+
$desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated');
119+
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
120+
if (!$desiredTagValueNode->value instanceof DeprecatedTagValueNode) {
121+
continue;
122+
}
123+
$attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->description);
124+
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
125+
break;
126+
}
127+
return $attributeGroup;
128+
}
129+
private function createAttributeGroup(string $annotationValue) : AttributeGroup
130+
{
131+
$matches = Strings::match($annotationValue, self::VERSION_MATCH_REGEX);
132+
$since = $matches[1] ?? null;
133+
$message = $matches[2] ?? null;
134+
return $this->phpAttributeGroupFactory->createFromClassWithItems('Deprecated', \array_filter(['message' => $message, 'since' => $since]));
135+
}
136+
private function removeDeprecatedAnnotations(PhpDocInfo $phpDocInfo) : bool
137+
{
138+
$hasChanged = \false;
139+
$desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated');
140+
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
141+
if (!$desiredTagValueNode->value instanceof GenericTagValueNode) {
142+
continue;
143+
}
144+
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
145+
$hasChanged = \true;
146+
}
147+
return $hasChanged;
148+
}
149+
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '865e85b65ff9297009e97c6b91bc8d27393668d7';
22+
public const PACKAGE_VERSION = '8874702f3aeacf4dfe59e8f57b5d62c63d1dce79';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-05-28 17:05:55';
27+
public const RELEASE_DATE = '2025-05-28 23:48:48';
2828
/**
2929
* @var int
3030
*/

src/ValueObject/PhpVersionFeature.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,4 +596,9 @@ final class PhpVersionFeature
596596
* @var int
597597
*/
598598
public const DEPRECATE_GET_CLASS_WITHOUT_ARGS = \Rector\ValueObject\PhpVersion::PHP_83;
599+
/**
600+
* @see https://wiki.php.net/rfc/deprecated_attribute
601+
* @var int
602+
*/
603+
public const DEPRECATED_ATTRIBUTE = \Rector\ValueObject\PhpVersion::PHP_84;
599604
}

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,7 @@
20952095
'Rector\\Php83\\Rector\\FuncCall\\CombineHostPortLdapUriRector' => $baseDir . '/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php',
20962096
'Rector\\Php83\\Rector\\FuncCall\\DynamicClassConstFetchRector' => $baseDir . '/rules/Php83/Rector/FuncCall/DynamicClassConstFetchRector.php',
20972097
'Rector\\Php83\\Rector\\FuncCall\\RemoveGetClassGetParentClassNoArgsRector' => $baseDir . '/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php',
2098+
'Rector\\Php84\\Rector\\Class_\\DeprecatedAnnotationToDeprecatedAttributeRector' => $baseDir . '/rules/Php84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttributeRector.php',
20982099
'Rector\\Php84\\Rector\\FuncCall\\AddEscapeArgumentRector' => $baseDir . '/rules/Php84/Rector/FuncCall/AddEscapeArgumentRector.php',
20992100
'Rector\\Php84\\Rector\\FuncCall\\RoundingModeEnumRector' => $baseDir . '/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php',
21002101
'Rector\\Php84\\Rector\\MethodCall\\NewMethodCallWithoutParenthesesRector' => $baseDir . '/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,7 @@ class ComposerStaticInit912e02b74e97bb7d91c242d9973e0c44
23142314
'Rector\\Php83\\Rector\\FuncCall\\CombineHostPortLdapUriRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php',
23152315
'Rector\\Php83\\Rector\\FuncCall\\DynamicClassConstFetchRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/FuncCall/DynamicClassConstFetchRector.php',
23162316
'Rector\\Php83\\Rector\\FuncCall\\RemoveGetClassGetParentClassNoArgsRector' => __DIR__ . '/../..' . '/rules/Php83/Rector/FuncCall/RemoveGetClassGetParentClassNoArgsRector.php',
2317+
'Rector\\Php84\\Rector\\Class_\\DeprecatedAnnotationToDeprecatedAttributeRector' => __DIR__ . '/../..' . '/rules/Php84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttributeRector.php',
23172318
'Rector\\Php84\\Rector\\FuncCall\\AddEscapeArgumentRector' => __DIR__ . '/../..' . '/rules/Php84/Rector/FuncCall/AddEscapeArgumentRector.php',
23182319
'Rector\\Php84\\Rector\\FuncCall\\RoundingModeEnumRector' => __DIR__ . '/../..' . '/rules/Php84/Rector/FuncCall/RoundingModeEnumRector.php',
23192320
'Rector\\Php84\\Rector\\MethodCall\\NewMethodCallWithoutParenthesesRector' => __DIR__ . '/../..' . '/rules/Php84/Rector/MethodCall/NewMethodCallWithoutParenthesesRector.php',

0 commit comments

Comments
 (0)