Skip to content

Commit 3f1c657

Browse files
Add Immutable attribute
1 parent 69c03bb commit 3f1c657

7 files changed

+92
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
9797
| Attribute | PHPDoc Annotations |
9898
|-------------------------------------------------------------------------------------------------------------------|--------------------------------------|
9999
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
100+
| [Immmutable](https://github.com/php-static-analysis/attributes/blob/main/doc/Immmutable.md) | `@immmutable` |
100101
| [Impure](https://github.com/php-static-analysis/attributes/blob/main/doc/Impure.md) | `@impure` |
101102
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
102103
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |

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

tests/ImmutableAttributeTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
4+
use test\PhpStaticAnalysis\PHPStanExtension\BaseAttributeTestCase;
5+
6+
class ImmutableAttributeTest extends BaseAttributeTestCase
7+
{
8+
public function testClassImmutableAttribute(): void
9+
{
10+
$errors = $this->analyse(__DIR__ . '/data/Immutable/ClassImmutableAttribute.php');
11+
$expectedErrors = [
12+
];
13+
14+
$this->checkExpectedErrors($errors, $expectedErrors);
15+
}
16+
17+
public function testTraitImmutableAttribute(): void
18+
{
19+
$errors = $this->analyse(__DIR__ . '/data/Immutable/TraitImmutableAttribute.php');
20+
$this->assertCount(0, $errors);
21+
}
22+
23+
public function testInterfaceImmutableAttribute(): void
24+
{
25+
$errors = $this->analyse(__DIR__ . '/data/Immutable/InterfaceImmutableAttribute.php');
26+
$this->assertCount(0, $errors);
27+
}
28+
29+
public function testInvalidClassImmutableAttribute(): void
30+
{
31+
$errors = $this->analyse(__DIR__ . '/data/Immutable/InvalidClassImmutableAttribute.php');
32+
33+
$expectedErrors = [
34+
'Attribute class PhpStaticAnalysis\Attributes\Immutable is not repeatable but is already present above the class.' => 10,
35+
'Attribute class PhpStaticAnalysis\Attributes\Immutable does not have the property target.' => 13,
36+
];
37+
38+
$this->checkExpectedErrors($errors, $expectedErrors);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;
4+
5+
use PhpStaticAnalysis\Attributes\Immutable;
6+
7+
#[Immutable] // All properties are read only
8+
class ClassImmutableAttribute
9+
{
10+
public string $name = '';
11+
}
12+
13+
$class = new ClassImmutableAttribute();
14+
$class->name = 'John';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;
4+
5+
use PhpStaticAnalysis\Attributes\Immutable;
6+
7+
#[Immutable]
8+
interface InterfaceImmutableAttribute
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;
4+
5+
use PhpStaticAnalysis\Attributes\Immutable;
6+
use PhpStaticAnalysis\Attributes\Param;
7+
use PhpStaticAnalysis\Attributes\Returns;
8+
9+
#[Immutable]
10+
#[Immutable]
11+
class InvalidClassImmutableAttribute
12+
{
13+
#[Immutable]
14+
public string $name = '';
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;
4+
5+
use PhpStaticAnalysis\Attributes\Immutable;
6+
7+
#[Immutable]
8+
trait TraitImmutableAttribute
9+
{
10+
}

0 commit comments

Comments
 (0)