Skip to content

Commit

Permalink
Add SelfOut attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 24, 2024
1 parent bc2e06a commit 3394d5d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` |
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
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.12 || dev-main",
"php-static-analysis/node-visitor": "^0.1.12 || dev-main",
"php-static-analysis/attributes": "^0.1.13 || dev-main",
"php-static-analysis/node-visitor": "^0.1.13 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
26 changes: 26 additions & 0 deletions tests/SelfOutAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension;

class SelfOutAttributeTest extends BaseAttributeTestCase
{
public function testMethodSelfOutAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/SelfOut/MethodSelfOutAttribute.php');
$this->assertCount(0, $errors);
}

public function testInvalidMethodSelfOutAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/SelfOut/InvalidMethodSelfOutAttribute.php');

$expectedErrors = [
'PHPDoc tag @phpstan-self-out has invalid value (): Unexpected token "\n ", expected type at offset 75' => 12,
'Parameter #1 $type of attribute class PhpStaticAnalysis\Attributes\SelfOut constructor expects string, int given.' => 14,
'Attribute class PhpStaticAnalysis\Attributes\SelfOut is not repeatable but is already present above the method.' => 22,
'Attribute class PhpStaticAnalysis\Attributes\SelfOut does not have the property target.' => 27,
];

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

namespace test\PhpStaticAnalysis\PHPStanExtension\data\SelfOut;

use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\SelfOut;
use PhpStaticAnalysis\Attributes\Template;

#[Template('TValue')]
class InvalidMethodSelfOutAttribute
{
#[Template('TItemValue')]
#[Param(item: 'TItemValue')]
#[SelfOut(0)]
public function add($item): void
{
}

#[Template('TItemValue')]
#[Param(item: 'TItemValue')]
#[SelfOut('self<TValue|TItemValue>')]
#[SelfOut('self<TValue|TItemValue>')]
public function addMore($item): void
{
}

#[SelfOut('self<TValue|TItemValue>')]
public string $property;
}
47 changes: 47 additions & 0 deletions tests/data/SelfOut/MethodSelfOutAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\SelfOut;

use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\SelfOut;
use PhpStaticAnalysis\Attributes\Template;

#[Template('TValue')]
class MethodSelfOutAttribute
{
#[Template('TItemValue')]
#[Param(item: 'TItemValue')]
#[SelfOut('self<TValue|TItemValue>')] // we specify the new type
public function add($item): void
{
}

/**
* @deprecated
*/
#[Template('TItemValue')]
#[Param(item: 'TItemValue')]
#[SelfOut('self<TValue|TItemValue>')]
public function addMore($item): void
{
}

/**
* @self-out self<TValue>
*/
#[Template('TItemValue')]
#[Param(item: 'TItemValue')]
#[SelfOut('self<TValue|TItemValue>')]
public function addEvenMore($item): void
{
}

/**
* @self-out self<TValue|TItemValue>
*/
#[Template('TItemValue')]
#[Param(item: 'TItemValue')]
public function addMoreAndMore($item): void
{
}
}

0 comments on commit 3394d5d

Please sign in to comment.