Skip to content

Commit 9876dee

Browse files
Add Pure and Impure attributes
1 parent 31eac4f commit 9876dee

10 files changed

+219
-25
lines changed

README.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,31 @@ This extension works by interacting with the parser that PHPStan uses to parse t
9494

9595
These are the available attributes and their corresponding PHPDoc annotations:
9696

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

121123

122124

composer.json

Lines changed: 2 additions & 2 deletions
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.14 || dev-main",
28-
"php-static-analysis/node-visitor": "^0.1.14 || dev-main",
27+
"php-static-analysis/attributes": "^0.1.15 || dev-main",
28+
"php-static-analysis/node-visitor": "^0.1.15 || dev-main",
2929
"phpstan/phpstan": "^1.8"
3030
},
3131
"require-dev": {

tests/ImpureAttributeTest.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\PHPStanExtension;
4+
5+
class ImpureAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testMethodImpureAttribute(): void
8+
{
9+
$errors = $this->analyse(__DIR__ . '/data/Impure/MethodImpureAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testFunctionImpureAttribute(): void
14+
{
15+
$errors = $this->analyse(__DIR__ . '/data/Impure/FunctionImpureAttribute.php');
16+
$this->assertCount(0, $errors);
17+
}
18+
19+
public function testInvalidMethodImpureAttribute(): void
20+
{
21+
$errors = $this->analyse(__DIR__ . '/data/Impure/InvalidMethodImpureAttribute.php');
22+
23+
$expectedErrors = [
24+
'Attribute class PhpStaticAnalysis\Attributes\Impure does not have the property target.' => 11,
25+
'Attribute class PhpStaticAnalysis\Attributes\Impure is not repeatable but is already present above the method.' => 15,
26+
];
27+
28+
$this->checkExpectedErrors($errors, $expectedErrors);
29+
}
30+
}

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\PHPStanExtension;
4+
5+
class PureAttributeTest extends BaseAttributeTestCase
6+
{
7+
public function testMethodPureAttribute(): void
8+
{
9+
$errors = $this->analyse(__DIR__ . '/data/Pure/MethodPureAttribute.php');
10+
$this->assertCount(0, $errors);
11+
}
12+
13+
public function testFunctionPureAttribute(): void
14+
{
15+
$errors = $this->analyse(__DIR__ . '/data/Pure/FunctionPureAttribute.php');
16+
$this->assertCount(0, $errors);
17+
}
18+
19+
public function testInvalidMethodPureAttribute(): void
20+
{
21+
$errors = $this->analyse(__DIR__ . '/data/Pure/InvalidMethodPureAttribute.php');
22+
23+
$expectedErrors = [
24+
'Attribute class PhpStaticAnalysis\Attributes\Pure does not have the property target.' => 11,
25+
'Attribute class PhpStaticAnalysis\Attributes\Pure is not repeatable but is already present above the method.' => 15,
26+
];
27+
28+
$this->checkExpectedErrors($errors, $expectedErrors);
29+
}
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data;
4+
5+
use PhpStaticAnalysis\Attributes\Impure;
6+
7+
$i = 0;
8+
9+
#[Impure]
10+
function add(int $left): int
11+
{
12+
global $i;
13+
14+
$i += $left;
15+
return $i;
16+
}
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\PHPStanExtension\data\Impure;
4+
5+
use PhpStaticAnalysis\Attributes\Impure;
6+
use PhpStaticAnalysis\Attributes\Param;
7+
use PhpStaticAnalysis\Attributes\Returns;
8+
9+
class InvalidMethodImpureAttribute
10+
{
11+
#[Impure]
12+
public string $name = '';
13+
14+
#[Impure]
15+
#[Impure]
16+
public function getMoreName(): void
17+
{
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Impure;
4+
5+
use PhpStaticAnalysis\Attributes\Impure;
6+
7+
class MethodImpureAttribute
8+
{
9+
public static int $i = 0;
10+
11+
#[Impure]
12+
public static function add(int $left): int
13+
{
14+
self::$i += $left;
15+
return self::$i;
16+
}
17+
18+
/**
19+
* @codeCoverageIgnore
20+
*/
21+
#[Impure]
22+
public function addAnother(int $left): int
23+
{
24+
self::$i += $left;
25+
return self::$i;
26+
}
27+
28+
/**
29+
* @impure
30+
*/
31+
public function addMore(int $left): int
32+
{
33+
self::$i += $left;
34+
return self::$i;
35+
}
36+
}
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\PHPStanExtension\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\PHPStanExtension\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\PHPStanExtension\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)