Type
Use PHPDoc types for richer types that are not supported by native PHP.
E.g.: Assign an associative array of the resource class body
as an “object-like array”.
/** @var array{greeting: string} */
public $body;
/** @var list<array{name: string, age:int}> */
public $body;
The tool understands the object’s type retrieved by the resource client with assert()
.
$user = $this->resource->get('/user', []);
assert($user instanceof User);
$name = $user->body['name']; // The name key will be interpolated
$user->body['__invalid__']; // Accessing an undefined key is an error.
Reference
Atomic Type
If a type cannot be split any more, it is called an atomic type; all PHP7 types are of this type. Union and intersection types use a combination of atomic types.
Scalar Type
/** @param int $i */
/** @param float $f */
/** @param string $str */
/** @param class-string $class */
/** @param class-string<AbstractFoo> $fooClass */
/** @param callable-string $callable */
/** @param numeric-string $num */
/** @param bool $isSet */
/** @param array-key $key */
/** @param numeric $num */
/** @param scalar $a */
Object Type
Generic Object
/** @return ArrayObject<int, string> */
Generator
/** @return Generator<int, string, mixed, void> */
Callable Type
/** @return callable(Type1, OptionalType2=, SpreadType3...): ReturnType */
/** @return Closure(bool):int */
Value Type
/** @return null */
/** @return true */
/** @return 42 */
/** Foo\Bar::MY_SCALAR_CONST $a */
/** @param A::class|B::class $s */
Array Type
Generic Array
/** @return array<TKey, TValue> */
/** @return array<int, Foo> */
/** @return array<string, int|string> */
Object-like Arrays
/** @return array{0: string, 1: string, foo: stdClass, 28: false} */
/** @return array{foo: string, bar: int} */
/** @return array{optional?: string, bar: int} */
List
/** @param list<string> $stringList */
PHPDoc Array
/** @param string[] $strings */
Other Atomic Type
/** @return iterable */
/** @return void */
/** @return empty */
/** @return mixed */
Intersection Type
/** @return A&B */
Union Type
/** @return int|false */
/** @return 0|1 */
/** @return 'a'|'b' */
See more..
- psalm - atomic_types | union | intersection
- phpstan - phpdoc type
PHPStorm Plugin