Types¶
This document attempts to show all the types that Phpactor supports.
For more information about types see:
Basic Types¶
Name |
Example |
PHP |
Phpactor |
---|---|---|---|
Array |
|
|
✔ |
Boolean |
|
|
✔ |
Float |
|
|
✔ |
Int |
|
|
✔ |
Resource |
(internal type) |
|
✔ |
String |
|
|
✔ |
Self |
|
|
✔ |
Parent |
|
|
✔ |
Callable |
|
|
✔ |
Iterable |
|
|
✔ |
Nullable |
|
|
✔ |
Object |
|
|
✔ |
Union |
|
|
✔ |
Mixed |
|
|
✔ |
Intersection |
|
|
✔ |
Return Only Types¶
Name |
Example |
PHP |
Phpactor |
Notes |
---|---|---|---|---|
Void |
|
|
✔ |
|
Static |
|
|
✔ |
|
Never |
|
|
✔ |
|
False |
|
|
✔ |
Pseudo-type before 8.2 |
Null |
|
|
✔ |
Docblock Types¶
Name |
Example |
Phpactor |
---|---|---|
Array Key |
|
✔ |
Array Literal |
|
✔ |
Array Shape |
|
✔ |
Class String |
|
✔ |
Closure |
|
✔ |
Float Literal |
|
✔ |
Generics |
|
✔ |
Int Literal |
|
✔ |
Int Range |
|
✔ |
Int Positive |
|
✔ |
Int Negative |
|
✔ |
List |
|
✔ |
Parenthesized |
|
✔ |
String Literal |
|
✔ |
This |
|
✔ |
Integer Types¶
Example |
PHP |
Supported |
Description |
---|---|---|---|
|
|
✔ |
Integer |
|
|
✔ |
Binary type |
|
|
✔ |
Hexidecimal |
|
|
✔ |
Octal |
|
|
✔ |
Decimal |
|
|
✘ |
Octal |
Conditional Types¶
Phpactor undestands conditional return types of the form:
/**
* @return (
* $array is array<int>
* ? int
* : ($array is array<float>
* ? float
* : float|int
* )
* )
*/
function array_some(array $array) {
return array_sum($array);
}
Generic Types¶
Phpactor understands Generic (or templated) types. See PHPStan or Psalm documentation for what these are and how they work.
Phpactor supports:
@implements
and@extends
in addition to@template-extends
and@template-implements
.@template
and@template T of Foo
Injecting template variables into the constructor.
Method level template vars.
class-string<T>
For example:
<?php
/**
* @template T
*/
class Foo {
/**
* @var T
*/
private $a;
/** @param T $a */
public function __construct($a) {
$this->a = $a;
}
/**
* @return T
*/
public function a()
{
return $this->a;
}
}
$f = new Foo(new Bar());
$bar = $f->a(); // Phpactor now knows that `$bar` is Bar
In addition Phpactor supports class-string<T> which allows you to capture a
class type from a class string (e.g. MyClass::class
is interpreted as a
class-string. The following extract is from the Phpactor Container.
<?php
interface Container
{
/**
* @template T of object
* @param class-string<T>|string $id
* @return ($id is class-string<T> ? T : mixed)
*/
public function get($id);
}
The conditional type enables the return value of get
to be an object of
class T
if the $id
is a class-string
or mixed
in any other
case.