1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00
psalm/docs/annotating_code/supported_annotations.md

182 lines
6.3 KiB
Markdown
Raw Normal View History

2018-01-30 00:14:36 -05:00
# Supported docblock annotations
Psalm supports a wide range of docblock annotations.
## PHPDoc tags
Psalm uses the following PHPDoc tags to understand your code:
2018-03-08 17:26:22 -05:00
- [`@var`](https://docs.phpdoc.org/references/phpdoc/tags/var.html)
2018-01-30 11:31:17 -05:00
Used for specifying the types of properties and variables
2018-03-08 17:26:22 -05:00
- [`@return`](https://docs.phpdoc.org/references/phpdoc/tags/return.html)
2018-01-30 11:31:17 -05:00
Used for specifying the return types of functions, methods and closures
2018-03-08 17:26:22 -05:00
- [`@param`](https://docs.phpdoc.org/references/phpdoc/tags/param.html)
2018-01-30 11:31:17 -05:00
Used for specifying types of parameters passed to functions, methods and closures
2018-03-08 17:26:22 -05:00
- [`@property`](https://docs.phpdoc.org/references/phpdoc/tags/property.html)
2018-01-30 11:31:17 -05:00
Used to specify what properties can be accessed on an object that uses `__get` and `__set`
2018-03-08 17:26:22 -05:00
- [`@property-read`](https://docs.phpdoc.org/references/phpdoc/tags/property-read.html)
2018-01-30 11:31:17 -05:00
Used to specify what properties can be read on object that uses `__get`
2018-03-08 17:26:22 -05:00
- [`@property-write`](https://docs.phpdoc.org/references/phpdoc/tags/property-write.html)
2018-01-30 11:31:17 -05:00
Used to specify what properties can be written on object that uses `__set`
2018-03-08 17:26:22 -05:00
- [`@deprecated`](https://docs.phpdoc.org/references/phpdoc/tags/deprecated.html)
2018-01-30 11:31:17 -05:00
Used to mark functions, methods, classes and interfaces as being deprecated
- [`@internal`](https://docs.phpdoc.org/references/phpdoc/tags/internal.html)
used to mark classes, functions and properties that are internal to an application or library.
2018-01-30 00:14:36 -05:00
### Off-label usage of the `@var` tag
The `@var` tag is supposed to only be used for properties. Psalm, taking a lead from PHPStorm and other static analysis tools, allows its use inline in the form `@var Type [VariableReference]`.
If `VariableReference` is provided, it should be of the form `$variable` or `$variable->property`. If used above an assignment, Psalm checks whether the `VariableReference` matches the variable being assigned. If they differ, Psalm will assign the `Type` to `VariableReference` and use it in the expression below.
2018-01-30 11:31:17 -05:00
If no `VariableReference` is given, the annotation tells Psalm that the right hand side of the expression, whether an assignment or a return, is of type `Type`.
2018-01-30 00:14:36 -05:00
```php
/** @var string */
$a = $_GET['foo'];
/** @var string $b */
$b = $_GET['bar'];
function bat(): string {
/** @var string */
return $_GET['bat'];
}
```
## Psalm-specific tags
There are a number of custom tags that determine how Psalm treats your code.
2019-01-20 12:50:47 -05:00
### `@param-out`
This is used to specify that a by-ref type is different from the one that entered. In the function below the first param can be null, but once the function has executed the by-ref value is not null.
```php
/**
* @param-out string $s
*/
function addFoo(?string &$s) : void {
if ($s === null) {
$s = "hello";
}
$s .= "foo";
}
```
2018-01-30 00:14:36 -05:00
### `@psalm-var`, `@psalm-param` and `@psalm-return`
2019-02-12 23:27:31 +01:00
When specifying types in a format not supported by phpDocumentor ([but supported by Psalm](#type-syntax)) you may wish to prepend `@psalm-` to the PHPDoc tag, so as to avoid confusing your IDE. If a `@psalm`-prefixed tag is given, Psalm will use it in place of its non-prefixed counterpart.
2018-01-30 00:14:36 -05:00
### `@psalm-suppress SomeIssueName`
This annotation is used to suppress issues. It can be used in function docblocks, class docblocks and also inline, applying to the following statement.
Function docblock example:
```php
/**
* @psalm-suppress PossiblyNullOperand
*/
function addString(?string $s) {
echo "hello " . $s;
}
```
Inline example:
```php
function addString(?string $s) {
/** @psalm-suppress PossiblyNullOperand */
echo "hello " . $s;
}
```
2018-06-03 23:38:06 -04:00
### `@psalm-assert`, `@psalm-assert-if-true` and `@psalm-assert-if-false`
See [Adding assertions](adding_assertions.md).
2018-01-30 00:14:36 -05:00
### `@psalm-ignore-nullable-return`
This can be used to tell Psalm not to worry if a function/method returns null. Its a bit of a hack, but occasionally useful for scenarios where you either have a very high confidence of a non-null value, or some other function guarantees a non-null value for that particular code path.
```php
class Foo {}
function takesFoo(Foo $f): void {}
/** @psalm-ignore-nullable-return */
function getFoo(): ?Foo {
return rand(0, 10000) > 1 ? new Foo() : null;
}
takesFoo(getFoo());
```
### `@psalm-ignore-falsable-return`
This provides the same, but for `false`. Psalm uses this internally for functions like `preg_replace`, which can return false if the given input has encoding errors, but where 99.9% of the time the function operates as expected.
2019-01-29 23:26:37 -05:00
### `@psalm-seal-properties`
2018-01-30 00:14:36 -05:00
2019-01-29 23:26:37 -05:00
If you have a magic property getter/setter, you can use `@psalm-seal-properties` to instruct Psalm to disallow getting and setting any properties not contained in a list of `@property` (or `@property-read`/`@property-write`) annotations.
2018-01-30 00:14:36 -05:00
```php
/**
2019-01-29 23:26:37 -05:00
* @property string $foo
* @psalm-seal-properties
*/
class A {
public function __get(string $name): ?string {
if ($name === "foo") {
return "hello";
}
}
public function __set(string $name, $value): void {}
}
$a = new A();
$a->bar = 5; // this call fails
```
### `@psalm-internal`
Used to mark a class, property or function as internal to a given namespace. Psalm treats this slightly differently to
2019-06-28 13:59:09 -04:00
the PHPDoc `@internal` tag. For `@internal`, an issue is raised if the calling code is in a namespace completely
unrelated to the namespace of the calling code, i.e. not sharing the first element of the namespace.
2019-05-15 00:20:52 +01:00
In contrast for `@psalm-internal`, the docbloc line must specify a namespace. An issue is raised if the calling code
is not within the given namespace.
2019-06-28 13:59:09 -04:00
As other tools do not support `@psalm-internal`, it may only be used in conjunction with `@internal`.
```PHP
namespace A\B {
/**
* @internal
* @psalm-internal A\B
*/
class Foo { }
}
namespace A\B\C {
class Bat {
public function batBat(): void {
$a = new \A\B\Foo(); // this is fine
}
}
}
namespace A\C {
class Bat {
public function batBat(): void {
$a = new \A\B\Foo(); // error
}
}
}
```
2018-01-30 00:14:36 -05:00
## Type Syntax
Psalm supports PHPDocs [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).
2019-01-02 13:56:43 -05:00
A detailed write-up is found in [Typing in Psalm](typing_in_psalm.md)