Skip to content

Commit

Permalink
Add Pure and Impure attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Feb 25, 2024
1 parent d726548 commit 4c9f253
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 22 deletions.
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ 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` |
| [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` |
| [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` |
| [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` |
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.14 || dev-main",
"php-static-analysis/node-visitor": "^0.1.14 || dev-main",
"php-static-analysis/attributes": "^0.1.15 || dev-main",
"php-static-analysis/node-visitor": "^0.1.15 || dev-main",
"vimeo/psalm": "^5"
},
"require-dev": {
Expand Down
30 changes: 30 additions & 0 deletions tests/PureAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin;

class PureAttributeTest extends BaseAttributeTestCase
{
public function testMethodPureAttribute(): void
{
$errors = $this->analyzeTestFile('/data/Pure/MethodPureAttribute.php');
$this->assertCount(0, $errors);
}

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

public function testInvalidMethodPureAttribute(): void
{
$errors = $this->analyzeTestFile('/data/Pure/InvalidMethodPureAttribute.php');

$expectedErrors = [
'Attribute Pure is not repeatable' => 15,
'Attribute Pure cannot be used on a property' => 11,
];

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

namespace test\PhpStaticAnalysis\PsalmPlugin\data;

use PhpStaticAnalysis\Attributes\Pure;

#[Pure]
function add(int $left, int $right): int
{
return $left + $right;
}
19 changes: 19 additions & 0 deletions tests/data/Pure/InvalidMethodPureAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Pure;

use PhpStaticAnalysis\Attributes\Param;
use PhpStaticAnalysis\Attributes\Pure;
use PhpStaticAnalysis\Attributes\Returns;

class InvalidMethodPureAttribute
{
#[Pure]
public string $name = '';

#[Pure]
#[Pure]
public function getMoreName(): void
{
}
}
31 changes: 31 additions & 0 deletions tests/data/Pure/MethodPureAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace test\PhpStaticAnalysis\PsalmPlugin\data\Pure;

use PhpStaticAnalysis\Attributes\Pure;

class MethodPureAttribute
{
#[Pure]
public function add(int $left, int $right): int
{
return $left + $right;
}

/**
* @codeCoverageIgnore
*/
#[Pure]
public function addAnother(int $left, int $right): int
{
return $left + $right;
}

/**
* @pure
*/
public function addMore(int $left, int $right): int
{
return $left + $right;
}
}

0 comments on commit 4c9f253

Please sign in to comment.