Skip to content

Commit 488ed90

Browse files
Add Internal attribute
1 parent de0afce commit 488ed90

File tree

5 files changed

+149
-3
lines changed

5 files changed

+149
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
113113
| Attribute | PHPDoc Annotations |
114114
|---------------------------------------------------------------------------------------------------|--------------------|
115115
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
116+
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
116117
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
117118
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
118119
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"require": {
2929
"php": ">=8.0",
3030
"cweagans/composer-patches": "^1.7",
31-
"php-static-analysis/attributes": "^0.1.8 || dev-main",
31+
"php-static-analysis/attributes": "^0.1.9 || dev-main",
3232
"rector/rector": "^0.19 || ^1.0"
3333
},
3434
"require-dev": {

config/sets/php-static-analysis-annotations-to-attributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use PhpStaticAnalysis\Attributes\Deprecated;
6+
use PhpStaticAnalysis\Attributes\Internal;
67
use PhpStaticAnalysis\Attributes\Method;
78
use PhpStaticAnalysis\Attributes\PropertyRead;
89
use PhpStaticAnalysis\Attributes\PropertyWrite;
@@ -23,6 +24,7 @@
2324
AnnotationsToAttributesRector::class,
2425
[
2526
new AnnotationToAttribute('deprecated', Deprecated::class),
27+
new AnnotationToAttribute('internal', Internal::class),
2628
new AnnotationToAttribute('method', Method::class),
2729
new AnnotationToAttribute('param', Param::class),
2830
new AnnotationToAttribute('property', Property::class),

src/AnnotationsToAttributesRector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
227227
}
228228

229229
$tagValueNode = $phpDocChildNode->value;
230-
$attributeComment = null;
230+
$attributeComment = '';
231231
switch (true) {
232232
case $tagValueNode instanceof MethodTagValueNode:
233233
$methodSignature = (string)($tagValueNode);
@@ -276,7 +276,17 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
276276
case $tagValueNode instanceof DeprecatedTagValueNode:
277277
case $tagValueNode instanceof GenericTagValueNode:
278278
$args = [];
279-
$attributeComment = (string)$tagValueNode;
279+
if ($phpDocChildNode->name === '@psalm-internal') {
280+
$remainingText = (string)$tagValueNode;
281+
$parts = explode(' ', $remainingText);
282+
$namespace = array_shift($parts);
283+
if ($namespace) {
284+
$args[] = new Node\Arg(new Scalar\String_($namespace));
285+
$attributeComment = implode(' ', $parts);
286+
}
287+
} else {
288+
$attributeComment = (string)$tagValueNode;
289+
}
280290
break;
281291
default:
282292
continue 2;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
4+
5+
use PhpStaticAnalysis\Attributes\Param;
6+
7+
/**
8+
* @internal
9+
*/
10+
class InternalAttributeTest
11+
{
12+
/**
13+
* @internal
14+
*/
15+
public const NAME = 'name';
16+
17+
/**
18+
* @internal
19+
*/
20+
public string $name = '';
21+
22+
/**
23+
* @internal
24+
*/
25+
public function getName(): void
26+
{
27+
}
28+
29+
/**
30+
* @codeCoverageIgnore
31+
* @internal
32+
*/
33+
public function getMoreNames(): void
34+
{
35+
}
36+
37+
/**
38+
* @internal
39+
*/
40+
#[Param(name:'string')]
41+
public function getAnotherName($name)
42+
{
43+
return "Hello " . $name;
44+
}
45+
46+
/**
47+
* @internal only available in the current namespace
48+
*/
49+
public function getUserName(): void
50+
{
51+
}
52+
53+
/**
54+
* @psalm-internal A\B
55+
*/
56+
public function getPsalmName(): void
57+
{
58+
}
59+
60+
/**
61+
* @psalm-internal A\B Only available in the A\B namespace
62+
*/
63+
public function getPsalmNameWithComment(): void
64+
{
65+
}
66+
}
67+
68+
/**
69+
* @internal
70+
*/
71+
function getName(): void
72+
{
73+
}
74+
75+
?>
76+
-----
77+
<?php
78+
79+
namespace test\PhpStaticAnalysis\RectorRule\Fixture;
80+
81+
use PhpStaticAnalysis\Attributes\Param;
82+
83+
#[\PhpStaticAnalysis\Attributes\Internal]
84+
class InternalAttributeTest
85+
{
86+
#[\PhpStaticAnalysis\Attributes\Internal]
87+
public const NAME = 'name';
88+
89+
#[\PhpStaticAnalysis\Attributes\Internal]
90+
public string $name = '';
91+
92+
#[\PhpStaticAnalysis\Attributes\Internal]
93+
public function getName(): void
94+
{
95+
}
96+
97+
/**
98+
* @codeCoverageIgnore
99+
*/
100+
#[\PhpStaticAnalysis\Attributes\Internal]
101+
public function getMoreNames(): void
102+
{
103+
}
104+
105+
#[Param(name:'string')]
106+
#[\PhpStaticAnalysis\Attributes\Internal]
107+
public function getAnotherName($name)
108+
{
109+
return "Hello " . $name;
110+
}
111+
112+
#[\PhpStaticAnalysis\Attributes\Internal] // only available in the current namespace
113+
public function getUserName(): void
114+
{
115+
}
116+
117+
#[\PhpStaticAnalysis\Attributes\Internal('A\B')]
118+
public function getPsalmName(): void
119+
{
120+
}
121+
122+
#[\PhpStaticAnalysis\Attributes\Internal('A\B')] // Only available in the A\B namespace
123+
public function getPsalmNameWithComment(): void
124+
{
125+
}
126+
}
127+
128+
#[\PhpStaticAnalysis\Attributes\Internal]
129+
function getName(): void
130+
{
131+
}
132+
133+
?>

0 commit comments

Comments
 (0)