2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
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;
|
2016-12-24 19:23:22 +01:00
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-02-15 17:07:08 +01:00
|
|
|
public function testPhpStormGenericsWithValidArrayIteratorArgument()
|
2018-02-01 07:10:27 +01:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:07:08 +01:00
|
|
|
/**
|
|
|
|
* @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());
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:13:33 +01:00
|
|
|
/**
|
|
|
|
* @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());
|
|
|
|
}
|
|
|
|
|
2018-03-01 17:19:23 +01:00
|
|
|
/**
|
|
|
|
* @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());
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPhpStormGenericsInvalidArgument()
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage PossiblyInvalidMethodCall
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPhpStormGenericsNoTypehint()
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2018-06-07 18:23:10 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidParamDefault
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidParamDefault()
|
|
|
|
{
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidParamDefault
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidTypehintParamDefaultButAllowedInConfig()
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2017-03-19 19:39:05 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
2018-01-21 16:22:04 +01:00
|
|
|
'nopType' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
|
|
|
|
/** @var int $a */',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'int',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'validDocblockReturn' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
return "boop";
|
|
|
|
}
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo2(): array {
|
2017-04-25 05:45:02 +02:00
|
|
|
return ["hello"];
|
|
|
|
}
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo3(): array {
|
2017-04-25 05:45:02 +02:00
|
|
|
return ["hello"];
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'reassertWithIs' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (is_array($a)) {
|
|
|
|
// do something
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-02-07 00:44:53 +01:00
|
|
|
'assertions' => [],
|
2018-02-07 21:20:47 +01:00
|
|
|
'error_level' => ['RedundantConditionGivenDocblockType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'checkArrayWithIs' => [
|
|
|
|
'<?php
|
|
|
|
/** @param mixed $b */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($b): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var array */
|
|
|
|
$a = (array)$b;
|
|
|
|
if (is_array($a)) {
|
|
|
|
// do something
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-02-07 00:44:53 +01:00
|
|
|
'assertions' => [],
|
2018-02-07 21:20:47 +01:00
|
|
|
'error_level' => ['RedundantConditionGivenDocblockType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'checkArrayWithIsInsideLoop' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array<mixed, array<mixed, mixed>> $data */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo($data): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
}',
|
2017-11-19 18:14:02 +01:00
|
|
|
'assertions' => [],
|
2018-02-07 21:20:47 +01:00
|
|
|
'error_level' => ['LoopInvalidation', 'MixedArrayOffset', 'RedundantConditionGivenDocblockType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'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-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'goodDocblockInNamespace' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
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-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-05-05 00:35:05 +02:00
|
|
|
],
|
|
|
|
|
2017-05-10 18:36:11 +02:00
|
|
|
'ignoreNullableReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $bar = 5;
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {}
|
2017-05-10 18:36:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ?A
|
|
|
|
* @psalm-ignore-nullable-return
|
|
|
|
*/
|
|
|
|
function makeA() {
|
2018-01-11 21:50:45 +01:00
|
|
|
return rand(0, 1) ? new A(): null;
|
2017-05-10 18:36:11 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takeA(A $a): void { }
|
2017-05-10 18:36:11 +02:00
|
|
|
|
|
|
|
$a = makeA();
|
|
|
|
$a->foo();
|
|
|
|
$a->bar = 7;
|
2017-05-27 02:05:57 +02:00
|
|
|
takeA($a);',
|
2017-05-10 18:36:11 +02:00
|
|
|
],
|
2017-06-13 20:00:41 +02:00
|
|
|
'invalidDocblockParamSuppress' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param int $bar
|
2018-01-05 18:11:12 +01:00
|
|
|
* @psalm-suppress MismatchingDocblockParamType
|
2017-06-13 20:00:41 +02:00
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(array $bar): void {
|
2017-06-13 20:00:41 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-07-25 22:11:02 +02:00
|
|
|
'differentDocblockParamClassSuppress' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
2018-05-21 06:46:56 +02:00
|
|
|
class B {}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param B $bar
|
2018-01-05 18:11:12 +01:00
|
|
|
* @psalm-suppress MismatchingDocblockParamType
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(A $bar): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'varDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<Exception> */
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a[0]->getMessage();',
|
|
|
|
],
|
2017-09-02 17:18:56 +02:00
|
|
|
'mixedDocblockParamTypeDefinedInParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @param mixed $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo($a): void {}
|
2017-09-02 17:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo($a): void {}
|
2017-09-02 17:18:56 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'intDocblockParamTypeDefinedInParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @param int $a */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo($a): void {}
|
2017-09-02 17:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo($a): void {}
|
2017-09-02 17:18:56 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-10-07 16:22:52 +02:00
|
|
|
'varSelf' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {}
|
2017-10-07 16:22:52 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function getMeAgain(): void {
|
2017-10-07 16:22:52 +02:00
|
|
|
/** @var self */
|
|
|
|
$me = $this;
|
|
|
|
$me->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-11-03 02:45:17 +01:00
|
|
|
'psalmVar' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
/** @psalm-var array<int, string> */
|
|
|
|
public $foo = [];
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function updateFoo(): void {
|
2017-11-03 02:45:17 +01:00
|
|
|
$this->foo[5] = "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'psalmParam' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesInt(int $a): void {}
|
2017-11-03 02:45:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-param array<int, string> $a
|
|
|
|
* @param string[] $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(array $a): void {
|
2017-11-03 02:45:17 +01:00
|
|
|
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;
|
|
|
|
}',
|
|
|
|
],
|
2018-02-08 05:33:31 +01:00
|
|
|
'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',
|
|
|
|
],
|
|
|
|
],
|
2018-03-21 15:46:21 +01:00
|
|
|
'allowOptionalParamsToBeEmptyArray' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array{b?: int, c?: string} $a */
|
|
|
|
function foo(array $a = []) : void {}',
|
|
|
|
],
|
2018-03-30 23:46:12 +02:00
|
|
|
'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 {}',
|
|
|
|
],
|
2018-04-20 21:22:48 +02:00
|
|
|
'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"]();',
|
2018-04-20 21:22:48 +02:00
|
|
|
],
|
|
|
|
'megaClosureAnnotationWithSpacing' => [
|
|
|
|
'<?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"]();',
|
2018-04-20 21:22:48 +02:00
|
|
|
],
|
2018-05-21 18:55:44 +02:00
|
|
|
'slashAfter?' => [
|
|
|
|
'<?php
|
|
|
|
namespace ns;
|
|
|
|
|
|
|
|
/** @param ?\stdClass $s */
|
|
|
|
function foo($s) : void {
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(null);
|
|
|
|
foo(new \stdClass);',
|
|
|
|
],
|
2018-05-28 23:26:43 +02:00
|
|
|
'generatorReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/** @return Generator<int, stdClass> */
|
|
|
|
function g():Generator { yield new stdClass; }
|
|
|
|
|
|
|
|
$g = g();',
|
|
|
|
'assertions' => [
|
2019-01-26 22:58:49 +01:00
|
|
|
'$g' => 'Generator<int, stdClass, mixed, mixed>',
|
2018-05-28 23:26:43 +02:00
|
|
|
],
|
|
|
|
],
|
2018-05-29 15:44:38 +02:00
|
|
|
'returnTypeShouldBeNullable' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
function foo() : ?stdClass {
|
|
|
|
return rand(0, 1) ? new stdClass : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$f = foo();
|
|
|
|
if ($f) {}'
|
|
|
|
],
|
2018-06-09 03:18:49 +02:00
|
|
|
'spreadOperatorArrayAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $s */
|
|
|
|
function foo(string ...$s) : void {}',
|
|
|
|
],
|
2018-07-05 23:25:26 +02:00
|
|
|
'valueReturnType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param "a"|"b" $_p
|
|
|
|
*/
|
|
|
|
function acceptsLiteral($_p): void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return "a"|"b"
|
|
|
|
*/
|
|
|
|
function returnsLiteral(): string {
|
|
|
|
return rand(0,1) ? "a" : "b";
|
|
|
|
}
|
|
|
|
|
|
|
|
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 { }
|
|
|
|
|
|
|
|
bar(foo());'
|
|
|
|
],
|
|
|
|
'typeAliasBeforeFunction' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
2018-09-05 04:27:55 +02:00
|
|
|
* @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();
|
2018-07-22 04:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
return new B();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/** @param CoolType $a **/
|
|
|
|
function bar ($a) : void { }
|
|
|
|
|
|
|
|
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 { }
|
|
|
|
|
|
|
|
bar(foo());'
|
|
|
|
],
|
|
|
|
'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 { }
|
|
|
|
|
|
|
|
bar(foo());'
|
|
|
|
],
|
2018-09-24 19:08:23 +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;
|
|
|
|
}',
|
|
|
|
],
|
2018-08-21 04:11:01 +02:00
|
|
|
'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();',
|
|
|
|
],
|
2018-08-23 04:53:44 +02:00
|
|
|
'spaceInType' => [
|
|
|
|
'<?php
|
|
|
|
/** @return string | null */
|
|
|
|
function foo(string $s = null) {
|
|
|
|
return $s;
|
|
|
|
}',
|
|
|
|
],
|
2018-11-01 22:03:08 +01:00
|
|
|
'missingReturnTypeWithBadDocblockIgnoreBoth' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return [bad]
|
|
|
|
*/
|
|
|
|
function fooBar() {
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'InvalidDocblock' => \Psalm\Config::REPORT_INFO,
|
|
|
|
'MissingReturnType' => \Psalm\Config::REPORT_INFO,
|
|
|
|
]
|
|
|
|
],
|
2019-01-18 06:56:24 +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);',
|
|
|
|
],
|
2019-01-19 17:31:51 +01:00
|
|
|
'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)];
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'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-01-19 19:32:43 +01:00
|
|
|
'paramOutChangeType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param-out string $s
|
|
|
|
*/
|
|
|
|
function addFoo(?string &$s) : void {
|
|
|
|
if ($s === null) {
|
|
|
|
$s = "hello";
|
|
|
|
}
|
|
|
|
$s .= "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
addFoo($a);
|
|
|
|
|
|
|
|
echo strlen($a);',
|
|
|
|
],
|
2019-01-20 17:49:13 +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)) {}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
'MixedAssignment'
|
|
|
|
]
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
2017-05-25 03:11:18 +02:00
|
|
|
'invalidReturn' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/**
|
|
|
|
* @return $thus
|
|
|
|
*/
|
|
|
|
public static function barBar();
|
|
|
|
}',
|
2017-11-15 03:43:31 +01:00
|
|
|
'error_message' => 'MissingDocblockType',
|
2017-05-25 03:11:18 +02:00
|
|
|
],
|
2017-07-10 02:32:35 +02:00
|
|
|
'invalidReturnClass' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/**
|
|
|
|
* @return 1
|
|
|
|
*/
|
|
|
|
public static function barBar();
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-03-25 03:02:44 +02:00
|
|
|
'invalidReturnBrackets' => [
|
|
|
|
'<?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',
|
|
|
|
],
|
2017-07-10 02:32:35 +02:00
|
|
|
'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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'missingParamType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param string $bar
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooBar(): void {
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fooBar("hello");',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'TooManyArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:8 - Too many arguments for method fooBar '
|
2018-02-02 17:26:55 +01:00
|
|
|
. '- expecting 0 but saw 1',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingParamVar' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooBar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5 - Badly-formatted @param',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-11-01 22:03:08 +01:00
|
|
|
'missingReturnTypeWithBadDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return [bad]
|
|
|
|
*/
|
|
|
|
function fooBar() {
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingReturnType',
|
|
|
|
[
|
|
|
|
'InvalidDocblock' => \Psalm\Config::REPORT_INFO,
|
|
|
|
]
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'invalidDocblockReturn' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(): int {
|
2017-07-25 22:11:02 +02:00
|
|
|
return 5;
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2018-01-05 18:11:12 +01:00
|
|
|
'error_message' => 'MismatchingDocblockReturnType',
|
2017-05-05 00:35:05 +02:00
|
|
|
],
|
2017-09-02 17:18:56 +02:00
|
|
|
'intParamTypeDefinedInParent' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(int $a): void {}
|
2017-09-02 17:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo($a): void {}
|
2017-09-02 17:18:56 +02:00
|
|
|
}',
|
2018-01-29 02:03:47 +01:00
|
|
|
'error_message' => 'MissingParamType',
|
2017-09-02 17:18:56 +02:00
|
|
|
'error_levels' => ['MethodSignatureMismatch'],
|
|
|
|
],
|
2017-11-03 02:45:17 +01:00
|
|
|
'psalmInvalidVar' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
/** @psalm-var array<int, string> */
|
|
|
|
public $foo = [];
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function updateFoo(): void {
|
2017-11-03 02:45:17 +01:00
|
|
|
$this->foo["boof"] = "hello";
|
|
|
|
}
|
|
|
|
}',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-11-03 02:45:17 +01:00
|
|
|
],
|
2017-11-15 03:43:31 +01:00
|
|
|
'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 {
|
2017-11-15 03:43:31 +01:00
|
|
|
/** @var $myVar MyNS\OtherClass */
|
|
|
|
$myVar = $x->conn()->method();
|
|
|
|
$myVar->otherMethod();
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingDocblockType',
|
|
|
|
],
|
2018-01-08 23:17:49 +01:00
|
|
|
'dontOverrideSameType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return ?int */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): ?int {
|
2018-01-08 23:17:49 +01:00
|
|
|
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',
|
|
|
|
],
|
2018-01-10 06:07:47 +01:00
|
|
|
'preventBadBoolean' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): boolean {
|
2018-01-10 06:07:47 +01:00
|
|
|
return true;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2018-01-19 22:06:30 +01:00
|
|
|
'preventBadObjectLikeFormat' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{} $arr
|
|
|
|
*/
|
|
|
|
function bar(array $arr): void {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
2018-01-20 17:48:16 +01:00
|
|
|
],
|
2018-02-01 07:10:27 +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',
|
|
|
|
],
|
2018-03-27 17:34:48 +02:00
|
|
|
'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',
|
|
|
|
],
|
2018-04-25 03:27:31 +02:00
|
|
|
'hyphenInType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return - Description
|
|
|
|
*/
|
|
|
|
function example() {
|
|
|
|
return "placeholder";
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-06-11 23:23:28 +02:00
|
|
|
'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',
|
|
|
|
],
|
2019-01-08 21:11:57 +01:00
|
|
|
'noCrashOnInvalidClassTemplateAsType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as ' . '
|
|
|
|
*/
|
|
|
|
class A {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
|
|
|
'noCrashOnInvalidFunctionTemplateAsType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template T as ' . '
|
|
|
|
*/
|
|
|
|
function foo() : void {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2019-01-09 17:51:29 +01:00
|
|
|
'returnTypeNewLineIsIgnored' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
* Some text
|
|
|
|
*/
|
|
|
|
function foo() {}',
|
|
|
|
'error_message' => 'MissingReturnType',
|
|
|
|
],
|
2019-01-18 06:56:24 +01:00
|
|
|
'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' => 'UndefinedClass',
|
|
|
|
],
|
2019-02-08 00:10:32 +01:00
|
|
|
'badPsalmType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-type Foo = array{a:}
|
|
|
|
*/',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-31 06:14:00 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|