1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/AnnotationTest.php

1599 lines
52 KiB
PHP
Raw Normal View History

2016-12-12 05:41:11 +01:00
<?php
namespace Psalm\Tests;
use Psalm\Config;
use Psalm\Context;
use const DIRECTORY_SEPARATOR;
class AnnotationTest extends TestCase
2016-12-12 05:41:11 +01:00
{
2018-11-06 03:57:36 +01:00
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return void
*/
public function testPhpStormGenericsWithValidArrayIteratorArgument()
{
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
function takesString(string $s): void {}
/** @param ArrayIterator|string[] $i */
function takesArrayIteratorOfString(ArrayIterator $i): void {
$s = $i->offsetGet("a");
takesString($s);
foreach ($i as $s2) {
takesString($s2);
}
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return void
*/
public function testPhpStormGenericsWithValidTraversableArgument()
{
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
function takesString(string $s): void {}
/** @param Traversable|string[] $i */
function takesTraversableOfString(Traversable $i): void {
foreach ($i as $s2) {
takesString($s2);
}
}'
);
$this->analyzeFile('somefile.php', new Context());
}
2019-03-23 19:27:54 +01:00
/**
2018-03-17 20:40:57 +01:00
* @return void
*/
public function testPhpStormGenericsWithClassProperty()
{
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
/** @psalm-suppress MissingConstructor */
class Foo {
/** @var \stdClass[]|\ArrayObject */
public $bar;
/**
* @return \stdClass[]|\ArrayObject
*/
public function getBar(): \ArrayObject
{
return $this->bar;
}
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return void
*/
public function testPhpStormGenericsWithGeneratorArray()
{
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
class A {
/**
* @return stdClass[]|Generator
*/
function getCollection(): Generator
{
yield new stdClass;
}
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return void
*/
public function testPhpStormGenericsWithValidIterableArgument()
{
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
function takesString(string $s): void {}
/** @param iterable|string[] $i */
function takesArrayIteratorOfString(iterable $i): void {
foreach ($i as $s2) {
takesString($s2);
}
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
2019-05-17 00:36:36 +02:00
* @return void
*/
public function testPhpStormGenericsInvalidArgument()
{
2019-05-17 00:36:36 +02:00
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('InvalidScalarArgument');
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
function takesInt(int $s): void {}
/** @param ArrayIterator|string[] $i */
function takesArrayIteratorOfString(ArrayIterator $i): void {
$s = $i->offsetGet("a");
takesInt($s);
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
2019-05-17 00:36:36 +02:00
* @return void
*/
public function testPhpStormGenericsNoTypehint()
{
2019-05-17 00:36:36 +02:00
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('PossiblyInvalidMethodCall');
Config::getInstance()->allow_phpstorm_generics = true;
$this->addFile(
'somefile.php',
'<?php
/** @param ArrayIterator|string[] $i */
function takesArrayIteratorOfString($i): void {
$s = $i->offsetGet("a");
}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
2019-05-17 00:36:36 +02:00
* @return void
*/
public function testInvalidParamDefault()
{
2019-05-17 00:36:36 +02:00
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('InvalidParamDefault');
$this->addFile(
'somefile.php',
'<?php
/**
* @param array $arr
* @return void
*/
function foo($arr = false) {}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
* @return void
*/
public function testInvalidParamDefaultButAllowedInConfig()
{
Config::getInstance()->add_param_default_to_docblock_type = true;
$this->addFile(
'somefile.php',
'<?php
/**
* @param array $arr
* @return void
*/
function foo($arr = false) {}
foo(false);
foo(["hello"]);'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
2019-05-17 00:36:36 +02:00
* @return void
*/
public function testInvalidTypehintParamDefaultButAllowedInConfig()
{
2019-05-17 00:36:36 +02:00
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessage('InvalidParamDefault');
Config::getInstance()->add_param_default_to_docblock_type = true;
$this->addFile(
'somefile.php',
'<?php
function foo(array $arr = false) : void {}'
);
$this->analyzeFile('somefile.php', new Context());
}
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
2018-11-06 03:57:36 +01:00
public function providerValidCodeParse()
{
return [
'nopType' => [
'<?php
$a = "hello";
/** @var int $a */',
'assertions' => [
'$a' => 'int',
],
],
'validDocblockReturn' => [
'<?php
/**
* @return string
*/
2018-01-11 21:50:45 +01:00
function fooFoo(): string {
return "boop";
}
/**
* @return array<int, string>
*/
2018-01-11 21:50:45 +01:00
function foo2(): array {
return ["hello"];
}
/**
* @return array<int, string>
*/
2018-01-11 21:50:45 +01:00
function foo3(): array {
return ["hello"];
2017-05-27 02:05:57 +02:00
}',
],
'reassertWithIs' => [
'<?php
/** @param array $a */
2018-01-11 21:50:45 +01:00
function foo($a): void {
if (is_array($a)) {
// do something
}
2017-05-27 02:05:57 +02:00
}',
'assertions' => [],
'error_level' => ['RedundantConditionGivenDocblockType'],
],
'checkArrayWithIs' => [
'<?php
/** @param mixed $b */
2018-01-11 21:50:45 +01:00
function foo($b): void {
/** @var array */
$a = (array)$b;
if (is_array($a)) {
// do something
}
2017-05-27 02:05:57 +02:00
}',
'assertions' => [],
'error_level' => ['RedundantConditionGivenDocblockType'],
],
'checkArrayWithIsInsideLoop' => [
'<?php
/** @param array<mixed, array<mixed, mixed>> $data */
2018-01-11 21:50:45 +01:00
function foo($data): void {
foreach ($data as $key => $val) {
if (!\is_array($data)) {
$data = [$key => null];
} else {
$data[$key] = !empty($val);
}
}
2017-05-27 02:05:57 +02:00
}',
'assertions' => [],
'error_level' => ['LoopInvalidation', 'MixedArrayOffset', 'DocblockTypeContradiction'],
],
'goodDocblock' => [
'<?php
class A {
/**
* @param A $a
* @param bool $b
*/
2018-01-11 21:50:45 +01:00
public function g(A $a, $b): void {
}
2017-05-27 02:05:57 +02:00
}',
],
'goodDocblockInNamespace' => [
'<?php
namespace Foo;
class A {
/**
* @param \Foo\A $a
* @param bool $b
*/
2018-01-11 21:50:45 +01:00
public function g(A $a, $b): void {
}
2017-05-27 02:05:57 +02:00
}',
],
'ignoreNullableReturn' => [
'<?php
class A {
/** @var int */
public $bar = 5;
2018-01-11 21:50:45 +01:00
public function foo(): void {}
}
/**
* @return ?A
* @psalm-ignore-nullable-return
*/
function makeA() {
2018-01-11 21:50:45 +01:00
return rand(0, 1) ? new A(): null;
}
2018-01-11 21:50:45 +01:00
function takeA(A $a): void { }
$a = makeA();
$a->foo();
$a->bar = 7;
2017-05-27 02:05:57 +02:00
takeA($a);',
],
'invalidDocblockParamSuppress' => [
'<?php
/**
* @param int $bar
* @psalm-suppress MismatchingDocblockParamType
*/
2018-01-11 21:50:45 +01:00
function fooFoo(array $bar): void {
}',
],
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
'differentDocblockParamClassSuppress' => [
'<?php
class A {}
class B {}
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
/**
* @param B $bar
* @psalm-suppress MismatchingDocblockParamType
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
*/
2018-01-11 21:50:45 +01:00
function fooFoo(A $bar): void {
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
}',
],
'varDocblock' => [
'<?php
/** @var array<Exception> */
$a = [];
$a[0]->getMessage();',
],
'mixedDocblockParamTypeDefinedInParent' => [
'<?php
class A {
/** @param mixed $a */
2018-01-11 21:50:45 +01:00
public function foo($a): void {}
}
class B extends A {
2018-01-11 21:50:45 +01:00
public function foo($a): void {}
}',
],
'intDocblockParamTypeDefinedInParent' => [
'<?php
class A {
/** @param int $a */
2018-01-11 21:50:45 +01:00
public function foo($a): void {}
}
class B extends A {
2018-01-11 21:50:45 +01:00
public function foo($a): void {}
}',
],
'varSelf' => [
'<?php
class A
{
2018-01-11 21:50:45 +01:00
public function foo(): void {}
2018-01-11 21:50:45 +01:00
public function getMeAgain(): void {
/** @var self */
$me = $this;
$me->foo();
}
}',
],
'psalmVar' => [
'<?php
class A
{
/** @psalm-var array<int, string> */
public $foo = [];
2018-01-11 21:50:45 +01:00
public function updateFoo(): void {
$this->foo[5] = "hello";
}
}',
],
'psalmParam' => [
'<?php
2018-01-11 21:50:45 +01:00
function takesInt(int $a): void {}
/**
* @psalm-param array<int, string> $a
* @param string[] $a
*/
2018-01-11 21:50:45 +01:00
function foo(array $a): void {
foreach ($a as $key => $value) {
takesInt($key);
}
}',
],
2017-12-30 16:54:01 +01:00
'returnDocblock' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(int $i): int {
2017-12-30 16:54:01 +01:00
/** @var int */
return $i;
}',
],
'doubleVar' => [
'<?php
function foo() : array {
return ["hello" => new stdClass, "goodbye" => new stdClass];
}
$a = null;
$b = null;
/**
* @var string $key
* @var stdClass $value
*/
foreach (foo() as $key => $value) {
$a = $key;
$b = $value;
}',
'assertions' => [
'$a' => 'null|string',
'$b' => 'null|stdClass',
],
],
'allowOptionalParamsToBeEmptyArray' => [
'<?php
/** @param array{b?: int, c?: string} $a */
function foo(array $a = []) : void {}',
],
'allowEmptyVarAnnotation' => [
'<?php
/**
* @param $x
*/
function example(array $x) : void {}',
],
2018-04-04 18:39:05 +02:00
'allowCapitalisedNamespacedString' => [
'<?php
namespace Foo;
/**
* @param String $x
*/
function example(string $x) : void {}',
],
'megaClosureAnnotationWithoutSpacing' => [
'<?php
/** @var array{a:Closure():(array<mixed, mixed>|null), b?:Closure():array<mixed, mixed>, c?:Closure():array<mixed, mixed>, d?:Closure():array<mixed, mixed>, e?:Closure():(array{f:null|string, g:null|string, h:null|string, i:string, j:mixed, k:mixed, l:mixed, m:mixed, n:bool, o?:array{0:string}}|null), p?:Closure():(array{f:null|string, g:null|string, h:null|string, q:string, i:string, j:mixed, k:mixed, l:mixed, m:mixed, n:bool, o?:array{0:string}}|null), r?:Closure():(array<mixed, mixed>|null), s:array<mixed, mixed>} */
$arr = [];
2018-09-17 18:15:45 +02:00
$arr["a"]();',
],
'megaClosureAnnotationWithSpacing' => [
'<?php
2019-06-16 18:39:07 +02:00
/**
* @var array{
* a: Closure() : (array<mixed, mixed>|null),
* b?: Closure() : array<mixed, mixed>,
* c?: Closure() : array<mixed, mixed>,
* d?: Closure() : array<mixed, mixed>,
* e?: Closure() : (array{
* f: null|string,
* g: null|string,
* h: null|string,
* i: string,
* j: mixed,
* k: mixed,
* l: mixed,
* m: mixed,
* n: bool,
* o?: array{0:string}
* }|null),
* p?: Closure() : (array{
* f: null|string,
* g: null|string,
* h: null|string,
* q: string,
* i: string,
* j: mixed,
* k: mixed,
* l: mixed,
* m: mixed,
* n: bool,
* o?: array{0:string}
* }|null),
* r?: Closure() : (array<mixed, mixed>|null),
* s: array<mixed, mixed>
* }
*
* Some text
*/
$arr = [];
2018-09-17 18:15:45 +02:00
$arr["a"]();',
],
2019-06-16 18:39:07 +02:00
'multipeLineGenericArray' => [
'<?php
/**
* @psalm-type MiddlewareArray = array<
* class-string<\Exception>,
* array<int, string>
* >
*
* @psalm-type RuleArray = array{
* rule: string,
* controller?: class-string<\Exception>,
* redirect?: string,
* code?: int,
* type?: string,
* middleware?: MiddlewareArray
* }
*
* Foo Bar
*/
class A {}',
],
2018-05-21 18:55:44 +02:00
'slashAfter?' => [
'<?php
namespace ns;
/** @param ?\stdClass $s */
function foo($s) : void {
}
foo(null);
foo(new \stdClass);',
],
'generatorReturnType' => [
'<?php
/** @return Generator<int, stdClass> */
function g():Generator { yield new stdClass; }
$g = g();',
'assertions' => [
'$g' => 'Generator<int, stdClass, mixed, mixed>',
],
],
'returnTypeShouldBeNullable' => [
'<?php
/**
* @return stdClass
*/
function foo() : ?stdClass {
return rand(0, 1) ? new stdClass : null;
}
$f = foo();
2019-03-23 19:27:54 +01:00
if ($f) {}',
],
'spreadOperatorAnnotation' => [
'<?php
/** @param string[] $s */
function foo(string ...$s) : void {}
/** @param string ...$s */
function bar(string ...$s) : void {}
foo("hello", "goodbye");
bar("hello", "goodbye");
foo(...["hello", "goodbye"]);
bar(...["hello", "goodbye"]);',
],
'spreadOperatorByRefAnnotation' => [
'<?php
/** @param string &...$s */
function foo(&...$s) : void {}
/** @param string ...&$s */
function bar(&...$s) : void {}
/** @param string[] &$s */
function bat(&...$s) : void {}
$a = "hello";
$b = "goodbye";
$c = "hello again";
foo($a);
2019-04-09 20:29:09 +02:00
bar($b);
bat($c);',
'assertions' => [
'$a' => 'string',
'$b' => 'string',
'$c' => 'string',
],
],
'valueReturnType' => [
'<?php
/**
* @param "a"|"b" $_p
*/
function acceptsLiteral($_p): void {}
/**
* @return "a"|"b"
*/
function returnsLiteral(): string {
return rand(0,1) ? "a" : "b";
}
2019-03-23 19:27:54 +01:00
acceptsLiteral(returnsLiteral());',
],
2018-07-15 23:23:17 +02:00
'typeAliasBeforeClass' => [
'<?php
/**
* @psalm-type CoolType = A|B|null
*/
class A {}
class B {}
/** @return CoolType */
function foo() {
if (rand(0, 1)) {
return new A();
}
if (rand(0, 1)) {
return new B();
}
return null;
}
/** @param CoolType $a **/
function bar ($a) : void { }
2019-03-23 19:27:54 +01:00
bar(foo());',
2018-07-15 23:23:17 +02:00
],
'typeAliasBeforeFunction' => [
'<?php
/**
* @psalm-type A_OR_B = A|B
* @psalm-type CoolType = A_OR_B|null
2018-07-15 23:23:17 +02:00
* @return CoolType
*/
function foo() {
if (rand(0, 1)) {
return new A();
}
if (rand(0, 1)) {
return new B();
}
return null;
}
class A {}
class B {}
/** @param CoolType $a **/
function bar ($a) : void { }
2019-03-23 19:27:54 +01:00
bar(foo());',
],
'typeAliasInSeparateBlockBeforeFunction' => [
'<?php
/**
* @psalm-type CoolType = A|B|null
*/
/**
* @return CoolType
*/
function foo() {
if (rand(0, 1)) {
return new A();
2018-07-15 23:23:17 +02:00
}
if (rand(0, 1)) {
return new B();
}
return null;
}
class A {}
class B {}
/** @param CoolType $a **/
function bar ($a) : void { }
2019-03-23 19:27:54 +01:00
bar(foo());',
2018-07-15 23:23:17 +02:00
],
'almostFreeStandingTypeAlias' => [
'<?php
/**
* @psalm-type CoolType = A|B|null
*/
// this breaks up the line
class A {}
class B {}
/** @return CoolType */
function foo() {
if (rand(0, 1)) {
return new A();
}
if (rand(0, 1)) {
return new B();
}
return null;
}
/** @param CoolType $a **/
function bar ($a) : void { }
2019-03-23 19:27:54 +01:00
bar(foo());',
2018-07-15 23:23:17 +02:00
],
'typeAliasUsedTwice' => [
'<?php
/** @psalm-type TA = array<int, string> */
class Bar {
public function foo() : void {
$bar =
/** @return TA */
function() {
return ["hello"];
};
/** @var array<int, TA> */
$bat = [$bar(), $bar()];
foreach ($bat as $b) {
echo $b[0];
}
}
}
/**
* @psalm-type _A=array{elt:int}
* @param _A $p
* @return _A
*/
function f($p) {
/** @var _A */
$r = $p;
return $r;
}',
],
'listUnpackWithDocblock' => [
'<?php
interface I {}
class A implements I {
public function bar() : void {}
}
/** @return I[] */
function foo() : array {
return [new A()];
}
/** @var A $a1 */
[$a1, $a2] = foo();
$a1->bar();',
],
'spaceInType' => [
'<?php
/** @return string | null */
function foo(string $s = null) {
return $s;
}',
],
'missingReturnTypeWithBadDocblockIgnoreBoth' => [
'<?php
/**
* @return [bad]
*/
function fooBar() {
}',
[],
[
'InvalidDocblock' => \Psalm\Config::REPORT_INFO,
'MissingReturnType' => \Psalm\Config::REPORT_INFO,
2019-03-23 19:27:54 +01:00
],
],
'objectWithPropertiesAnnotation' => [
'<?php
/** @param object{foo:string} $o */
function foo(object $o) : string {
return $o->foo;
}
$s = new \stdClass();
$s->foo = "hello";
foo($s);
class A {
/** @var string */
public $foo = "hello";
}
foo(new A);',
],
'refineTypeInNestedCall' => [
'<?php
function foo(array $arr): \Generator {
/** @var array<string, mixed> $arr */
foreach (array_filter(array_keys($arr), function (string $key) : bool {
return strpos($key, "BAR") === 0;
}) as $envVar) {
yield $envVar => [getenv($envVar)];
}
2019-03-23 19:27:54 +01:00
}',
],
'allowAnnotationOnServer' => [
'<?php
function foo(): \Generator {
/** @var array<string, mixed> $_SERVER */
foreach (array_filter(array_keys($_SERVER), function (string $key) : bool {
return strpos($key, "BAR") === 0;
}) as $envVar) {
yield $envVar => [getenv($envVar)];
}
2019-03-23 19:27:54 +01:00
}',
],
'annotationOnForeachItems' => [
'<?php
function foo(array $arr) : void {
$item = null;
/** @var string $item */
foreach ($arr as $item) {}
if (is_null($item)) {}
}
function bar(array $arr) : void {
$item = null;
/** @var string $item */
foreach ($arr as $item => $_) {}
if (is_null($item)) {}
}
function bat(array $arr) : void {
$item = null;
/** @var string $item */
foreach ($arr as list($item)) {}
if (is_null($item)) {}
}
function baz(array $arr) : void {
$item = null;
/** @var string $item */
foreach ($arr as list($item => $_)) {}
if (is_null($item)) {}
}',
[],
[
2019-03-23 19:27:54 +01:00
'MixedAssignment',
],
],
'extraneousDocblockParamName' => [
'<?php
/**
* @param string $foo
* @param string[] $bar
* @param string[] $barb
*/
function f(string $foo, array $barb): void {}',
],
'nonEmptyArray' => [
'<?php
/** @param non-empty-array<string> $arr */
function foo(array $arr) : void {
foreach ($arr as $a) {}
echo $a;
}
foo(["a", "b", "c"]);
/** @param array<string> $arr */
function bar(array $arr) : void {
if (!$arr) {
return;
}
foo($arr);
2019-03-23 19:27:54 +01:00
}',
],
'nonEmptyArrayInNamespace' => [
'<?php
namespace ns;
/** @param non-empty-array<string> $arr */
function foo(array $arr) : void {
foreach ($arr as $a) {}
echo $a;
}
foo(["a", "b", "c"]);
/** @param array<string> $arr */
function bar(array $arr) : void {
if (!$arr) {
return;
}
foo($arr);
2019-03-23 19:27:54 +01:00
}',
],
'inheritParentReturnDocbblock' => [
'<?php
class Foo {
/**
* @return int[]
*/
public function doFoo() {
return [1, 2, 3];
}
}
class Bar extends Foo {
public function doFoo(): array {
return [4, 5, 6];
}
}
$b = (new Bar)->doFoo();',
[
'$b' => 'array<array-key, int>',
]
],
2019-05-16 04:30:35 +02:00
'noExceptionOnIntersection' => [
'<?php
class Foo {
/** @var null|\DateTime&\DateTimeImmutable */
private $s = null;
}',
],
'intersectionWithSpace' => [
'<?php
interface A {
public function foo() : void;
}
interface B {
public function bar() : void;
}
/** @param A & B $a */
function f(A $a) : void {
$a->foo();
$a->bar();
}'
],
'allowClosingComma' => [
'<?php
/**
* @param array{
* foo: string,
* bar: string,
* baz: array{
* a: int,
* },
* } $foo
*/
function foo(array $foo) : int {
return count($foo);
}
/**
* @var array{
* foo:string,
* bar:string,
* baz:string,
* } $foo
*/
$foo = ["foo" => "", "bar" => "", "baz" => ""];'
],
'inheritedSelfAnnotation' => [
'<?php
interface I {
/**
* @param self $i
* @return self
*/
function foo(self $i) : self;
}
class C implements I {
public function foo(I $i) : I {
return $i;
}
}
function takeI(I $i) : I {
return (new C)->foo($i);
}',
],
];
}
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
{
return [
2019-05-17 00:36:36 +02:00
'invalidClassMethodReturn' => [
'<?php
class C {
/**
* @return $thus
*/
public function barBar() {
return $this;
}
}',
'error_message' => 'MissingDocblockType',
],
'invalidClassMethodReturnClass' => [
'<?php
class C {
/**
* @return 1
*/
public static function barBar() {
return 1;
}
}',
'error_message' => 'InvalidDocblock',
],
'invalidClassMethodReturnBrackets' => [
'<?php
class C {
/**
* @return []
*/
public static function barBar() {
return [];
}
}',
'error_message' => 'InvalidDocblock',
],
'invalidInterfaceMethodReturn' => [
'<?php
interface I {
/**
* @return $thus
*/
public static function barBar();
}',
'error_message' => 'MissingDocblockType',
],
2019-05-17 00:36:36 +02:00
'invalidInterfaceMethodReturnClass' => [
'<?php
interface I {
/**
* @return 1
*/
public static function barBar();
}',
'error_message' => 'InvalidDocblock',
],
2019-05-17 00:36:36 +02:00
'invalidInterfaceMethodReturnBrackets' => [
'<?php
interface I {
/**
* @return []
*/
public static function barBar();
}',
'error_message' => 'InvalidDocblock',
],
'invalidPropertyClass' => [
'<?php
class A {
/**
* @var 1
*/
public $bar;
}',
'error_message' => 'InvalidDocblock',
],
'invalidPropertyBrackets' => [
'<?php
class A {
/**
* @var []
*/
public $bar;
}',
'error_message' => 'InvalidDocblock',
],
'invalidReturnClassWithComma' => [
'<?php
interface I {
/**
* @return 1,
*/
public static function barBar();
}',
'error_message' => 'InvalidDocblock',
],
'returnClassWithComma' => [
'<?php
interface I {
/**
* @return a,
*/
public static function barBar();
}',
'error_message' => 'InvalidDocblock',
],
'missingParamType' => [
'<?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
* @param string $bar
*/
2018-01-11 21:50:45 +01:00
function fooBar(): void {
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
}
fooBar("hello");',
2019-02-27 22:00:44 +01:00
'error_message' => 'TooManyArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:21 - Too many arguments for method fooBar '
. '- expecting 0 but saw 1',
],
'missingParamVar' => [
'<?php
/**
* @param string
*/
2018-01-11 21:50:45 +01:00
function fooBar(): void {
}',
2019-02-27 22:00:44 +01:00
'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:21 - Badly-formatted @param',
],
'missingReturnTypeWithBadDocblock' => [
'<?php
/**
* @return [bad]
*/
function fooBar() {
}',
'error_message' => 'MissingReturnType',
[
'InvalidDocblock' => \Psalm\Config::REPORT_INFO,
2019-03-23 19:27:54 +01:00
],
],
'invalidDocblockReturn' => [
'<?php
/**
* @return string
*/
2018-01-11 21:50:45 +01:00
function fooFoo(): int {
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
return 5;
}',
'error_message' => 'MismatchingDocblockReturnType',
],
'intParamTypeDefinedInParent' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function foo(int $a): void {}
}
class B extends A {
2018-01-11 21:50:45 +01:00
public function foo($a): void {}
}',
'error_message' => 'MissingParamType',
'error_levels' => ['MethodSignatureMismatch'],
],
'psalmInvalidVar' => [
'<?php
class A
{
/** @psalm-var array<int, string> */
public $foo = [];
2018-01-11 21:50:45 +01:00
public function updateFoo(): void {
$this->foo["boof"] = "hello";
}
}',
'error_message' => 'InvalidPropertyAssignmentValue',
],
'incorrectDocblockOrder' => [
'<?php
class MyClass {
/**
* Comment
* @var $fooPropTypo string
*/
public $fooProp = "/tmp/file.txt";
}',
'error_message' => 'MissingDocblockType',
],
'badlyFormattedVar' => [
'<?php
/**
* @return string[]
*/
function returns_strings() {
/** @var array(string) $result */
$result = ["example"];
return $result;
}',
'error_message' => 'InvalidDocblock',
],
'badlyWrittenVar' => [
'<?php
/** @param mixed $x */
2018-01-11 21:50:45 +01:00
function myvalue($x): void {
/** @var $myVar MyNS\OtherClass */
$myVar = $x->conn()->method();
$myVar->otherMethod();
}',
'error_message' => 'MissingDocblockType',
],
'dontOverrideSameType' => [
'<?php
class A {
/** @return ?int */
2018-01-11 21:50:45 +01:00
public function foo(): ?int {
if (rand(0, 1)) return 5;
}
}',
'error_message' => 'InvalidReturnType',
],
2018-01-10 04:46:55 +01:00
'alwaysCheckReturnType' => [
'<?php
class A {}
/**
* @return A
* @psalm-suppress MismatchingDocblockReturnType
*/
2018-01-11 21:50:45 +01:00
function foo(): B {
2018-01-10 04:46:55 +01:00
return new A;
}',
'error_message' => 'UndefinedClass',
],
'preventBadBoolean' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(): boolean {
return true;
}',
'error_message' => 'UndefinedClass',
],
'undefinedDocblockClassCall' => [
'<?php
class B {
/**
* @return A
* @psalm-suppress UndefinedDocblockClass
* @psalm-suppress InvalidReturnStatement
* @psalm-suppress InvalidReturnType
*/
public function foo() {
return new stdClass();
}
public function bar() {
$this->foo()->bar();
}
}
',
'error_message' => 'UndefinedDocblockClass',
],
'preventBadObjectLikeFormat' => [
'<?php
/**
* @param array{} $arr
*/
function bar(array $arr): void {}',
'error_message' => 'InvalidDocblock',
2018-01-20 17:48:16 +01:00
],
'noPhpStormAnnotationsThankYou' => [
'<?php
/** @param ArrayIterator|string[] $i */
function takesArrayIteratorOfString(ArrayIterator $i): void {}',
'error_message' => 'MismatchingDocblockParamType',
],
'noPhpStormAnnotationsPossiblyInvalid' => [
'<?php
/** @param ArrayIterator|string[] $i */
function takesArrayIteratorOfString($i): void {
$s = $i->offsetGet("a");
}',
'error_message' => 'PossiblyInvalidMethodCall',
],
'badStaticVar' => [
'<?php
/** @var static */
$a = new stdClass();',
'error_message' => 'InvalidDocblock',
],
2018-03-29 08:20:19 +02:00
'doubleBar' => [
'<?php
/** @param PDO||Closure|numeric $a */
function foo($a) : void {}',
'error_message' => 'InvalidDocblock',
],
2018-04-05 20:11:57 +02:00
'badStringVar' => [
'<?php
/** @var string; */
$a = "hello";',
'error_message' => 'InvalidDocblock',
],
2018-04-16 00:16:31 +02:00
'badCallableVar' => [
'<?php
/** @return Closure(int): */
function foo() : callable {
2018-09-17 18:15:45 +02:00
return function () : void {};
2018-04-16 00:16:31 +02:00
}',
'error_message' => 'InvalidDocblock',
],
'hyphenInType' => [
'<?php
/**
* @return - Description
*/
function example() {
return "placeholder";
}',
'error_message' => 'InvalidDocblock',
],
'badAmpersand' => [
'<?php
/** @return &array */
function foo() : array {
return [];
}',
'error_message' => 'InvalidDocblock',
],
2018-07-15 23:23:17 +02:00
'invalidTypeAlias' => [
'<?php
/**
* @psalm-type CoolType = A|B>
*/
class A {}',
'error_message' => 'InvalidDocblock',
],
2018-08-09 04:44:02 +02:00
'typeAliasInObjectLike' => [
'<?php
/**
* @psalm-type aType null|"a"|"b"|"c"|"d"
*/
/** @psalm-return array{0:bool,1:aType} */
function f(): array {
return [(bool)rand(0,1), rand(0,1) ? "z" : null];
}',
'error_message' => 'InvalidReturnStatement',
],
2019-01-08 20:50:45 +01:00
'noCrashOnHalfDoneArrayPropertyType' => [
'<?php
class A {
/** @var array< */
private $foo = [];
}',
'error_message' => 'InvalidDocblock',
],
'noCrashOnHalfDoneObjectLikeArrayPropertyType' => [
'<?php
class A {
/** @var array{ */
private $foo = [];
}',
'error_message' => 'InvalidDocblock',
],
'noCrashOnInvalidClassTemplateAsType' => [
'<?php
/**
* @template T as ' . '
*/
class A {}',
'error_message' => 'InvalidDocblock',
],
'noCrashOnInvalidFunctionTemplateAsType' => [
'<?php
/**
* @template T as ' . '
*/
function foo() : void {}',
'error_message' => 'InvalidDocblock',
],
'returnTypeNewLineIsIgnored' => [
'<?php
/**
* @return
* Some text
*/
function foo() {}',
'error_message' => 'MissingReturnType',
],
'objectWithPropertiesAnnotationNoMatchingProperty' => [
'<?php
/** @param object{foo:string} $o */
function foo(object $o) : string {
return $o->foo;
}
class A {}
foo(new A);',
'error_message' => 'InvalidArgument',
],
2019-01-26 23:30:44 +01:00
'badVar' => [
'<?php
/** @var Foo */
$a = $_GET["foo"];',
'error_message' => 'UndefinedDocblockClass',
2019-01-26 23:30:44 +01:00
],
'badPsalmType' => [
'<?php
/**
* @psalm-type Foo = array{a:}
*/',
'error_message' => 'InvalidDocblock',
],
2019-02-23 17:02:04 +01:00
'mismatchingDocblockParamName' => [
'<?php
2019-02-27 22:00:44 +01:00
/** @param string[] $bar */
function f(array $barb): void {}',
'error_message' => 'InvalidDocblockParamName - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:41',
2019-02-23 17:02:04 +01:00
],
'nonEmptyArrayCalledWithEmpty' => [
'<?php
/** @param non-empty-array<string> $arr */
function foo(array $arr) : void {
foreach ($arr as $a) {}
echo $a;
}
foo([]);',
'error_message' => 'InvalidArgument',
],
'nonEmptyArrayCalledWithEmptyInNamespace' => [
'<?php
namespace ns;
/** @param non-empty-array<string> $arr */
function foo(array $arr) : void {
foreach ($arr as $a) {}
echo $a;
}
foo([]);',
'error_message' => 'InvalidArgument',
],
'nonEmptyArrayCalledWithArray' => [
'<?php
/** @param non-empty-array<string> $arr */
function foo(array $arr) : void {
foreach ($arr as $a) {}
echo $a;
}
/** @param array<string> $arr */
function bar(array $arr) {
foo($arr);
}',
'error_message' => 'ArgumentTypeCoercion',
],
'automaticInheritDoc' => [
'<?php
class Y {
/**
* @param string[] $arr
*/
public function boo(array $arr) : void {}
}
class X extends Y {
public function boo(array $arr) : void {}
}
(new X())->boo([1, 2]);',
2019-03-23 19:27:54 +01:00
'error_message' => 'InvalidScalarArgument',
],
'spreadOperatorArrayAnnotationBadArg' => [
'<?php
/** @param string[] $s */
function foo(string ...$s) : void {}
foo(5);',
'error_message' => 'InvalidScalarArgument',
],
'spreadOperatorArrayAnnotationBadSpreadArg' => [
'<?php
/** @param string[] $s */
function foo(string ...$s) : void {}
foo(...[5]);',
'error_message' => 'InvalidScalarArgument',
],
2019-04-09 20:29:09 +02:00
'spreadOperatorByRefAnnotationBadCall1' => [
'<?php
/** @param string &...$s */
function foo(&...$s) : void {}
$a = 1;
foo($a);',
'error_message' => 'InvalidScalarArgument',
],
'spreadOperatorByRefAnnotationBadCall2' => [
'<?php
/** @param string ...&$s */
function foo(&...$s) : void {}
$b = 2;
foo($b);',
'error_message' => 'InvalidScalarArgument',
],
'spreadOperatorByRefAnnotationBadCall3' => [
'<?php
/** @param string[] &$s */
function foo(&...$s) : void {}
$c = 3;
foo($c);',
'error_message' => 'InvalidScalarArgument',
],
2019-06-01 22:57:33 +02:00
'identifyReturnType' => [
'<?php
/** @return array{hello: string} */
function foo() {}',
'error_message' => 'InvalidReturnType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:33',
],
'invalidParamDocblockAsterisk' => [
'<?php
/**
* @param * $reference
*/
function f($reference) {}',
'error_message' => 'MissingDocblockType'
],
];
2016-12-31 06:14:00 +01:00
}
2016-12-12 05:41:11 +01:00
}