[
'T = $T;
}
/**
* @return T
*/
public function bar() {
$t = $this->T;
return new $t();
}
}
$at = "A";
/** @var Foo */
$afoo = new Foo($at);
$afoo_bar = $afoo->bar();
$bfoo = new Foo(B::class);
$bfoo_bar = $bfoo->bar();
// this shouldn’t cause a problem as it’s a docbblock type
if (!($bfoo_bar instanceof B)) {}
$cfoo = new Foo("C");
$cfoo_bar = $cfoo->bar();
$dt = "D";
$dfoo = new Foo($dt);',
'assertions' => [
'$afoo' => 'Foo',
'$afoo_bar' => 'A',
'$bfoo' => 'Foo',
'$bfoo_bar' => 'B',
'$cfoo' => 'Foo',
'$cfoo_bar' => 'C',
'$dfoo' => 'Foo',
],
'error_levels' => [
'MixedReturnStatement',
'LessSpecificReturnStatement',
'RedundantConditionGivenDocblockType',
],
],
'classTemplateSelfs' => [
'T = $T;
}
/**
* @return T
*/
public function bar() {
$t = $this->T;
return new $t();
}
}
class E {
/**
* @return Foo
*/
public static function getFoo() {
return new Foo(__CLASS__);
}
/**
* @return Foo
*/
public static function getFoo2() {
return new Foo(self::class);
}
/**
* @return Foo
*/
public static function getFoo3() {
return new Foo(static::class);
}
}
class G extends E {}
$efoo = E::getFoo();
$efoo2 = E::getFoo2();
$efoo3 = E::getFoo3();
$gfoo = G::getFoo();
$gfoo2 = G::getFoo2();
$gfoo3 = G::getFoo3();',
'assertions' => [
'$efoo' => 'Foo',
'$efoo2' => 'Foo',
'$efoo3' => 'Foo',
'$gfoo' => 'Foo',
'$gfoo2' => 'Foo',
'$gfoo3' => 'Foo',
],
'error_levels' => [
'MixedReturnStatement',
'LessSpecificReturnStatement',
'RedundantConditionGivenDocblockType',
],
],
'classTemplateExternalClasses' => [
'T = $T;
}
/**
* @return T
*/
public function bar() {
$t = $this->T;
return new $t();
}
}
$efoo = new Foo(Exception::class)
$efoo_bar = $efoo->bar();
$ffoo = new Foo("LogicException");
$ffoo_bar = $ffoo->bar();',
'assertions' => [
'$efoo' => 'Foo',
'$efoo_bar' => 'Exception',
'$ffoo' => 'Foo',
'$ffoo_bar' => 'LogicException',
],
'error_levels' => ['MixedReturnStatement', 'LessSpecificReturnStatement'],
],
'classTemplateContainer' => [
'obj = $obj;
}
/**
* @return T
*/
public function bar() {
return $this->obj;
}
/**
* @return T
*/
public function bat() {
return $this->bar();
}
public function __toString(): string {
return "hello " . $this->obj;
}
}
$afoo = new Foo(new A());
$afoo_bar = $afoo->bar();',
'assertions' => [
'$afoo' => 'Foo',
'$afoo_bar' => 'A',
],
'error_levels' => ['MixedOperand'],
],
'phanTuple' => [
'_0 = $_0;
}
/**
* @return int
* The arity of this tuple
*/
public function arity(): int
{
return (int)static::ARITY;
}
/**
* @return array
* An array of all elements in this tuple.
*/
public function toArray(): array
{
return [
$this->_0,
];
}
}
/**
* A tuple of 2 elements.
*
* @template T0
* The type of element zero
*
* @template T1
* The type of element one
*
* @inherits Tuple1
*/
class Tuple2 extends Tuple1
{
/** @var int */
const ARITY = 2;
/** @var T1 */
public $_1;
/**
* @param T0 $_0
* The 0th element
*
* @param T1 $_1
* The 1st element
*/
public function __construct($_0, $_1) {
parent::__construct($_0);
$this->_1 = $_1;
}
/**
* @return array
* An array of all elements in this tuple.
*/
public function toArray(): array
{
return [
$this->_0,
$this->_1,
];
}
}
$a = new Tuple2("cool", 5);
/** @return void */
function takes_int(int $i) {}
/** @return void */
function takes_string(string $s) {}
takes_string($a->_0);
takes_int($a->_1);',
],
'validTemplatedType' => [
' [
' [
'foo("string"));',
],
'genericArrayKeys' => [
' $arr
* @return array
*/
function my_array_keys($arr) {
return array_keys($arr);
}
$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);',
'assertions' => [
'$a' => 'array',
],
],
'genericArrayFlip' => [
' $arr
* @return array
*/
function my_array_flip($arr) {
return array_flip($arr);
}
$b = my_array_flip(["hello" => 5, "goodbye" => 6]);',
'assertions' => [
'$b' => 'array',
],
],
'genericArrayPop' => [
' $arr
* @return TValue|null
*/
function my_array_pop(array &$arr) {
return array_pop($arr);
}
/** @var mixed */
$b = ["a" => 5, "c" => 6];
$a = my_array_pop($b);',
'assertions' => [
'$a' => 'mixed',
'$b' => 'array',
],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
],
'intersectionTemplatedTypes' => [
' */
private $data;
/** @psalm-param iterable $data */
public function __construct(iterable $data) {
$this->data = $data;
}
}
class Item {}
/** @psalm-param Collection- $c */
function takesCollectionOfItems(Collection $c): void {}
/** @psalm-var iterable
- $data2 */
$data2 = [];
takesCollectionOfItems(new Collection($data2));
/** @psalm-var iterable
- &Countable $data */
$data = [];
takesCollectionOfItems(new Collection($data));',
],
'templateCallableReturnType' => [
' [
' [
' 'InvalidScalarArgument',
],
'invalidTemplatedStaticMethodType' => [
' 'InvalidScalarArgument',
],
'invalidTemplatedInstanceMethodType' => [
'foo(4));',
'error_message' => 'InvalidScalarArgument',
],
];
}
}