1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #3543 - add documentation for @psalm-type and @psalm-import-type (#4291)

This commit is contained in:
Leighton Thomas 2020-10-06 23:58:25 +01:00 committed by GitHub
parent a12fbb7f90
commit 2b5b255ad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -403,6 +403,61 @@ $username = $_GET['username']; // prints something like "test.php:4 $username: m
See [Security Analysis annotations](../security_analysis/annotations.md).
### `@psalm-type`
This allows you to define an alias for another type.
```php
<?php
/**
* @psalm-type PhoneType = array{phone: string}
*/
class Phone {
/**
* @psalm-return PhoneType
*/
public function toArray(): array {
return ["phone" => "Nokia"];
}
}
```
### `@psalm-import-type`
You can use this annotation to import a type defined with [`@psalm-type`](#psalm-type) if it was defined somewhere else.
```php
<?php
/**
* @psalm-import-type PhoneType from Phone
*/
class User {
/**
* @psalm-return PhoneType
*/
public function toArray(): array {
return array_merge([], (new Phone())->toArray());
}
}
```
You can also alias a type when you import it:
```php
<?php
/**
* @psalm-import-type PhoneType from Phone as MyPhoneTypeAlias
*/
class User {
/**
* @psalm-return MyPhoneTypeAlias
*/
public function toArray(): array {
return array_merge([], (new Phone())->toArray());
}
}
```
## Type Syntax
Psalm supports PHPDocs [type syntax](https://docs.phpdoc.org/latest/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).