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 29992c6 commit 2c65f4e
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 26 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,27 @@ This extension works by interacting with the parser that Psalm uses to parse the

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` |
| [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` |
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
| [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` |
| [Pure](https://github.com/php-static-analysis/attributes/blob/main/doc/Pure.md) | `@pure` |
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
| [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` |
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
| 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` |
| [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` |
| [Method](https://github.com/php-static-analysis/attributes/blob/main/doc/Method.md) | `@method` |
| [Mixin](https://github.com/php-static-analysis/attributes/blob/main/doc/Mixin.md) | `@mixin` |
| [Param](https://github.com/php-static-analysis/attributes/blob/main/doc/Param.md) | `@param` |
| [ParamOut](https://github.com/php-static-analysis/attributes/blob/main/doc/ParamOut.md) | `@param-out` |
| [Property](https://github.com/php-static-analysis/attributes/blob/main/doc/Property.md) | `@property` `@var` |
| [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` |
| [Pure](https://github.com/php-static-analysis/attributes/blob/main/doc/Pure.md) | `@pure` |
| [RequireExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireExtends.md) | `@require-extends` |
| [RequireImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/RequireImplements.md) | `@require-implements` |
| [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` |
| [TemplateCovariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateCovariant.md) | `@template-covariant` |
| [TemplateExtends](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateExtends.md) | `@extends` `@template-extends` |
| [TemplateImplements](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateImplements.md) | `@implements` `@template-implements` |
| [TemplateUse](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateUse.md) | `@use` `@template-use` |
| [Type](https://github.com/php-static-analysis/attributes/blob/main/doc/Type.md) | `@var` `@return` |
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"require": {
"php": ">=8.0",
"ext-simplexml": "*",
"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",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
41 changes: 41 additions & 0 deletions tests/ImmutableAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php


namespace test\PhpStaticAnalysis\PsalmPlugin;

class ImmutableAttributeTest extends BaseAttributeTestCase
{
public function testClassImmutableAttribute(): void
{
$errors = $this->analyzeTestFile( '/data/Immutable/ClassImmutableAttribute.php');
$expectedErrors = [
'test\PhpStaticAnalysis\PsalmPlugin\data\Immutable\ClassImmutableAttribute::$name is marked readonly' => 14,
];

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

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

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

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

$expectedErrors = [
'Attribute Immutable is not repeatable' => 10,
'Attribute Immutable cannot be used on a property' => 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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\data\Immutable;

use PhpStaticAnalysis\Attributes\Immutable;

#[Immutable]
trait TraitImmutableAttribute
{
}

0 comments on commit 2c65f4e

Please sign in to comment.