1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00
psalm/tests/TemplateTest.php

792 lines
26 KiB
PHP
Raw Normal View History

2017-02-10 02:35:17 +01:00
<?php
namespace Psalm\Tests;
class TemplateTest extends TestCase
2017-02-10 02:35:17 +01:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2017-02-10 02:35:17 +01:00
/**
* @return array
2017-02-10 02:35:17 +01:00
*/
public function providerFileCheckerValidCodeParse()
2017-02-10 02:35:17 +01:00
{
return [
'classTemplate' => [
'<?php
class A {}
class B {}
class C {}
class D {}
2017-06-29 16:22:49 +02:00
/**
* @template T as object
*/
class Foo {
/** @var string */
public $T;
2017-06-29 16:22:49 +02:00
/**
* @param string $T
* @template-typeof T $T
*/
public function __construct(string $T) {
$this->T = $T;
}
2017-06-29 16:22:49 +02:00
/**
* @return T
*/
public function bar() {
$t = $this->T;
return new $t();
}
}
2017-06-29 16:22:49 +02:00
$at = "A";
2017-06-29 16:22:49 +02:00
/** @var Foo<A> */
$afoo = new Foo($at);
$afoo_bar = $afoo->bar();
2017-06-29 16:22:49 +02:00
$bfoo = new Foo(B::class);
$bfoo_bar = $bfoo->bar();
2017-06-29 16:22:49 +02:00
// this shouldnt cause a problem as its a docbblock type
if (!($bfoo_bar instanceof B)) {}
$cfoo = new Foo("C");
$cfoo_bar = $cfoo->bar();
2017-06-29 16:22:49 +02:00
$dt = "D";
$dfoo = new Foo($dt);',
'assertions' => [
'$afoo' => 'Foo<A>',
'$afoo_bar' => 'A',
'$bfoo' => 'Foo<B>',
'$bfoo_bar' => 'B',
'$cfoo' => 'Foo<C>',
'$cfoo_bar' => 'C',
'$dfoo' => 'Foo<mixed>',
],
'error_levels' => [
'MixedReturnStatement',
'LessSpecificReturnStatement',
'RedundantConditionGivenDocblockType',
],
],
'classTemplateSelfs' => [
'<?php
/**
* @template T as object
*/
class Foo {
/** @var string */
public $T;
/**
* @param string $T
* @template-typeof T $T
*/
public function __construct(string $T) {
$this->T = $T;
}
/**
* @return T
*/
public function bar() {
$t = $this->T;
return new $t();
}
}
class E {
/**
* @return Foo<self>
*/
public static function getFoo() {
return new Foo(__CLASS__);
}
/**
* @return Foo<self>
*/
public static function getFoo2() {
return new Foo(self::class);
}
/**
* @return Foo<static>
*/
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<E>',
'$efoo2' => 'Foo<E>',
'$efoo3' => 'Foo<E>',
'$gfoo' => 'Foo<E>',
'$gfoo2' => 'Foo<E>',
'$gfoo3' => 'Foo<G>',
],
'error_levels' => [
'MixedReturnStatement',
'LessSpecificReturnStatement',
'RedundantConditionGivenDocblockType',
],
],
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
'classTemplateExternalClasses' => [
'<?php
/**
* @template T as object
*/
class Foo {
/** @var string */
public $T;
/**
* @param string $T
* @template-typeof T $T
*/
public function __construct(string $T) {
$this->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<Exception>',
'$efoo_bar' => 'Exception',
'$ffoo' => 'Foo<LogicException>',
'$ffoo_bar' => 'LogicException',
],
'error_levels' => ['MixedReturnStatement', 'LessSpecificReturnStatement'],
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
],
'classTemplateContainer' => [
'<?php
class A {}
2017-06-29 16:22:49 +02:00
/**
* @template T
*/
class Foo {
/** @var T */
public $obj;
2017-06-29 16:22:49 +02:00
/**
* @param T $obj
*/
public function __construct($obj) {
$this->obj = $obj;
}
2017-06-29 16:22:49 +02:00
/**
* @return T
*/
public function bar() {
return $this->obj;
}
2017-06-29 16:22:49 +02:00
/**
* @return T
*/
public function bat() {
return $this->bar();
}
2018-01-11 21:50:45 +01:00
public function __toString(): string {
return "hello " . $this->obj;
}
}
2017-06-29 16:22:49 +02:00
$afoo = new Foo(new A());
$afoo_bar = $afoo->bar();',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$afoo' => 'Foo<A>',
'$afoo_bar' => 'A',
],
'error_levels' => ['MixedOperand'],
],
'phanTuple' => [
'<?php
namespace Phan\Library;
2017-06-29 16:22:49 +02:00
/**
* An abstract tuple.
*/
abstract class Tuple
{
const ARITY = 0;
2017-06-29 16:22:49 +02:00
/**
* @return int
* The arity of this tuple
*/
2018-01-11 21:50:45 +01:00
public function arity(): int
{
return (int)static::ARITY;
}
2017-06-29 16:22:49 +02:00
/**
* @return array
* An array of all elements in this tuple.
*/
2018-01-11 21:50:45 +01:00
abstract public function toArray(): array;
}
2017-06-29 16:22:49 +02:00
/**
* A tuple of 1 element.
*
* @template T0
* The type of element zero
*/
class Tuple1 extends Tuple
{
/** @var int */
const ARITY = 1;
2017-06-29 16:22:49 +02:00
/** @var T0 */
public $_0;
2017-06-29 16:22:49 +02:00
/**
* @param T0 $_0
* The 0th element
*/
public function __construct($_0) {
$this->_0 = $_0;
}
2017-06-29 16:22:49 +02:00
/**
* @return int
* The arity of this tuple
*/
2018-01-11 21:50:45 +01:00
public function arity(): int
{
return (int)static::ARITY;
}
2017-06-29 16:22:49 +02:00
/**
* @return array
* An array of all elements in this tuple.
*/
2018-01-11 21:50:45 +01:00
public function toArray(): array
{
return [
$this->_0,
];
}
}
2017-06-29 16:22:49 +02:00
/**
* A tuple of 2 elements.
*
* @template T0
* The type of element zero
*
* @template T1
* The type of element one
*
* @inherits Tuple1<T0>
*/
class Tuple2 extends Tuple1
{
/** @var int */
const ARITY = 2;
2017-06-29 16:22:49 +02:00
/** @var T1 */
public $_1;
2017-06-29 16:22:49 +02:00
/**
* @param T0 $_0
* The 0th element
*
* @param T1 $_1
* The 1st element
*/
public function __construct($_0, $_1) {
parent::__construct($_0);
$this->_1 = $_1;
}
2017-06-29 16:22:49 +02:00
/**
* @return array
* An array of all elements in this tuple.
*/
2018-01-11 21:50:45 +01:00
public function toArray(): array
{
return [
$this->_0,
$this->_1,
];
}
}
2017-06-29 16:22:49 +02:00
$a = new Tuple2("cool", 5);
2017-06-29 16:22:49 +02:00
/** @return void */
function takes_int(int $i) {}
2017-06-29 16:22:49 +02:00
/** @return void */
function takes_string(string $s) {}
2017-06-29 16:22:49 +02:00
takes_string($a->_0);
2017-05-27 02:05:57 +02:00
takes_int($a->_1);',
],
'validTemplatedType' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
namespace FooFoo;
/**
* @template T
* @param T $x
* @return T
*/
function foo($x) {
return $x;
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function bar(string $a): void { }
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
bar(foo("string"));',
],
'validTemplatedStaticMethodType' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
namespace FooFoo;
class A {
/**
* @template T
* @param T $x
* @return T
*/
public static function foo($x) {
return $x;
}
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function bar(string $a): void { }
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
bar(A::foo("string"));',
],
'validTemplatedInstanceMethodType' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
namespace FooFoo;
class A {
/**
* @template T
* @param T $x
* @return T
*/
public function foo($x) {
return $x;
}
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function bar(string $a): void { }
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
bar((new A())->foo("string"));',
],
'genericArrayKeys' => [
'<?php
/**
* @template T
*
* @param array<T, mixed> $arr
* @return array<int, T>
*/
function my_array_keys($arr) {
return array_keys($arr);
}
2017-06-29 16:22:49 +02:00
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
$a = my_array_keys(["hello" => 5, "goodbye" => new \Exception()]);',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$a' => 'array<int, string>',
2017-05-27 02:05:57 +02:00
],
],
'genericArrayFlip' => [
'<?php
/**
* @template TKey
* @template TValue
*
* @param array<TKey, TValue> $arr
* @return array<TValue, TKey>
*/
function my_array_flip($arr) {
return array_flip($arr);
}
2017-06-29 16:22:49 +02:00
$b = my_array_flip(["hello" => 5, "goodbye" => 6]);',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$b' => 'array<int, string>',
2017-05-27 02:05:57 +02:00
],
],
'genericArrayPop' => [
'<?php
/**
* @template TValue
*
* @param array<mixed, TValue> $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<mixed, mixed>',
],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
],
'intersectionTemplatedTypes' => [
'<?php
namespace NS;
use Countable;
/** @template T */
class Collection
{
/** @psalm-var iterable<T> */
private $data;
/** @psalm-param iterable<T> $data */
public function __construct(iterable $data) {
$this->data = $data;
}
}
class Item {}
/** @psalm-param Collection<Item> $c */
function takesCollectionOfItems(Collection $c): void {}
/** @psalm-var iterable<Item> $data2 */
$data2 = [];
takesCollectionOfItems(new Collection($data2));
/** @psalm-var iterable<Item>&Countable $data */
$data = [];
takesCollectionOfItems(new Collection($data));',
],
'templateCallableReturnType' => [
'<?php
namespace NS;
/**
* @template T
* @psalm-param callable():T $action
* @psalm-return T
*/
function retry(int $maxRetries, callable $action) {
return $action();
}
function takesInt(int $p): void{};
takesInt(retry(1, function(): int { return 1; }));',
],
'templateClosureReturnType' => [
'<?php
namespace NS;
/**
* @template T
* @psalm-param \Closure():T $action
* @psalm-return T
*/
function retry(int $maxRetries, callable $action) {
return $action();
}
function takesInt(int $p): void{};
takesInt(retry(1, function(): int { return 1; }));',
],
'repeatedCall' => [
'<?php
namespace NS;
use Closure;
/**
* @template TKey
* @template TValue
*/
class ArrayCollection {
/** @var array<TKey,TValue> */
private $data;
/** @param array<TKey,TValue> $data */
public function __construct(array $data) {
$this->data = $data;
}
/**
* @template T
* @param Closure(TValue):T $func
* @return ArrayCollection<TKey,T>
*/
public function map(Closure $func) {
return new static(array_map($func, $this->data));
}
}
class Item {}
/**
* @param ArrayCollection<mixed,Item> $i
*/
function takesCollectionOfItems(ArrayCollection $i): void {}
$c = new ArrayCollection([ new Item ]);
takesCollectionOfItems($c);
takesCollectionOfItems($c->map(function(Item $i): Item { return $i;}));
takesCollectionOfItems($c->map(function(Item $i): Item { return $i;}));'
],
'replaceChildType' => [
'<?php
/**
* @template TKey
* @template TValue
* @param Traversable<TKey, TValue> $t
* @return array<TKey, TValue>
*/
function f(Traversable $t): array {
$ret = [];
foreach ($t as $k => $v) $ret[$k] = $v;
return $ret;
}
/** @return Generator<int, stdClass> */
function g():Generator { yield new stdClass; }
takesArrayOfStdClass(f(g()));
/** @param array<stdClass> $p */
function takesArrayOfStdClass(array $p): void {}',
],
'noRepeatedTypeException' => [
'<?php
/** @template T */
class Foo
{
/**
* @psalm-var class-string
*/
private $type;
/** @var array<T> */
private $items;
/**
* @param class-string $type
* @template-typeof T $type
*/
public function __construct(string $type)
{
if (!in_array($type, [A::class, B::class], true)) {
throw new \InvalidArgumentException;
}
$this->type = $type;
$this->items = [];
}
/** @param T $item */
public function add($item): void
{
$this->items[] = $item;
}
}
class FooFacade
{
/**
* @template T
* @param T $item
*/
public function add($item): void
{
$foo = $this->ensureFoo([$item]);
$foo->add($item);
}
/**
* @template T
* @param array<mixed,T> $items
* @return Foo<T>
*/
private function ensureFoo(array $items): EntitySeries
{
$type = $items[0] instanceof A ? A::class : B::class;
return new Foo($type);
}
}
class A {}
class B {}'
],
];
2017-02-10 06:14:44 +01:00
}
2017-02-10 02:35:17 +01:00
/**
* @return array
2017-02-10 02:35:17 +01:00
*/
public function providerFileCheckerInvalidCodeParse()
2017-02-10 02:35:17 +01:00
{
return [
'invalidTemplatedType' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
namespace FooFoo;
/**
* @template T
* @param T $x
* @return T
*/
function foo($x) {
return $x;
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function bar(string $a): void { }
2017-06-29 16:22:49 +02:00
bar(foo(4));',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScalarArgument',
],
'invalidTemplatedStaticMethodType' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
namespace FooFoo;
class A {
/**
* @template T
* @param T $x
* @return T
*/
public static function foo($x) {
return $x;
}
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function bar(string $a): void { }
2017-06-29 16:22:49 +02:00
bar(A::foo(4));',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScalarArgument',
],
'invalidTemplatedInstanceMethodType' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
namespace FooFoo;
class A {
/**
* @template T
* @param T $x
* @return T
*/
public function foo($x) {
return $x;
}
}
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01:00
function bar(string $a): void { }
2017-06-29 16:22:49 +02:00
bar((new A())->foo(4));',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidScalarArgument',
],
'replaceChildTypeNoHint' => [
'<?php
/**
* @template TKey
* @template TValue
* @param Traversable<TKey, TValue> $t
* @return array<TKey, TValue>
*/
function f(Traversable $t): array {
$ret = [];
foreach ($t as $k => $v) $ret[$k] = $v;
return $ret;
}
function g():Generator { yield new stdClass; }
takesArrayOfStdClass(f(g()));
/** @param array<stdClass> $p */
function takesArrayOfStdClass(array $p): void {}',
'error_message' => 'MixedTypeCoercion',
],
'restrictTemplateInput' => [
'<?php
/** @template T */
class Foo
{
/**
* @psalm-var class-string
*/
private $type;
/** @var array<T> */
private $items;
/**
* @param class-string $type
* @template-typeof T $type
*/
public function __construct(string $type)
{
if (!in_array($type, [A::class, B::class], true)) {
throw new \InvalidArgumentException;
}
$this->type = $type;
$this->items = [];
}
/** @param T $item */
public function add($item): void
{
$this->items[] = $item;
}
}
class A {}
class B {}
$foo = new Foo(A::class);
$foo->add(new B);',
'error_message' => 'InvalidArgument',
],
];
}
2017-02-10 02:35:17 +01:00
}