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 de0afce commit 488ed90
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=8.0",
"cweagans/composer-patches": "^1.7",
"php-static-analysis/attributes": "^0.1.8 || dev-main",
"php-static-analysis/attributes": "^0.1.9 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use PhpStaticAnalysis\Attributes\Deprecated;
use PhpStaticAnalysis\Attributes\Internal;
use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
Expand All @@ -23,6 +24,7 @@
AnnotationsToAttributesRector::class,
[
new AnnotationToAttribute('deprecated', Deprecated::class),
new AnnotationToAttribute('internal', Internal::class),
new AnnotationToAttribute('method', Method::class),
new AnnotationToAttribute('param', Param::class),
new AnnotationToAttribute('property', Property::class),
Expand Down
14 changes: 12 additions & 2 deletions src/AnnotationsToAttributesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
}

$tagValueNode = $phpDocChildNode->value;
$attributeComment = null;
$attributeComment = '';
switch (true) {
case $tagValueNode instanceof MethodTagValueNode:
$methodSignature = (string)($tagValueNode);
Expand Down Expand Up @@ -276,7 +276,17 @@ private function processAnnotations(PhpDocInfo $phpDocInfo): array
case $tagValueNode instanceof DeprecatedTagValueNode:
case $tagValueNode instanceof GenericTagValueNode:
$args = [];
$attributeComment = (string)$tagValueNode;
if ($phpDocChildNode->name === '@psalm-internal') {
$remainingText = (string)$tagValueNode;
$parts = explode(' ', $remainingText);
$namespace = array_shift($parts);
if ($namespace) {
$args[] = new Node\Arg(new Scalar\String_($namespace));
$attributeComment = implode(' ', $parts);
}
} else {
$attributeComment = (string)$tagValueNode;
}
break;
default:
continue 2;
Expand Down
133 changes: 133 additions & 0 deletions tests/Fixture/InternalAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

/**
* @internal
*/
class InternalAttributeTest
{
/**
* @internal
*/
public const NAME = 'name';

/**
* @internal
*/
public string $name = '';

/**
* @internal
*/
public function getName(): void
{
}

/**
* @codeCoverageIgnore
* @internal
*/
public function getMoreNames(): void
{
}

/**
* @internal
*/
#[Param(name:'string')]
public function getAnotherName($name)
{
return "Hello " . $name;
}

/**
* @internal only available in the current namespace
*/
public function getUserName(): void
{
}

/**
* @psalm-internal A\B
*/
public function getPsalmName(): void
{
}

/**
* @psalm-internal A\B Only available in the A\B namespace
*/
public function getPsalmNameWithComment(): void
{
}
}

/**
* @internal
*/
function getName(): void
{
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

#[\PhpStaticAnalysis\Attributes\Internal]
class InternalAttributeTest
{
#[\PhpStaticAnalysis\Attributes\Internal]
public const NAME = 'name';

#[\PhpStaticAnalysis\Attributes\Internal]
public string $name = '';

#[\PhpStaticAnalysis\Attributes\Internal]
public function getName(): void
{
}

/**
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Internal]
public function getMoreNames(): void
{
}

#[Param(name:'string')]
#[\PhpStaticAnalysis\Attributes\Internal]
public function getAnotherName($name)
{
return "Hello " . $name;
}

#[\PhpStaticAnalysis\Attributes\Internal] // only available in the current namespace
public function getUserName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Internal('A\B')]
public function getPsalmName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Internal('A\B')] // Only available in the A\B namespace
public function getPsalmNameWithComment(): void
{
}
}

#[\PhpStaticAnalysis\Attributes\Internal]
function getName(): void
{
}

?>

0 comments on commit 488ed90

Please sign in to comment.