Skip to content

Commit 4c9f253

Browse files
Add Pure and Impure attributes
1 parent d726548 commit 4c9f253

File tree

6 files changed

+115
-22
lines changed

6 files changed

+115
-22
lines changed

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,25 @@ These are the available attributes and their corresponding PHPDoc annotations:
9999

100100
| Attribute | PHPDoc Annotations |
101101
|---------------------------------------------------------------------------------------------------|--------------------|
102-
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
103-
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
104-
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
105-
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
106-
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
107-
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
108-
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
109-
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
110-
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
111-
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
112-
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
113-
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
114-
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
115-
| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` |
116-
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
117-
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
118-
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
119-
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
120-
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
121-
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
102+
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
103+
| [Impure](https://github.com/php-static-analysis/attributes/blob/main/doc/Impure.md) | `@impure` |
104+
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
105+
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
106+
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
107+
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
108+
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
109+
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
110+
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
111+
| [PropertyRead](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyRead.md) | `@property-read` |
112+
| [PropertyWrite](https://github.com/php-static-analysis/attributes/blob/main/doc/PropertyWrite.md) | `@property-write` |
113+
| [Pure](https://github.com/php-static-analysis/attributes/blob/main/doc/Pure.md) | `@pure` |
114+
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
115+
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
116+
| [Returns](https://github.com/php-static-analysis/attributes/blob/main/doc/Returns.md) | `@return` |
117+
| [SelfOut](https://github.com/php-static-analysis/attributes/blob/main/doc/SelfOut.md) | `@self-out` `@this-out` |
118+
| [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
119+
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
120+
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
121+
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
122+
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
123+
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"require": {
2727
"php": ">=8.0",
2828
"ext-simplexml": "*",
29-
"php-static-analysis/attributes": "^0.1.14 || dev-main",
30-
"php-static-analysis/node-visitor": "^0.1.14 || dev-main",
29+
"php-static-analysis/attributes": "^0.1.15 || dev-main",
30+
"php-static-analysis/node-visitor": "^0.1.15 || dev-main",
3131
"vimeo/psalm": "^5"
3232
},
3333
"require-dev": {

tests/PureAttributeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin;
4+
5+
class PureAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testMethodPureAttribute(): void
8+
{
9+
$errors = $this->analyzeTestFile('/data/Pure/MethodPureAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testFunctionPureAttribute(): void
14+
{
15+
$errors = $this->analyzeTestFile('/data/Pure/FunctionPureAttribute.php');
16+
$this->assertCount(0, $errors);
17+
}
18+
19+
public function testInvalidMethodPureAttribute(): void
20+
{
21+
$errors = $this->analyzeTestFile('/data/Pure/InvalidMethodPureAttribute.php');
22+
23+
$expectedErrors = [
24+
'Attribute Pure is not repeatable' => 15,
25+
'Attribute Pure cannot be used on a property' => 11,
26+
];
27+
28+
$this->checkExpectedErrors($errors, $expectedErrors);
29+
}
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data;
4+
5+
use PhpStaticAnalysis\Attributes\Pure;
6+
7+
#[Pure]
8+
function add(int $left, int $right): int
9+
{
10+
return $left + $right;
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PsalmPlugin\data\Pure;
4+
5+
use PhpStaticAnalysis\Attributes\Param;
6+
use PhpStaticAnalysis\Attributes\Pure;
7+
use PhpStaticAnalysis\Attributes\Returns;
8+
9+
class InvalidMethodPureAttribute
10+
{
11+
#[Pure]
12+
public string $name = '';
13+
14+
#[Pure]
15+
#[Pure]
16+
public function getMoreName(): void
17+
{
18+
}
19+
}
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\PsalmPlugin\data\Pure;
4+
5+
use PhpStaticAnalysis\Attributes\Pure;
6+
7+
class MethodPureAttribute
8+
{
9+
#[Pure]
10+
public function add(int $left, int $right): int
11+
{
12+
return $left + $right;
13+
}
14+
15+
/**
16+
* @codeCoverageIgnore
17+
*/
18+
#[Pure]
19+
public function addAnother(int $left, int $right): int
20+
{
21+
return $left + $right;
22+
}
23+
24+
/**
25+
* @pure
26+
*/
27+
public function addMore(int $left, int $right): int
28+
{
29+
return $left + $right;
30+
}
31+
}

0 commit comments

Comments
 (0)