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 1aad2a8 commit a686670
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -26,8 +26,8 @@
"require": {
"php": ">=8.0",
"ext-simplexml": "*",
"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",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/AttributeStatementProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __set(string $property, mixed $value)
private function traverseAst(array $ast): array
{
$traverser = new NodeTraverser();
$nodeVisitor = new AttributeNodeVisitor();
$nodeVisitor = new AttributeNodeVisitor('psalm');
$traverser->addVisitor($nodeVisitor);

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

namespace test\PhpStaticAnalysis\PsalmPlugin;

class InternalAttributeTest extends BaseAttributeTestCase
{
public function testClassInternalAttribute(): void
{
$errors = $this->analyzeTestFile('/data/Internal/ClassInternalAttribute.php');
$expectedErrors = [
'test\PhpStaticAnalysis\PsalmPlugin\data\Internal\ClassInternalAttribute is internal to newNamespace\test but called from newNamespace\other\otherClass' => 34,
'The method test\PhpStaticAnalysis\PsalmPlugin\data\Internal\ClassInternalAttribute::myFunction is internal to newNamespace\test and test but called from newNamespace\other\otherClass::otherFunction' => 36,
];
$this->checkExpectedErrors($errors, $expectedErrors);
}

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

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

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

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

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

public function testInvalidMethodInternalAttribute(): void
{
$errors = $this->analyzeTestFile('/data/Internal/InvalidMethodInternalAttribute.php');
$expectedErrors = [
'psalm-internal annotation used without specifying namespace in docblock for test\PhpStaticAnalysis\PsalmPlugin\data\Internal\InvalidMethodInternalAttribute::getName' => 9,
'Argument 1 of PhpStaticAnalysis\Attributes\Internal::__construct expects null|string, but 0 provided' => 9,
'Attribute Internal cannot be used on a function/method parameter' => 15,
'Attribute Internal is not repeatable' => 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\PsalmPlugin\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

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

namespace newNamespace\test;

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

$class->myFunction();
}
}

namespace newNamespace\other;

class otherClass
{
public function otherFunction(): void
{
$class = new \test\PhpStaticAnalysis\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\data\Internal;

use PhpStaticAnalysis\Attributes\Internal;

#[Internal]
trait TraitInternalAttribute
{
}

0 comments on commit a686670

Please sign in to comment.