Skip to content

Commit

Permalink
Add Immutable attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 28, 2024
1 parent 69c03bb commit 3f1c657
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ These are the available attributes and their corresponding PHPDoc annotations:
| Attribute | PHPDoc Annotations |
|-------------------------------------------------------------------------------------------------------------------|--------------------------------------|
| [Deprecated](https://github.com/php-static-analysis/attributes/blob/main/doc/Deprecated.md) | `@deprecated` |
| [Immmutable](https://github.com/php-static-analysis/attributes/blob/main/doc/Immmutable.md) | `@immmutable` |
| [Impure](https://github.com/php-static-analysis/attributes/blob/main/doc/Impure.md) | `@impure` |
| [Internal](https://github.com/php-static-analysis/attributes/blob/main/doc/Internal.md) | `@internal` |
| [IsReadOnly](https://github.com/php-static-analysis/attributes/blob/main/doc/IsReadOnly.md) | `@readonly` |
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.16 || dev-main",
"php-static-analysis/node-visitor": "^0.1.16 || dev-main",
"php-static-analysis/attributes": "^0.1.17 || dev-main",
"php-static-analysis/node-visitor": "^0.1.17 || dev-main",
"phpstan/phpstan": "^1.8"
},
"require-dev": {
Expand Down
40 changes: 40 additions & 0 deletions tests/ImmutableAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php


use test\PhpStaticAnalysis\PHPStanExtension\BaseAttributeTestCase;

class ImmutableAttributeTest extends BaseAttributeTestCase
{
public function testClassImmutableAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Immutable/ClassImmutableAttribute.php');
$expectedErrors = [
];

$this->checkExpectedErrors($errors, $expectedErrors);
}

public function testTraitImmutableAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Immutable/TraitImmutableAttribute.php');
$this->assertCount(0, $errors);
}

public function testInterfaceImmutableAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Immutable/InterfaceImmutableAttribute.php');
$this->assertCount(0, $errors);
}

public function testInvalidClassImmutableAttribute(): void
{
$errors = $this->analyse(__DIR__ . '/data/Immutable/InvalidClassImmutableAttribute.php');

$expectedErrors = [
'Attribute class PhpStaticAnalysis\Attributes\Immutable is not repeatable but is already present above the class.' => 10,
'Attribute class PhpStaticAnalysis\Attributes\Immutable does not have the property target.' => 13,
];

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

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;

use PhpStaticAnalysis\Attributes\Immutable;

#[Immutable] // All properties are read only
class ClassImmutableAttribute
{
public string $name = '';
}

$class = new ClassImmutableAttribute();
$class->name = 'John';
10 changes: 10 additions & 0 deletions tests/data/Immutable/InterfaceImmutableAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;

use PhpStaticAnalysis\Attributes\Immutable;

#[Immutable]
interface InterfaceImmutableAttribute
{
}
15 changes: 15 additions & 0 deletions tests/data/Immutable/InvalidClassImmutableAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;

use PhpStaticAnalysis\Attributes\Immutable;
use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\Returns;

#[Immutable]
#[Immutable]
class InvalidClassImmutableAttribute
{
#[Immutable]
public string $name = '';
}
10 changes: 10 additions & 0 deletions tests/data/Immutable/TraitImmutableAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace test\PhpStaticAnalysis\PHPStanExtension\data\Immutable;

use PhpStaticAnalysis\Attributes\Immutable;

#[Immutable]
trait TraitImmutableAttribute
{
}

0 comments on commit 3f1c657

Please sign in to comment.