-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathDeprecatedAnnotationToDeprecatedAttributeConverter.php
More file actions
132 lines (111 loc) · 4.49 KB
/
DeprecatedAnnotationToDeprecatedAttributeConverter.php
File metadata and controls
132 lines (111 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Rector\PhpAttribute;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Trait_;
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\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
final readonly class DeprecatedAnnotationToDeprecatedAttributeConverter
{
/**
* @see https://regex101.com/r/qNytVk/1
* @var string
*/
private const VERSION_MATCH_REGEX = '/^(?:(\d+\.\d+\.\d+)\s+)?(.*)$/';
/**
* @see https://regex101.com/r/SVDPOB/1
* @var string
*/
private const START_STAR_SPACED_REGEX = '#^ *\*#ms';
public function __construct(
private PhpDocTagRemover $phpDocTagRemover,
private PhpAttributeGroupFactory $phpAttributeGroupFactory,
private DocBlockUpdater $docBlockUpdater,
private PhpDocInfoFactory $phpDocInfoFactory,
) {
}
public function convert(ClassConst|Function_|ClassMethod|Const_|Trait_ $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;
}
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);
if ($matches === null) {
$annotationValue = Strings::replace($annotationValue, self::START_STAR_SPACED_REGEX, '');
return new AttributeGroup([
new Attribute(
new FullyQualified('Deprecated'),
[new Arg(
value: new String_($annotationValue, [
AttributeKey::KIND => String_::KIND_NOWDOC,
AttributeKey::DOC_LABEL => 'TXT',
]),
name: new Identifier('message')
)]
),
]);
}
$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;
}
}