Skip to content

Commit 493630e

Browse files
Add Internal attribute
1 parent e2bcf3a commit 493630e

11 files changed

+194
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
9797
| Attribute | PHPDoc Annotations |
9898
|-------------------------------------------------------------------------------------------------------------------|---------------------------|
9999
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
100+
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
100101
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
101102
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
102103
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"prefer-stable": true,
2525
"require": {
2626
"php": ">=8.0",
27-
"php-static-analysis/attributes": "^0.1.8 || dev-main",
28-
"php-static-analysis/node-visitor": "^0.1.8 || dev-main",
27+
"php-static-analysis/attributes": "^0.1.9 || dev-main",
28+
"php-static-analysis/node-visitor": "^0.1.9 || dev-main",
2929
"phpstan/phpstan": "^1.8"
3030
},
3131
"require-dev": {

src/Parser/AttributeParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function parseString(string $sourceCode): array
3535
private function traverseAst(array $ast): array
3636
{
3737
$traverser = new NodeTraverser();
38-
$nodeVisitor = new AttributeNodeVisitor();
38+
$nodeVisitor = new AttributeNodeVisitor('phpstan');
3939
$traverser->addVisitor($nodeVisitor);
4040

4141
$ast = $traverser->traverse($ast);

tests/InternalAttributeTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension;
4+
5+
class InternalAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testClassInternalAttribute(): void
8+
{
9+
$errors = $this->analyse(__DIR__ . '/data/Internal/ClassInternalAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testTraitInternalAttribute(): void
14+
{
15+
$errors = $this->analyse(__DIR__ . '/data/Internal/TraitInternalAttribute.php');
16+
$this->assertCount(0, $errors);
17+
}
18+
19+
public function testInterfaceInternalAttribute(): void
20+
{
21+
$errors = $this->analyse(__DIR__ . '/data/Internal/InterfaceInternalAttribute.php');
22+
$this->assertCount(0, $errors);
23+
}
24+
25+
public function testMethodInternalAttribute(): void
26+
{
27+
$errors = $this->analyse(__DIR__ . '/data/Internal/MethodInternalAttribute.php');
28+
$this->assertCount(0, $errors);
29+
}
30+
31+
public function testFunctionInternalAttribute(): void
32+
{
33+
$errors = $this->analyse(__DIR__ . '/data/Internal/FunctionInternalAttribute.php');
34+
$this->assertCount(0, $errors);
35+
}
36+
37+
public function testProperyInternalAttribute(): void
38+
{
39+
$errors = $this->analyse(__DIR__ . '/data/Internal/PropertyInternalAttribute.php');
40+
$this->assertCount(0, $errors);
41+
}
42+
43+
public function testInvalidMethodInternalAttribute(): void
44+
{
45+
$errors = $this->analyse(__DIR__ . '/data/Internal/InvalidMethodInternalAttribute.php');
46+
$expectedErrors = [
47+
'Parameter #1 $namespace of attribute class PhpStaticAnalysis\Attributes\Internal constructor expects string|null, int given.' => 9,
48+
'Attribute class PhpStaticAnalysis\Attributes\Internal does not have the parameter target.' => 15,
49+
'Attribute class PhpStaticAnalysis\Attributes\Internal is not repeatable but is already present above the method.' => 22,
50+
];
51+
52+
$this->checkExpectedErrors($errors, $expectedErrors);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
#[Internal('newNamespace\test')] // Can only be accessed from the current namespace
8+
class ClassInternalAttribute
9+
{
10+
#[Internal]
11+
public function myFunction(): void
12+
{
13+
}
14+
}
15+
16+
namespace newNamespace\test;
17+
18+
class newClass
19+
{
20+
public function newFunction(): void
21+
{
22+
$class = new \test\PhpStaticAnalysis\PHPStanExtension\data\Internal\ClassInternalAttribute();
23+
24+
$class->myFunction();
25+
}
26+
}
27+
28+
namespace newNamespace\other;
29+
30+
class otherClass
31+
{
32+
public function otherFunction(): void
33+
{
34+
$class = new \test\PhpStaticAnalysis\PHPStanExtension\data\Internal\ClassInternalAttribute();
35+
36+
$class->myFunction();
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
#[Internal]
8+
function returnInternal(): void
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
#[Internal]
8+
interface InterfaceInternalAttribute
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
class InvalidMethodInternalAttribute
8+
{
9+
#[Internal(0)]
10+
public function getName(): void
11+
{
12+
}
13+
14+
public function getExtraName(
15+
#[Internal]
16+
string $name
17+
): string {
18+
return $name;
19+
}
20+
21+
#[Internal]
22+
#[Internal]
23+
public function getMoreName(): void
24+
{
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
class MethodInternalAttribute
8+
{
9+
#[Internal]
10+
public function returnInternal(): void
11+
{
12+
}
13+
14+
/**
15+
* @codeCoverageIgnore
16+
*/
17+
#[Internal]
18+
public function returnAnotherInternal(): void
19+
{
20+
}
21+
22+
/**
23+
* @internal
24+
*/
25+
public function returnMoreInternals(): void
26+
{
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
class PropertyInternalAttribute
8+
{
9+
#[Internal]
10+
public const NAME = 'name';
11+
12+
#[Internal]
13+
public string $name = '';
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;
4+
5+
use PhpStaticAnalysis\Attributes\Internal;
6+
7+
#[Internal]
8+
trait TraitInternalAttribute
9+
{
10+
}

0 commit comments

Comments
 (0)