Skip to content

Commit

Permalink
Add Internal attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 22, 2024
1 parent e2bcf3a commit 493630e
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| Attribute | PHPDoc Annotations |
|-------------------------------------------------------------------------------------------------------------------|---------------------------|
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"php-static-analysis/attributes": "^0.1.8 || dev-main",
"php-static-analysis/node-visitor": "^0.1.8 || dev-main",
"php-static-analysis/attributes": "^0.1.9 || dev-main",
"php-static-analysis/node-visitor": "^0.1.9 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function parseString(string $sourceCode): array
private function traverseAst(array $ast): array
{
$traverser = new NodeTraverser();
$nodeVisitor = new AttributeNodeVisitor();
$nodeVisitor = new AttributeNodeVisitor('phpstan');
$traverser->addVisitor($nodeVisitor);

$ast = $traverser->traverse($ast);
Expand Down
54 changes: 54 additions & 0 deletions tests/InternalAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension;

class InternalAttributeTest extends BaseAttributeTestCase
{
public function testClassInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/ClassInternalAttribute.php');
$this->assertCount(0, $errors);
}

public function testTraitInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/TraitInternalAttribute.php');
$this->assertCount(0, $errors);
}

public function testInterfaceInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/InterfaceInternalAttribute.php');
$this->assertCount(0, $errors);
}

public function testMethodInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/MethodInternalAttribute.php');
$this->assertCount(0, $errors);
}

public function testFunctionInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/FunctionInternalAttribute.php');
$this->assertCount(0, $errors);
}

public function testProperyInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/PropertyInternalAttribute.php');
$this->assertCount(0, $errors);
}

public function testInvalidMethodInternalAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Internal/InvalidMethodInternalAttribute.php');
$expectedErrors = [
'Parameter #1 $namespace of attribute class PhpStaticAnalysis\Attributes\Internal constructor expects string|null, int given.' => 9,
'Attribute class PhpStaticAnalysis\Attributes\Internal does not have the parameter target.' => 15,
'Attribute class PhpStaticAnalysis\Attributes\Internal is not repeatable but is already present above the method.' => 22,
];

$this->checkExpectedErrors($errors, $expectedErrors);
}
}
38 changes: 38 additions & 0 deletions tests/data/Internal/ClassInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

#[Internal('newNamespace\test')] // Can only be accessed from the current namespace
class ClassInternalAttribute
{
#[Internal]
public function myFunction(): void
{
}
}

namespace newNamespace\test;

class newClass
{
public function newFunction(): void
{
$class = new \test\PhpStaticAnalysis\PHPStanExtension\data\Internal\ClassInternalAttribute();

$class->myFunction();
}
}

namespace newNamespace\other;

class otherClass
{
public function otherFunction(): void
{
$class = new \test\PhpStaticAnalysis\PHPStanExtension\data\Internal\ClassInternalAttribute();

$class->myFunction();
}
}
10 changes: 10 additions & 0 deletions tests/data/Internal/FunctionInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data;

use PhpStaticAnalysis\Attributes\Internal;

#[Internal]
function returnInternal(): void
{
}
10 changes: 10 additions & 0 deletions tests/data/Internal/InterfaceInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

#[Internal]
interface InterfaceInternalAttribute
{
}
26 changes: 26 additions & 0 deletions tests/data/Internal/InvalidMethodInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

class InvalidMethodInternalAttribute
{
#[Internal(0)]
public function getName(): void
{
}

public function getExtraName(
#[Internal]
string $name
): string {
return $name;
}

#[Internal]
#[Internal]
public function getMoreName(): void
{
}
}
28 changes: 28 additions & 0 deletions tests/data/Internal/MethodInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

class MethodInternalAttribute
{
#[Internal]
public function returnInternal(): void
{
}

/**
* @codeCoverageIgnore
*/
#[Internal]
public function returnAnotherInternal(): void
{
}

/**
* @internal
*/
public function returnMoreInternals(): void
{
}
}
14 changes: 14 additions & 0 deletions tests/data/Internal/PropertyInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

class PropertyInternalAttribute
{
#[Internal]
public const NAME = 'name';

#[Internal]
public string $name = '';
}
10 changes: 10 additions & 0 deletions tests/data/Internal/TraitInternalAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

#[Internal]
trait TraitInternalAttribute
{
}

0 comments on commit 493630e

Please sign in to comment.