Skip to content
Permalink
Browse files

Add docs for @psalm-pure

  • Loading branch information...
muglug committed Sep 8, 2019
1 parent 5d08c47 commit f8b8f35c031e2fad6754a961a9fad2708649d8da
Showing with 27 additions and 0 deletions.
  1. +27 −0 docs/annotating_code/supported_annotations.md
@@ -257,6 +257,33 @@ class E {

Used to annotate a class where every property is treated by consumers as `@psalm-readonly` and every method is treated as `@psalm-mutation-free`.

### `@psalm-pure`

Used to annotate a [pure function](https://en.wikipedia.org/wiki/Pure_function) - one whose output is just a function of its input.

```php
class Arithmetic {
/** @psalm-pure */
public static function add(int $left, int $right) : int {
return $left + $right;
}
/** @psalm-pure - this is wrong */
public static function addCumulative(int $left) : int {
/** @var int */
static $i = 0; // this is a side effect, and thus a bug
$i += $left;
return $i;
}
}
echo Arithmetic::add(40, 2);
echo Arithmetic::add(40, 2); // same value is emitted
echo Arithmetic::addCumulative(3); // outputs 3
echo Arithmetic::addCumulative(3); // outputs 6
```

## Type Syntax

Psalm supports PHPDoc’s [type syntax](https://docs.phpdoc.org/guides/types.html), and also the [proposed PHPDoc PSR type syntax](https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types).

0 comments on commit f8b8f35

Please sign in to comment.
You can’t perform that action at this time.