Skip to content

Commit 3394d5d

Browse files
Add SelfOut attribute
1 parent bc2e06a commit 3394d5d

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
107107
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
108108
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
109109
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
110+
| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` |
110111
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
111112
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
112113
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |

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.12 || dev-main",
28-
"php-static-analysis/node-visitor": "^0.1.12 || dev-main",
27+
"php-static-analysis/attributes": "^0.1.13 || dev-main",
28+
"php-static-analysis/node-visitor": "^0.1.13 || dev-main",
2929
"phpstan/phpstan": "^1.8"
3030
},
3131
"require-dev": {

tests/SelfOutAttributeTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension;
4+
5+
class SelfOutAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testMethodSelfOutAttribute(): void
8+
{
9+
$errors = $this->analyse(__DIR__ . '/data/SelfOut/MethodSelfOutAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testInvalidMethodSelfOutAttribute(): void
14+
{
15+
$errors = $this->analyse(__DIR__ . '/data/SelfOut/InvalidMethodSelfOutAttribute.php');
16+
17+
$expectedErrors = [
18+
'PHPDoc tag @phpstan-self-out has invalid value (): Unexpected token "\n ", expected type at offset 75' => 12,
19+
'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\SelfOut constructor expects string, int given.' => 14,
20+
'Attribute class PhpStaticAnalysis\Attributes\SelfOut is not repeatable but is already present above the method.' => 22,
21+
'Attribute class PhpStaticAnalysis\Attributes\SelfOut does not have the property target.' => 27,
22+
];
23+
24+
$this->checkExpectedErrors($errors, $expectedErrors);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\SelfOut;
4+
5+
use PhpStaticAnalysis\Attributes\Param;
6+
use PhpStaticAnalysis\Attributes\SelfOut;
7+
use PhpStaticAnalysis\Attributes\Template;
8+
9+
#[Template('TValue')]
10+
class InvalidMethodSelfOutAttribute
11+
{
12+
#[Template('TItemValue')]
13+
#[Param(item: 'TItemValue')]
14+
#[SelfOut(0)]
15+
public function add($item): void
16+
{
17+
}
18+
19+
#[Template('TItemValue')]
20+
#[Param(item: 'TItemValue')]
21+
#[SelfOut('self<TValue|TItemValue>')]
22+
#[SelfOut('self<TValue|TItemValue>')]
23+
public function addMore($item): void
24+
{
25+
}
26+
27+
#[SelfOut('self<TValue|TItemValue>')]
28+
public string $property;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\SelfOut;
4+
5+
use PhpStaticAnalysis\Attributes\Param;
6+
use PhpStaticAnalysis\Attributes\SelfOut;
7+
use PhpStaticAnalysis\Attributes\Template;
8+
9+
#[Template('TValue')]
10+
class MethodSelfOutAttribute
11+
{
12+
#[Template('TItemValue')]
13+
#[Param(item: 'TItemValue')]
14+
#[SelfOut('self<TValue|TItemValue>')] // we specify the new type
15+
public function add($item): void
16+
{
17+
}
18+
19+
/**
20+
* @deprecated
21+
*/
22+
#[Template('TItemValue')]
23+
#[Param(item: 'TItemValue')]
24+
#[SelfOut('self<TValue|TItemValue>')]
25+
public function addMore($item): void
26+
{
27+
}
28+
29+
/**
30+
* @self-out self<TValue>
31+
*/
32+
#[Template('TItemValue')]
33+
#[Param(item: 'TItemValue')]
34+
#[SelfOut('self<TValue|TItemValue>')]
35+
public function addEvenMore($item): void
36+
{
37+
}
38+
39+
/**
40+
* @self-out self<TValue|TItemValue>
41+
*/
42+
#[Template('TItemValue')]
43+
#[Param(item: 'TItemValue')]
44+
public function addMoreAndMore($item): void
45+
{
46+
}
47+
}

0 commit comments

Comments
 (0)