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 189e5ad commit 8032ba7
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 21 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,29 @@ 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` |
| [SelfOutAttributeTest.php](..%2Fphpstan-extension%2Ftests%2FSelfOutAttributeTest.php) | [Template](https://github.com/php-static-analysis/attributes/blob/main/doc/Template.md) | `@template` |
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
| [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` |
| [TemplateContravariant](https://github.com/php-static-analysis/attributes/blob/main/doc/TemplateContravariant.md) | `@template-contravariant` |
| [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` |

### Location of Param and ParamOut attributes

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"require": {
"php": ">=8.0",
"cweagans/composer-patches": "^1.7",
"php-static-analysis/attributes": "^0.1.14 || dev-main",
"php-static-analysis/attributes": "^0.1.15 || dev-main",
"rector/rector": "^0.19 || ^1.0"
},
"require-dev": {
Expand Down
4 changes: 4 additions & 0 deletions config/sets/php-static-analysis-annotations-to-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
declare(strict_types=1);

use PhpStaticAnalysis\Attributes\Deprecated;
use PhpStaticAnalysis\Attributes\Impure;
use PhpStaticAnalysis\Attributes\Internal;
use PhpStaticAnalysis\Attributes\Method;
use PhpStaticAnalysis\Attributes\Mixin;
use PhpStaticAnalysis\Attributes\ParamOut;
use PhpStaticAnalysis\Attributes\PropertyRead;
use PhpStaticAnalysis\Attributes\PropertyWrite;
use PhpStaticAnalysis\Attributes\Pure;
use PhpStaticAnalysis\Attributes\RequireExtends;
use PhpStaticAnalysis\Attributes\RequireImplements;
use PhpStaticAnalysis\Attributes\SelfOut;
Expand All @@ -33,6 +35,7 @@
[
new AnnotationToAttribute('deprecated', Deprecated::class),
new AnnotationToAttribute('extends', TemplateExtends::class),
new AnnotationToAttribute('impure', Impure::class),
new AnnotationToAttribute('implements', TemplateImplements::class),
new AnnotationToAttribute('internal', Internal::class),
new AnnotationToAttribute('method', Method::class),
Expand All @@ -42,6 +45,7 @@
new AnnotationToAttribute('property', Property::class),
new AnnotationToAttribute('property_read', PropertyRead::class),
new AnnotationToAttribute('property_write', PropertyWrite::class),
new AnnotationToAttribute('pure', Pure::class),
new AnnotationToAttribute('readonly', IsReadOnly::class),
new AnnotationToAttribute('require_extends', RequireExtends::class),
new AnnotationToAttribute('require_implements', RequireImplements::class),
Expand Down
89 changes: 89 additions & 0 deletions tests/Fixture/ImpureAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

class ImpureAttributeTest
{
/**
* @impure
*/
public function getName(): void
{
}

/**
* @codeCoverageIgnore
* @impure
*/
public function getMoreNames(): void
{
}

/**
* @impure
*/
#[Param(name:'string')]
public function getAnotherName($name)
{
return "Hello " . $name;
}

/**
* @impure this function is impure
*/
public function getUserName(): void
{
}

/**
* @phpstan-impure
*/
public function getPHPStanName(): void
{
}
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

class ImpureAttributeTest
{
#[\PhpStaticAnalysis\Attributes\Impure]
public function getName(): void
{
}

/**
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Impure]
public function getMoreNames(): void
{
}

#[Param(name:'string')]
#[\PhpStaticAnalysis\Attributes\Impure]
public function getAnotherName($name)
{
return "Hello " . $name;
}

#[\PhpStaticAnalysis\Attributes\Impure] // this function is impure
public function getUserName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Impure]
public function getPHPStanName(): void
{
}
}

?>
101 changes: 101 additions & 0 deletions tests/Fixture/PureAttributeTest.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

class PureAttributeTest
{
/**
* @pure
*/
public function getName(): void
{
}

/**
* @codeCoverageIgnore
* @pure
*/
public function getMoreNames(): void
{
}

/**
* @pure
*/
#[Param(name:'string')]
public function getAnotherName($name)
{
return "Hello " . $name;
}

/**
* @pure this function is pure
*/
public function getUserName(): void
{
}

/**
* @psalm-pure
*/
public function getPsalmName(): void
{
}

/**
* @phpstan-pure
*/
public function getPHPStanName(): void
{
}
}

?>
-----
<?php

namespace test\PhpStaticAnalysis\RectorRule\Fixture;

use PhpStaticAnalysis\Attributes\Param;

class PureAttributeTest
{
#[\PhpStaticAnalysis\Attributes\Pure]
public function getName(): void
{
}

/**
* @codeCoverageIgnore
*/
#[\PhpStaticAnalysis\Attributes\Pure]
public function getMoreNames(): void
{
}

#[Param(name:'string')]
#[\PhpStaticAnalysis\Attributes\Pure]
public function getAnotherName($name)
{
return "Hello " . $name;
}

#[\PhpStaticAnalysis\Attributes\Pure] // this function is pure
public function getUserName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Pure]
public function getPsalmName(): void
{
}

#[\PhpStaticAnalysis\Attributes\Pure]
public function getPHPStanName(): void
{
}
}

?>

0 comments on commit 8032ba7

Please sign in to comment.