Skip to content

Commit 1028781

Browse files
Add SelfOut attribute
1 parent 75d8995 commit 1028781

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require": {
2626
"php": ">=8.0",
2727
"nikic/php-parser": "^4 || ^5",
28-
"php-static-analysis/attributes": "^0.1.12 || dev-main"
28+
"php-static-analysis/attributes": "^0.1.13 || dev-main"
2929
},
3030
"require-dev": {
3131
"php-static-analysis/phpstan-extension": "dev-main",

src/AttributeNodeVisitor.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpStaticAnalysis\Attributes\PropertyRead;
2323
use PhpStaticAnalysis\Attributes\PropertyWrite;
2424
use PhpStaticAnalysis\Attributes\Returns;
25+
use PhpStaticAnalysis\Attributes\SelfOut;
2526
use PhpStaticAnalysis\Attributes\Template;
2627
use PhpStaticAnalysis\Attributes\TemplateContravariant;
2728
use PhpStaticAnalysis\Attributes\TemplateCovariant;
@@ -34,7 +35,8 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
3435
{
3536
private const ARGS_NONE = 'none';
3637
private const ARGS_ONE = 'one';
37-
private const ARGS_ONE_OPTIONAL = 'one_optional';
38+
private const ARGS_ONE_OPTIONAL = 'one optional';
39+
private const ARGS_ONE_WITH_PREFIX = 'one with prefix';
3840
private const ARGS_TWO_WITH_TYPE = 'two with type';
3941
private const ARGS_MANY_IN_USE = "many in use";
4042
private const ARGS_MANY_WITH_NAME = "many with name";
@@ -77,6 +79,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
7779
Param::class,
7880
ParamOut::class,
7981
Returns::class,
82+
SelfOut::class,
8083
Template::class,
8184
Type::class,
8285
],
@@ -134,6 +137,7 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
134137
'PropertyRead' => PropertyRead::class,
135138
'PropertyWrite' => PropertyWrite::class,
136139
'Returns' => Returns::class,
140+
'SelfOut' => SelfOut::class,
137141
'Template' => Template::class,
138142
'TemplateContravariant' => TemplateContravariant::class,
139143
'TemplateCovariant' => TemplateCovariant::class,
@@ -178,6 +182,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
178182
Returns::class => [
179183
'all' => 'return',
180184
],
185+
SelfOut::class => [
186+
'all' => 'self-out',
187+
],
181188
Template::class => [
182189
'all' => 'template',
183190
],
@@ -239,6 +246,9 @@ class AttributeNodeVisitor extends NodeVisitorAbstract
239246
Returns::class => [
240247
'all' => self::ARGS_ONE,
241248
],
249+
SelfOut::class => [
250+
'all' => self::ARGS_ONE_WITH_PREFIX,
251+
],
242252
Template::class => [
243253
'all' => self::ARGS_TWO_WITH_TYPE,
244254
],
@@ -318,6 +328,12 @@ public function enterNode(Node $node)
318328
$tagCreated = true;
319329
}
320330
break;
331+
case self::ARGS_ONE_WITH_PREFIX:
332+
if (isset($args[0])) {
333+
$tagsToAdd[] = $this->createTag($nodeType, $attributeName, $args[0], prefix: $this->toolType);
334+
$tagCreated = true;
335+
}
336+
break;
321337
case self::ARGS_ONE_OPTIONAL:
322338
if (isset($args[0])) {
323339
$tagsToAdd[] = $this->createTag(
@@ -393,7 +409,7 @@ private function createTag(
393409
} else {
394410
return '';
395411
}
396-
if ($prefix !== null) {
412+
if ($prefix !== null && $prefix !== '') {
397413
$tagName = $prefix . '-' . $tagName;
398414
}
399415
$tag = '@' . $tagName;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\NodeVisitor;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Attribute;
7+
use PhpParser\Node\AttributeGroup;
8+
use PhpParser\Node\Name\FullyQualified;
9+
use PhpStaticAnalysis\Attributes\SelfOut;
10+
11+
class SelfOutAttributeNodeVisitorTest extends AttributeNodeVisitorTestBase
12+
{
13+
public function testAddsReturnPHPDoc(): void
14+
{
15+
$node = new Node\Stmt\ClassMethod('Test');
16+
$this->addSelfOutAttributeToNode($node);
17+
$this->nodeVisitor->enterNode($node);
18+
$docText = $this->getDocText($node);
19+
$this->assertEquals("/**\n * @self-out self<T>\n */", $docText);
20+
}
21+
22+
private function addSelfOutAttributeToNode(Node\Stmt\ClassMethod $node): void
23+
{
24+
$args = [
25+
new Node\Arg(new Node\Scalar\String_('self<T>'))
26+
];
27+
$attributeName = new FullyQualified(SelfOut::class);
28+
$attribute = new Attribute($attributeName, $args);
29+
$node->attrGroups = [new AttributeGroup([$attribute])];
30+
}
31+
}

0 commit comments

Comments
 (0)