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
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2016-12-24 19:23:22 +01:00
|
|
|
|
2018-02-01 07:10:27 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPhpStormGenericsWithValidArgument()
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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-03-06 17:20:54 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidArgument
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDontAllowStringConstCoercion()
|
|
|
|
{
|
|
|
|
Config::getInstance()->allow_coercion_from_string_to_class_const = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param class-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
takesClassConstants("A");'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2018-03-06 18:19:50 +01:00
|
|
|
* @expectedExceptionMessage InvalidStringClass
|
2018-03-06 17:20:54 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDontAllowStringStandInForNewClass()
|
|
|
|
{
|
|
|
|
Config::getInstance()->allow_string_standin_for_class = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
$a = "A";
|
|
|
|
|
|
|
|
new $a();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2018-03-06 18:19:50 +01:00
|
|
|
* @expectedExceptionMessage InvalidStringClass
|
2018-03-06 17:20:54 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDontAllowStringStandInForStaticMethodCall()
|
|
|
|
{
|
|
|
|
Config::getInstance()->allow_string_standin_for_class = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = "A";
|
|
|
|
|
|
|
|
$a::foo();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2018-04-22 04:44:54 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPhpDocMethodWhenUndefined()
|
|
|
|
{
|
2018-04-22 05:08:08 +02:00
|
|
|
Config::getInstance()->use_phpdoc_methods_without_call = true;
|
2018-04-22 04:44:54 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
* @method void setInteger(int $integer)
|
|
|
|
* @method setString(int $integer)
|
|
|
|
* @method getBool(string $foo) : bool
|
|
|
|
* @method (string|int)[] getArray() : array
|
|
|
|
* @method (callable() : string) getCallable() : callable
|
|
|
|
*/
|
|
|
|
class Child {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$a = $child->getString();
|
|
|
|
$child->setInteger(4);
|
|
|
|
/** @psalm-suppress MixedAssignment */
|
|
|
|
$b = $child->setString(5);
|
|
|
|
$c = $child->getBool("hello");
|
|
|
|
$d = $child->getArray();
|
|
|
|
$e = $child->getCallable();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2018-04-22 05:08:08 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCannotOverrideParentRetunTypeWhenIgnoringPhpDocMethod()
|
|
|
|
{
|
|
|
|
Config::getInstance()->use_phpdoc_methods_without_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public static function getMe() : self {
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method getMe() : Child
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}
|
|
|
|
|
|
|
|
$child = Child::getMe();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$this->assertSame('Parent', (string) $context->vars_in_scope['$child']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testOverrideParentRetunType()
|
|
|
|
{
|
|
|
|
Config::getInstance()->use_phpdoc_methods_without_call = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public static function getMe() : self {
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method getMe() : Child
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}
|
|
|
|
|
|
|
|
$child = Child::getMe();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$this->assertSame('Child', (string) $context->vars_in_scope['$child']);
|
|
|
|
}
|
|
|
|
|
2017-03-19 19:39:05 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
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
|
|
|
'deprecatedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function barBar(): 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
|
|
|
],
|
|
|
|
'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 {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-05 22:06:06 +01:00
|
|
|
'arrayOfClassConstants' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
takesClassConstants([A::class, B::class]);',
|
|
|
|
],
|
|
|
|
'arrayOfStringClasses' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
takesClassConstants(["A", "B"]);',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['TypeCoercion'],
|
|
|
|
],
|
|
|
|
'singleClassConstant' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param class-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
takesClassConstants(A::class);',
|
|
|
|
],
|
2018-03-05 23:10:52 +01:00
|
|
|
'singleClassConstantWithString' => [
|
2018-03-05 22:06:06 +01:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param class-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
takesClassConstants("A");',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['TypeCoercion'],
|
|
|
|
],
|
2018-03-05 23:10:52 +01:00
|
|
|
'returnClassConstant' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : string {
|
|
|
|
return A::class;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'returnClassConstantAllowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : string {
|
|
|
|
return "A";
|
|
|
|
}',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
|
|
|
|
],
|
|
|
|
'returnClassConstantArray' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return [A::class, B::class];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'returnClassConstantArrayAllowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
2018-03-05 23:46:13 +01:00
|
|
|
class B {}
|
2018-03-05 23:10:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return ["A", "B"];
|
|
|
|
}',
|
|
|
|
'annotations' => [],
|
|
|
|
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
|
|
|
|
],
|
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 = [];
|
|
|
|
|
|
|
|
$arr["a"]()',
|
|
|
|
],
|
|
|
|
'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 = [];
|
|
|
|
|
|
|
|
$arr["a"]()',
|
|
|
|
],
|
2018-04-27 21:00:22 +02:00
|
|
|
'magicMethodValidAnnotations' => [
|
2018-04-22 04:13:10 +02:00
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public function __call() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
* @method void setInteger(int $integer)
|
|
|
|
* @method setString(int $integer)
|
2018-04-23 15:52:40 +02:00
|
|
|
* @method getBool(string $foo) : bool
|
|
|
|
* @method (string|int)[] getArray() : array with some text
|
|
|
|
* @method void setArray(array $arr = array(), int $foo = 5) with some more text
|
2018-04-22 04:13:10 +02:00
|
|
|
* @method (callable() : string) getCallable() : callable
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$a = $child->getString();
|
2018-04-22 04:44:54 +02:00
|
|
|
$child->setInteger(4);
|
2018-04-27 21:00:22 +02:00
|
|
|
/** @psalm-suppress MixedAssignment */
|
2018-04-22 04:13:10 +02:00
|
|
|
$b = $child->setString(5);
|
2018-04-22 04:44:54 +02:00
|
|
|
$c = $child->getBool("hello");
|
2018-04-22 04:13:10 +02:00
|
|
|
$d = $child->getArray();
|
2018-04-23 15:52:40 +02:00
|
|
|
$child->setArray(["boo"])
|
2018-04-22 04:13:10 +02:00
|
|
|
$e = $child->getCallable();',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
'$b' => 'mixed',
|
|
|
|
'$c' => 'bool',
|
|
|
|
'$d' => 'array<mixed, string|int>',
|
|
|
|
'$e' => 'callable():string',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
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
|
|
|
'deprecatedMethodWithCall' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function barBar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Foo::barBar();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DeprecatedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-05-25 06:34:39 +02:00
|
|
|
'deprecatedClassWithStaticCall' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
class Foo {
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function barBar(): void {
|
2017-05-25 06:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Foo::barBar();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DeprecatedClass',
|
2017-05-25 06:34:39 +02:00
|
|
|
],
|
|
|
|
'deprecatedClassWithNew' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
|
|
|
|
$a = new Foo();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DeprecatedClass',
|
2017-05-25 06:34:39 +02:00
|
|
|
],
|
2018-01-23 15:09:43 +01:00
|
|
|
'deprecatedClassWithExtends' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
|
|
|
|
class Bar extends Foo {}',
|
|
|
|
'error_message' => 'DeprecatedClass',
|
|
|
|
],
|
2017-05-25 07:32:34 +02:00
|
|
|
'deprecatedPropertyGet' => [
|
|
|
|
'<?php
|
|
|
|
class A{
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
echo (new A)->foo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DeprecatedProperty',
|
2017-05-25 07:32:34 +02:00
|
|
|
],
|
|
|
|
'deprecatedPropertySet' => [
|
|
|
|
'<?php
|
|
|
|
class A{
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
$a = new A;
|
|
|
|
$a->foo = 5;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'DeprecatedProperty',
|
2017-05-25 07:32:34 +02:00
|
|
|
],
|
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
|
|
|
],
|
|
|
|
'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-03 00:15:52 +02:00
|
|
|
'noStringParamType' => [
|
2017-09-02 17:18:56 +02:00
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-09-03 00:15:52 +02:00
|
|
|
echo substr($a, 4, 2);
|
2017-09-02 17:18:56 +02:00
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,'
|
2017-09-03 00:15:52 +02:00
|
|
|
. ' should be string',
|
|
|
|
'error_levels' => ['MixedArgument'],
|
|
|
|
],
|
2017-09-03 01:23:00 +02:00
|
|
|
'noParamTypeButConcat' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-09-03 01:23:00 +02:00
|
|
|
echo $a . "foo";
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,'
|
2017-09-03 01:23:00 +02:00
|
|
|
. ' should be string',
|
|
|
|
'error_levels' => ['MixedOperand'],
|
|
|
|
],
|
2017-09-07 03:44:26 +02:00
|
|
|
'noParamTypeButAddition' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-09-07 03:44:26 +02:00
|
|
|
echo $a + 5;
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,'
|
2017-09-07 03:44:26 +02:00
|
|
|
. ' should be int|float',
|
|
|
|
'error_levels' => ['MixedOperand', 'MixedArgument'],
|
|
|
|
],
|
|
|
|
'noParamTypeButDivision' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-09-07 03:44:26 +02:00
|
|
|
echo $a / 5;
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,'
|
2017-09-07 03:44:26 +02:00
|
|
|
. ' should be int|float',
|
|
|
|
'error_levels' => ['MixedOperand', 'MixedArgument'],
|
|
|
|
],
|
2017-09-03 01:48:59 +02:00
|
|
|
'noParamTypeButTemplatedString' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-09-03 01:48:59 +02:00
|
|
|
echo "$a";
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,'
|
2017-09-03 01:48:59 +02:00
|
|
|
. ' should be string',
|
|
|
|
'error_levels' => ['MixedOperand'],
|
|
|
|
],
|
2017-09-03 00:15:52 +02:00
|
|
|
'noStringIntParamType' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-09-03 00:15:52 +02:00
|
|
|
if (is_string($a)) {
|
|
|
|
echo substr($a, 4, 2);
|
|
|
|
} else {
|
|
|
|
echo substr("hello", $a, 2);
|
|
|
|
}
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:2 - Parameter $a has no provided type,'
|
2017-09-03 00:15:52 +02:00
|
|
|
. ' should be int|string',
|
|
|
|
'error_levels' => ['MixedArgument'],
|
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-09-03 00:15:52 +02:00
|
|
|
'alreadyHasCheck' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $s): void {}
|
2017-09-03 00:15:52 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function shouldTakeString($s): void {
|
2017-09-03 00:15:52 +02:00
|
|
|
if (is_string($s)) takesString($s);
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:4 - Parameter $s has no provided type,'
|
2017-09-03 00:15:52 +02:00
|
|
|
. ' could not infer',
|
|
|
|
'error_levels' => ['MixedArgument'],
|
|
|
|
],
|
|
|
|
'isSetBeforeInferrence' => [
|
|
|
|
'input' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $s): void {}
|
2017-09-03 00:15:52 +02:00
|
|
|
|
|
|
|
/** @return mixed */
|
|
|
|
function returnsMixed() {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function shouldTakeString($s): void {
|
2017-09-03 00:15:52 +02:00
|
|
|
$s = returnsMixed();
|
|
|
|
takesString($s);
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingParamType - src' . DIRECTORY_SEPARATOR . 'somefile.php:7 - Parameter $s has no provided type,'
|
2017-09-03 00:15:52 +02:00
|
|
|
. ' could not infer',
|
|
|
|
'error_levels' => ['MixedArgument', 'InvalidReturnType', 'MixedAssignment'],
|
|
|
|
],
|
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-05 22:06:06 +01:00
|
|
|
'arrayOfStringClasses' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
|
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
takesClassConstants(["A", "B"]);',
|
|
|
|
'error_message' => 'TypeCoercion',
|
|
|
|
],
|
|
|
|
'arrayOfNonExistentStringClasses' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<class-string> $arr
|
|
|
|
*/
|
|
|
|
function takesClassConstants(array $arr) : void {}
|
|
|
|
takesClassConstants(["A", "B"]);',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
'error_levels' => ['TypeCoercion'],
|
|
|
|
],
|
|
|
|
'singleClassConstantWithInvalidDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param clas-string $s
|
|
|
|
*/
|
|
|
|
function takesClassConstants(string $s) : void {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-03-05 23:10:52 +01:00
|
|
|
'returnClassConstantDisallowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return class-string
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : string {
|
|
|
|
return "A";
|
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
|
|
|
'returnClassConstantArrayDisallowCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return ["A", "B"];
|
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
2018-03-05 23:46:13 +01:00
|
|
|
'returnClassConstantArrayAllowCoercionWithUndefinedClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string>
|
|
|
|
*/
|
|
|
|
function takesClassConstants() : array {
|
|
|
|
return ["A", "B"];
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
|
|
|
|
],
|
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 {
|
|
|
|
return function () : void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-04-22 04:13:10 +02:00
|
|
|
'magicMethodAnnotationWithoutCall' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
*/
|
|
|
|
class Child {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$a = $child->getString();',
|
|
|
|
'error_message' => 'UndefinedMethod',
|
|
|
|
],
|
|
|
|
'magicMethodAnnotationWithBadDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public function __call() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString(\)
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-04-23 15:52:40 +02:00
|
|
|
'magicMethodAnnotationWithUnionTypeInDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public function __call() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString(string|int $x)
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-04-22 06:40:30 +02:00
|
|
|
'magicMethodAnnotationWithSealed' => [
|
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public function __call() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
* @psalm-seal-methods
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
$child->getString();
|
|
|
|
$child->foo();',
|
|
|
|
'error_message' => 'UndefinedMethod - src/somefile.php:14 - Method Child::foo does not exist',
|
|
|
|
],
|
2018-04-22 04:44:54 +02:00
|
|
|
'magicMethodAnnotationInvalidArg' => [
|
|
|
|
'<?php
|
|
|
|
class Parent {
|
|
|
|
public function __call() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method setString(int $integer)
|
|
|
|
*/
|
|
|
|
class Child extends Parent {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setString("five");',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2018-04-25 03:27:31 +02:00
|
|
|
'hyphenInType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return - Description
|
|
|
|
*/
|
|
|
|
function example() {
|
|
|
|
return "placeholder";
|
|
|
|
}',
|
|
|
|
'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
|
|
|
}
|