2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
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
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-24 19:23:22 +01:00
|
|
|
public function testNopType()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
2016-12-24 19:23:22 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/** @var int $a */
|
|
|
|
'
|
|
|
|
);
|
2016-12-24 19:23:22 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-05-27 02:05:57 +02:00
|
|
|
$this->assertSame('int', (string) $context->vars_in_scope['$a']);
|
2016-12-24 19:23:22 +01:00
|
|
|
}
|
2016-12-31 06:14:00 +01:00
|
|
|
|
2017-03-19 19:39:05 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'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
|
|
|
}',
|
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
|
|
|
}',
|
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' => [],
|
2017-12-17 16:58:03 +01:00
|
|
|
'error_level' => ['LoopInvalidation', 'MixedArrayOffset'],
|
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
|
|
|
],
|
|
|
|
'propertyDocblock' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2017-09-02 17:18:56 +02:00
|
|
|
/** @param string $name */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get($name): ?string {
|
2017-09-02 17:18:56 +02:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 00:35:05 +02:00
|
|
|
|
2017-09-02 17:18:56 +02:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set($name, $value): void {
|
2017-09-02 17:18:56 +02:00
|
|
|
}
|
2017-05-05 00:35:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
2017-11-17 02:47:58 +01:00
|
|
|
$a->foo = "hello";
|
|
|
|
$a->bar = "hello"; // not a property',
|
2017-05-05 00:35:05 +02:00
|
|
|
],
|
2017-11-17 22:19:48 +01:00
|
|
|
'propertySealedDocblockDefinedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get(string $name): ?string {
|
2017-11-17 22:19:48 +01:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set(string $name, $value): void {
|
2017-11-17 22:19:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo $a->foo;',
|
|
|
|
],
|
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;
|
|
|
|
}',
|
|
|
|
],
|
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',
|
|
|
|
],
|
|
|
|
'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
|
|
|
],
|
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");',
|
|
|
|
'error_message' => 'TooManyArguments',
|
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
|
|
|
}',
|
2017-07-27 03:30:01 +02:00
|
|
|
'error_message' => 'InvalidDocblock - src/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
|
|
|
],
|
|
|
|
'propertyDocblockInvalidAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get(string $name): ?string {
|
2017-05-05 00:35:05 +02:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:27:01 +01:00
|
|
|
/** @param mixed $value */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set(string $name, $value): void {
|
2017-05-05 00:35:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-05-05 00:35:05 +02:00
|
|
|
],
|
2017-12-14 20:22:27 +01:00
|
|
|
'propertyWriteDocblockInvalidAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property-write string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get(string $name): ?string {
|
2017-12-14 20:22:27 +01:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set(string $name, $value): void {
|
2017-12-14 20:22:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-12-14 20:22:27 +01:00
|
|
|
],
|
2017-11-17 02:47:58 +01:00
|
|
|
'propertySealedDocblockUndefinedPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get(string $name): ?string {
|
2017-11-17 02:47:58 +01:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set(string $name, $value): void {
|
2017-11-17 02:47:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->bar = 5;',
|
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
|
|
|
],
|
2017-11-17 22:19:48 +01:00
|
|
|
'propertySealedDocblockDefinedPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get(string $name): ?string {
|
2017-11-17 22:19:48 +01:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set(string $name, $value): void {
|
2017-11-17 22:19:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-11-17 22:19:48 +01:00
|
|
|
],
|
2017-12-14 20:22:27 +01:00
|
|
|
'propertyReadInvalidFetch' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property-read string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $name) {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo count($a->foo);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2017-11-17 02:47:58 +01:00
|
|
|
'propertySealedDocblockUndefinedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __get(string $name): ?string {
|
2017-11-17 02:47:58 +01:00
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function __set(string $name, $value): void {
|
2017-11-17 02:47:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo $a->bar;',
|
|
|
|
'error_message' => 'UndefinedPropertyFetch',
|
|
|
|
],
|
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
|
|
|
}',
|
2017-09-03 00:15:52 +02:00
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:2 - Parameter $a has no provided type,'
|
|
|
|
. ' 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";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:2 - Parameter $a has no provided type,'
|
|
|
|
. ' 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;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:2 - Parameter $a has no provided type,'
|
|
|
|
. ' 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;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:2 - Parameter $a has no provided type,'
|
|
|
|
. ' 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";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:2 - Parameter $a has no provided type,'
|
|
|
|
. ' 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);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:2 - Parameter $a has no provided type,'
|
|
|
|
. ' 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
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam',
|
|
|
|
'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);
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:4 - Parameter $s has no provided type,'
|
|
|
|
. ' 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);
|
|
|
|
}',
|
|
|
|
'error_message' => 'UntypedParam - src/somefile.php:7 - Parameter $s has no provided type,'
|
|
|
|
. ' 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',
|
|
|
|
]
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-31 06:14:00 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|