2016-07-12 06:53:36 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
2018-01-21 16:22:04 +01:00
|
|
|
use Psalm\Context;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class PropertyTypeTest extends TestCase
|
2016-07-12 06:53:36 +02:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-04-15 03:32:14 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2017-04-15 03:32:14 +02:00
|
|
|
*/
|
|
|
|
public function testForgetPropertyAssignments()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('NullableReturnStatement');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2017-04-15 03:32:14 +02:00
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
2019-09-08 00:34:18 +02:00
|
|
|
class XCollector {
|
|
|
|
/** @var X[] */
|
|
|
|
private static array $xs = [];
|
|
|
|
|
|
|
|
public static function modify() : void {
|
|
|
|
foreach (self::$xs as $x) {
|
|
|
|
$x->x = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
2019-09-08 00:34:18 +02:00
|
|
|
public $x;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
|
|
|
public function getX(): int {
|
2018-04-13 17:24:59 +02:00
|
|
|
$this->x = 5;
|
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
XCollector::modify();
|
2018-04-13 17:24:59 +02:00
|
|
|
|
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2018-04-13 17:24:59 +02:00
|
|
|
*/
|
2019-09-08 00:34:18 +02:00
|
|
|
public function testForgetPropertyAssignmentsPassesNormally()
|
2018-04-13 17:24:59 +02:00
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
2019-09-08 00:34:18 +02:00
|
|
|
class XCollector {
|
|
|
|
/** @var X[] */
|
|
|
|
private static array $xs = [];
|
|
|
|
|
|
|
|
public static function modify() : void {
|
|
|
|
foreach (self::$xs as $x) {
|
|
|
|
$x->x = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:24:59 +02:00
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
2019-09-08 00:34:18 +02:00
|
|
|
public $x;
|
2018-04-13 17:24:59 +02:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
public function getX(): int {
|
2018-04-13 17:24:59 +02:00
|
|
|
$this->x = 5;
|
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
XCollector::modify();
|
2018-04-13 17:24:59 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-04-15 03:32:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-07 00:44:53 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-09-08 00:34:18 +02:00
|
|
|
public function testForgetPropertyAssignmentsInBranchWithThrow()
|
2018-02-07 00:44:53 +01:00
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
2019-09-08 00:34:18 +02:00
|
|
|
class XCollector {
|
|
|
|
/** @var X[] */
|
|
|
|
private static array $xs = [];
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
public static function modify() : void {
|
|
|
|
foreach (self::$xs as $x) {
|
|
|
|
$x->x = null;
|
|
|
|
}
|
2018-02-07 00:44:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
|
|
|
public $x;
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
public function getX(bool $b): int {
|
|
|
|
$this->x = 5;
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
if ($b) {
|
|
|
|
XCollector::modify();
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2018-02-07 00:44:53 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 22:57:24 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-09-08 00:34:18 +02:00
|
|
|
public function testForgetPropertyAssignmentsInBranchWithThrowNormally()
|
2018-07-12 22:57:24 +02:00
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
2019-09-08 00:34:18 +02:00
|
|
|
class XCollector {
|
|
|
|
/** @var X[] */
|
|
|
|
private static array $xs = [];
|
2018-07-12 22:57:24 +02:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
public static function modify() : void {
|
|
|
|
foreach (self::$xs as $x) {
|
|
|
|
$x->x = null;
|
|
|
|
}
|
2018-07-12 22:57:24 +02:00
|
|
|
}
|
2019-09-08 00:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
|
|
|
public $x;
|
2018-07-12 22:57:24 +02:00
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
public function getX(bool $b): int {
|
|
|
|
$this->x = 5;
|
|
|
|
|
|
|
|
if ($b) {
|
|
|
|
XCollector::modify();
|
|
|
|
throw new \Exception("bad");
|
2018-07-12 22:57:24 +02:00
|
|
|
}
|
2019-09-08 00:34:18 +02:00
|
|
|
|
|
|
|
return $this->x;
|
2018-07-12 22:57:24 +02:00
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2019-09-08 00:34:18 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2018-07-12 22:57:24 +02:00
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'newVarInIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2019-08-11 06:22:28 +02:00
|
|
|
public $foo = "hello";
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar()
|
|
|
|
{
|
|
|
|
if (rand(0,10) === 5) {
|
|
|
|
$this->foo = [];
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!is_array($this->foo)) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertyWithoutTypeSuppressingIssue' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2019-08-11 06:22:28 +02:00
|
|
|
public $foo = "hello";
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = (new A)->foo;',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MissingPropertyType',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertyWithoutTypeSuppressingIssueAndAssertingNull' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
function foo() {
|
|
|
|
$boop = $this->foo === null && rand(0,1);
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $this->foo->baz;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'UndefinedThisPropertyFetch',
|
|
|
|
'MixedAssignment',
|
2018-05-09 21:49:22 +02:00
|
|
|
'MixedArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
'MixedMethodCall',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedPropertyFetch',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'sharedPropertyInIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo = 0;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = rand(0, 10) ? new A(): (rand(0, 10) ? new B(): null);
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a instanceof A || $a instanceof B) {
|
|
|
|
$b = $a->foo;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'int|null|string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'sharedPropertyInElseIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo = 0;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = rand(0, 10) ? new A(): new B();
|
2018-05-18 17:02:50 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = null;
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 10) === 4) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
elseif ($a instanceof A || $a instanceof B) {
|
|
|
|
$b = $a->foo;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'int|null|string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullablePropertyCheck' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $aa = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var A|null */
|
|
|
|
public $bb;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$b = rand(0, 10) ? new A(): new B();
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($b instanceof B && isset($b->bb) && $b->bb->aa === "aa") {
|
|
|
|
echo $b->bb->aa;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullablePropertyAfterGuard' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $aa;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = new A();
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a->aa) {
|
|
|
|
$a->aa = "hello";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
echo substr($a->aa, 1);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableStaticPropertyWithIfCheck' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var A|null */
|
|
|
|
public static $fooFoo;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public static function getFoo(): A {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!self::$fooFoo) {
|
|
|
|
self::$fooFoo = new A();
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return self::$fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'reflectionProperties' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = new \ReflectionMethod("Foo", "__construct");
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
echo $a->name . " - " . $a->class;',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'grandparentReflectedProperties' => [
|
|
|
|
'<?php
|
|
|
|
$a = new DOMElement("foo");
|
|
|
|
$owner = $a->ownerDocument;',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$owner' => 'DOMDocument',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-10-10 06:54:50 +02:00
|
|
|
'propertyMapHydration' => [
|
|
|
|
'<?php
|
|
|
|
function foo(DOMElement $e) : void {
|
|
|
|
echo $e->attributes->length;
|
|
|
|
}',
|
|
|
|
],
|
2020-01-29 14:39:07 +01:00
|
|
|
'genericTypeFromPropertyMap' => [
|
|
|
|
'<?php
|
|
|
|
function foo(DOMElement $e) : ?DOMAttr {
|
|
|
|
return $e->attributes->item(0);
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'goodArrayProperties' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A1 implements I1{}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B1 implements I1 {}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C1 {
|
|
|
|
/** @var array<I1> */
|
|
|
|
public $is = [];
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$c = new C1;
|
|
|
|
$c->is = [new A1];
|
|
|
|
$c->is = [new A1, new A1];
|
|
|
|
$c->is = [new A1, new B1];',
|
|
|
|
'assertions' => [],
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'issetPropertyDoesNotExist' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = new A();
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (isset($a->bar)) {
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'notSetInConstructorButHasDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a = 0;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() { }
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertySetInPrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->a = 5;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'definedInTraitSetInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
trait A {
|
|
|
|
/** @var string **/
|
|
|
|
public $a;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
use A;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->a = "hello";
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertySetInNestedPrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->bar();
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function bar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->a = 5;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertyArrayIssetAssertion' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(string $s): void { }
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $a = [];
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (isset($this->a["hello"])) {
|
|
|
|
bar($this->a["hello"]);
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertyArrayIssetAssertionWithVariableOffset' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(string $s): void { }
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $a = [];
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = "hello";
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!isset($this->a[$b])) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
bar($this->a[$b]);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'staticPropertyArrayIssetAssertionWithVariableOffset' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(string $s): void { }
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public static $a = [];
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = "hello";
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!isset(A::$a[$b])) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
bar(A::$a[$b]);
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-11-28 23:42:00 +01:00
|
|
|
'staticPropertyArrayIssetAssertionWithVariableOffsetAndElse' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(string $s): void { }
|
2017-11-28 23:42:00 +01:00
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public static $a = [];
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-11-28 23:42:00 +01:00
|
|
|
$b = "hello";
|
|
|
|
|
|
|
|
if (!isset(A::$a[$b])) {
|
|
|
|
$g = "bar";
|
|
|
|
} else {
|
|
|
|
bar(A::$a[$b]);
|
|
|
|
$g = "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
bar($g);
|
|
|
|
}',
|
|
|
|
],
|
2017-06-21 07:25:41 +02:00
|
|
|
'traitConstructor' => [
|
|
|
|
'<?php
|
|
|
|
trait T {
|
|
|
|
/** @var string **/
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
2018-05-30 18:23:53 +02:00
|
|
|
use T;
|
2017-06-21 07:25:41 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-06-30 07:24:45 +02:00
|
|
|
'abstractClassWithNoConstructor' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'abstractClassConstructorAndChildConstructor' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-07-09 03:19:16 +02:00
|
|
|
'abstractClassConstructorAndImplicitChildConstructor' => [
|
2017-06-30 07:24:45 +02:00
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
public function __construct(int $bar) {
|
|
|
|
$this->foo = (string)$bar;
|
2017-06-30 07:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 03:19:16 +02:00
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class E extends \Exception{}',
|
2017-06-30 07:24:45 +02:00
|
|
|
],
|
2017-09-13 17:32:13 +02:00
|
|
|
'notSetInEmptyConstructor' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor */
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() { }
|
|
|
|
}',
|
|
|
|
],
|
2017-09-24 14:17:27 +02:00
|
|
|
'extendsClassWithPrivateConstructorSet' => [
|
|
|
|
'<?php
|
2017-11-30 05:46:56 +01:00
|
|
|
namespace Q;
|
2017-09-24 14:17:27 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
class Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $aString;
|
2017-09-24 14:17:27 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->aString = "aa";
|
|
|
|
echo($this->aString);
|
|
|
|
}
|
|
|
|
}
|
2017-09-24 14:17:27 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
class Descendant extends Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $aBool;
|
2017-09-24 14:17:27 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->aBool = true;
|
|
|
|
}
|
|
|
|
}',
|
2017-09-24 14:17:27 +02:00
|
|
|
],
|
|
|
|
'extendsClassWithPrivateAndException' => [
|
|
|
|
'<?php
|
2017-11-30 05:46:56 +01:00
|
|
|
abstract class A extends \Exception {
|
|
|
|
/** @var string **/
|
|
|
|
private $p;
|
2017-09-24 14:17:27 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
/** @param string $p **/
|
|
|
|
final public function __construct($p) {
|
|
|
|
$this->p = $p;
|
|
|
|
}
|
|
|
|
}
|
2017-09-24 14:17:27 +02:00
|
|
|
|
2017-11-30 05:46:56 +01:00
|
|
|
final class B extends A {}',
|
2017-09-24 14:17:27 +02:00
|
|
|
],
|
2017-09-14 05:57:11 +02:00
|
|
|
'setInAbstractMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void;
|
2017-09-14 05:57:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class A implements I {
|
|
|
|
/** @var string */
|
|
|
|
public $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2019-03-01 23:30:55 +01:00
|
|
|
public function foo(): void {
|
2017-09-14 05:57:11 +02:00
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'PropertyNotSetInConstructor' => Config::REPORT_INFO,
|
|
|
|
],
|
|
|
|
],
|
2019-03-01 23:30:55 +01:00
|
|
|
'callsPrivateParentMethodThenUsesParentInitializedProperty' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setBar(): void {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
echo $this->bar;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-11-30 05:46:56 +01:00
|
|
|
'setInFinalMethod' => [
|
|
|
|
'<?php
|
|
|
|
class C
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $b;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
|
|
|
*/
|
|
|
|
public function __construct(array $opts)
|
|
|
|
{
|
|
|
|
$this->setOptions($opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
|
|
|
*/
|
|
|
|
final public function setOptions(array $opts): void
|
|
|
|
{
|
|
|
|
$this->a = $opts["a"] ?? "defaultA";
|
|
|
|
$this->b = $opts["b"] ?? "defaultB";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'setInFinalClass' => [
|
|
|
|
'<?php
|
|
|
|
final class C
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $b;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
|
|
|
*/
|
|
|
|
public function __construct(array $opts)
|
|
|
|
{
|
|
|
|
$this->setOptions($opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
|
|
|
*/
|
|
|
|
public function setOptions(array $opts): void
|
|
|
|
{
|
|
|
|
$this->a = $opts["a"] ?? "defaultA";
|
|
|
|
$this->b = $opts["b"] ?? "defaultB";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-12-14 04:29:38 +01:00
|
|
|
'selfPropertyType' => [
|
|
|
|
'<?php
|
|
|
|
class Node
|
|
|
|
{
|
|
|
|
/** @var self|null */
|
|
|
|
public $next;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->next = new Node();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = new Node();
|
2018-09-17 18:15:45 +02:00
|
|
|
$next = $node->next;',
|
2017-12-14 04:29:38 +01:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$next' => 'Node|null',
|
2017-12-14 04:29:38 +01:00
|
|
|
],
|
|
|
|
],
|
2017-12-27 12:27:59 +01:00
|
|
|
'perPropertySuppress' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
* @psalm-suppress PropertyNotSetInConstructor
|
|
|
|
*/
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() { }
|
|
|
|
}',
|
|
|
|
],
|
2017-12-30 16:42:06 +01:00
|
|
|
'analyzePropertyMappedClass' => [
|
|
|
|
'<?php
|
|
|
|
namespace PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
|
|
|
|
class Finally_ extends Node\Stmt
|
|
|
|
{
|
|
|
|
/** @var Node[] Statements */
|
|
|
|
public $stmts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a finally node.
|
|
|
|
*
|
2019-10-29 20:46:36 +01:00
|
|
|
* @param array<int, Node\Stmt> $stmts Statements
|
2017-12-30 16:42:06 +01:00
|
|
|
* @param array $attributes Additional attributes
|
|
|
|
*/
|
|
|
|
public function __construct(array $stmts = array(), array $attributes = array()) {
|
|
|
|
parent::__construct($attributes);
|
|
|
|
$this->stmts = $stmts;
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:16:25 +02:00
|
|
|
public function getSubNodeNames() : array {
|
2017-12-30 16:42:06 +01:00
|
|
|
return array("stmts");
|
|
|
|
}
|
2018-04-17 18:16:25 +02:00
|
|
|
|
|
|
|
public function getType() : string {
|
|
|
|
return "Stmt_Finally";
|
|
|
|
}
|
2017-12-30 16:42:06 +01:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
2018-01-13 07:52:46 +01:00
|
|
|
'privatePropertyAccessible' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct(string $foo) {
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct(string $foo) {
|
|
|
|
$this->foo = $foo;
|
|
|
|
parent::__construct($foo);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'privatePropertyAccessibleDifferentType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct(string $foo) {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct(string $foo) {
|
|
|
|
$this->foo = $foo;
|
|
|
|
parent::__construct($foo);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-17 17:00:58 +01:00
|
|
|
'privatePropertyAccessibleInTwoSubclasses' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $prop;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->prop = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class C extends A {
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $prop;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->prop = 2;
|
|
|
|
}
|
|
|
|
}',
|
2018-01-17 22:07:46 +01:00
|
|
|
],
|
2018-02-22 01:34:21 +01:00
|
|
|
'noIssueWhenSuppressingMixedAssignmentForProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
/** @param mixed $a */
|
|
|
|
public function barBar($a): void
|
|
|
|
{
|
|
|
|
$this->foo = $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'propertyAssignmentToMixed' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $a */
|
|
|
|
function barBar(C $c, $a): void
|
|
|
|
{
|
|
|
|
$c->foo = $a;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
2018-03-08 15:29:32 +01:00
|
|
|
'propertySetInBothIfBranches' => [
|
|
|
|
'<?php
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
private $status;
|
|
|
|
|
|
|
|
public function __construct(int $in)
|
|
|
|
{
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->status = 1;
|
|
|
|
} else {
|
|
|
|
$this->status = $in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'propertySetInPrivateMethodWithIfAndElse' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->foo();
|
|
|
|
} else {
|
|
|
|
$this->bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function foo(): void {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function bar(): void {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-04-07 18:13:30 +02:00
|
|
|
'allowMixedAssignmetWhenDesired' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2019-08-11 06:22:28 +02:00
|
|
|
private $mixed = "hello";
|
2018-04-07 18:13:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setMixed($value): void
|
|
|
|
{
|
|
|
|
$this->mixed = $value;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-09 18:54:31 +02:00
|
|
|
'suppressUndefinedThisPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
/** @psalm-suppress UndefinedThisPropertyAssignment */
|
|
|
|
$this->bar = rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedThisPropertyFetch */
|
|
|
|
public function foo() : void {
|
|
|
|
if ($this->bar === null && rand(0, 1)) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'suppressUndefinedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
/** @psalm-suppress UndefinedThisPropertyAssignment */
|
|
|
|
$this->bar = rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
/** @psalm-suppress UndefinedPropertyFetch */
|
|
|
|
if ($a->bar === null && rand(0, 1)) {}',
|
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'setPropertiesOfSpecialObjects' => [
|
|
|
|
'<?php
|
|
|
|
$a = new stdClass();
|
|
|
|
$a->b = "c";
|
|
|
|
|
|
|
|
$d = new SimpleXMLElement("<person><child role=\"son\"></child></person>");
|
|
|
|
$d->e = "f";',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'stdClass',
|
|
|
|
'$a->b' => 'string',
|
|
|
|
'$d' => 'SimpleXMLElement',
|
|
|
|
'$d->e' => 'mixed',
|
|
|
|
],
|
|
|
|
],
|
2018-07-12 22:57:24 +02:00
|
|
|
'allowLessSpecificReturnTypeForOverriddenMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function aa(): ?string {
|
|
|
|
return "bar";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public static function aa(): ?string {
|
|
|
|
return rand(0, 1) ? "bar" : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends A {
|
|
|
|
public static function aa(): ?string {
|
|
|
|
return "bar";
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-07-12 22:57:24 +02:00
|
|
|
],
|
|
|
|
'allowLessSpecificReturnTypeForInterfaceMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {
|
|
|
|
public static function foo(): ?string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar implements Foo {
|
|
|
|
public static function foo(): ?string
|
|
|
|
{
|
|
|
|
return "bar";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz implements Foo {
|
|
|
|
/**
|
|
|
|
* @return string $baz
|
|
|
|
*/
|
|
|
|
public static function foo(): ?string
|
|
|
|
{
|
|
|
|
return "baz";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bax implements Foo {
|
|
|
|
/**
|
|
|
|
* @return null|string $baz
|
|
|
|
*/
|
|
|
|
public static function foo(): ?string
|
|
|
|
{
|
|
|
|
return "bax";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baw implements Foo {
|
|
|
|
/**
|
|
|
|
* @return null|string $baz
|
|
|
|
*/
|
|
|
|
public static function foo(): ?string
|
|
|
|
{
|
|
|
|
/** @var null|string $val */
|
|
|
|
$val = "baw";
|
|
|
|
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-08-07 20:43:05 +02:00
|
|
|
'staticPropertyMethodCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var self|null */
|
|
|
|
public static $instance;
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
public $bat;
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
if (self::$instance) {
|
|
|
|
self::$instance->bar();
|
|
|
|
echo self::$instance->bat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
if ($a->instance) {
|
|
|
|
$a->instance->bar();
|
|
|
|
echo $a->instance->bat;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nonStaticPropertyMethodCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var self|null */
|
|
|
|
public $instance;
|
|
|
|
|
|
|
|
/** @var string|null */
|
|
|
|
public $bat;
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
if ($this->instance) {
|
|
|
|
$this->instance->bar();
|
|
|
|
echo $this->instance->bat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
if ($a->instance) {
|
|
|
|
$a->instance->bar();
|
|
|
|
echo $a->instance->bat;
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-08-07 20:43:05 +02:00
|
|
|
],
|
|
|
|
'staticPropertyOfStaticTypeMethodCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var static|null */
|
|
|
|
public $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $bat;
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
if ($this->instance) {
|
|
|
|
$this->instance->bar();
|
|
|
|
echo $this->instance->bat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar() : void {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-08-07 20:43:05 +02:00
|
|
|
],
|
2018-08-09 03:31:13 +02:00
|
|
|
'classStringPropertyType' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/** @psalm-var array<class-string, int> */
|
|
|
|
public $member = [
|
|
|
|
InvalidArgumentException::class => 1,
|
|
|
|
];
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-08-09 03:31:13 +02:00
|
|
|
],
|
2018-10-10 17:45:56 +02:00
|
|
|
'allowPrivatePropertySetAfterInstanceof' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function bar() : void {
|
|
|
|
if (!$this instanceof B) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->foo = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
|
|
|
],
|
2019-01-09 14:35:53 +01:00
|
|
|
'noCrashForAbstractConstructorWithInstanceofInterface' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if ($this instanceof I) {
|
|
|
|
$this->a = $this->bar();
|
|
|
|
} else {
|
|
|
|
$this->a = 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I {
|
|
|
|
public function bar() : int;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'SKIPPED-abstractConstructorWithInstanceofClass' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if ($this instanceof B) {
|
|
|
|
$this->a = $this->bar();
|
|
|
|
} else {
|
|
|
|
$this->a = 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function bar() : int {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_levels' => [],
|
2019-01-09 14:35:53 +01:00
|
|
|
],
|
2019-01-19 16:09:26 +01:00
|
|
|
'inheritDocPropertyTypes' => [
|
|
|
|
'<?php
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public static $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Y extends X {
|
|
|
|
public $a = "foo";
|
|
|
|
public static $b = "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
(new Y)->a = "hello";
|
|
|
|
echo (new Y)->a;
|
|
|
|
Y::$b = "bar";
|
|
|
|
echo Y::$b;',
|
|
|
|
],
|
2019-01-20 17:20:39 +01:00
|
|
|
'subclassPropertySetInParentConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class Base {
|
|
|
|
/** @var string */
|
|
|
|
protected $prop;
|
|
|
|
public function __construct(string $s) {
|
|
|
|
$this->prop = $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Child extends Base {
|
|
|
|
/** @var string */
|
|
|
|
protected $prop;
|
|
|
|
}',
|
|
|
|
],
|
2019-01-31 23:48:48 +01:00
|
|
|
'callInParentContext' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2019-02-01 00:05:21 +01:00
|
|
|
/** @var int */
|
|
|
|
public $i = 1;
|
2019-01-31 23:48:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class B
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $foo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var A[]
|
|
|
|
*/
|
|
|
|
private $as = [];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->foo = "";
|
|
|
|
$this->bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar(): void
|
|
|
|
{
|
|
|
|
\usort($this->as, function (A $a, A $b): int {
|
|
|
|
return $b->i <=> $a->i;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-02-01 14:58:52 +01:00
|
|
|
'staticVarSelf' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var self */
|
|
|
|
public static $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = Foo::$current;',
|
|
|
|
[
|
|
|
|
'$a' => 'Foo',
|
2019-03-23 19:27:54 +01:00
|
|
|
],
|
2019-02-01 14:58:52 +01:00
|
|
|
],
|
2019-02-05 06:36:57 +01:00
|
|
|
'noMixedErrorWhenAssignmentExpectsMixed' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<string, mixed> $bar */
|
|
|
|
public $bar = [];
|
|
|
|
|
|
|
|
/** @param mixed $b */
|
|
|
|
public function foo($b) : void {
|
|
|
|
$this->bar["a"] = $b;
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-05 06:36:57 +01:00
|
|
|
],
|
2019-02-10 21:01:10 +01:00
|
|
|
'propertySetInGrandparentExplicitly' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $s;
|
|
|
|
|
|
|
|
public function __construct(string $s) {
|
|
|
|
$this->s = $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends B {
|
|
|
|
public function __construct(string $s) {
|
|
|
|
A::__construct($s);
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-10 21:01:10 +01:00
|
|
|
],
|
|
|
|
'propertySetInGrandparentImplicitly' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $s;
|
|
|
|
|
|
|
|
public function __construct(string $s) {
|
|
|
|
$this->s = $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {}
|
2019-03-23 19:27:54 +01:00
|
|
|
class C extends B {}',
|
2019-02-10 21:01:10 +01:00
|
|
|
],
|
2019-02-13 19:32:19 +01:00
|
|
|
'unitializedPropertySuppressPropertyNotSetInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setFoo(); // public method that circumvents checks
|
|
|
|
echo strlen($this->foo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFoo() : void {
|
|
|
|
$this->foo = "foo";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
2019-03-23 19:27:54 +01:00
|
|
|
['PropertyNotSetInConstructor'],
|
2019-02-13 19:32:19 +01:00
|
|
|
],
|
2019-02-15 20:42:17 +01:00
|
|
|
'setObjectLikePropertyType' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @psalm-var array{from:bool, to:bool}
|
|
|
|
*/
|
|
|
|
protected $changed = [
|
|
|
|
"from" => false,
|
|
|
|
"to" => false,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-param "from"|"to" $property
|
|
|
|
*/
|
|
|
|
public function ChangeThing(string $property) : void {
|
|
|
|
$this->changed[$property] = true;
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-15 20:42:17 +01:00
|
|
|
],
|
2019-02-17 02:27:42 +01:00
|
|
|
'noRedundantConditionWhenCheckingInitializations' => [
|
|
|
|
'<?php
|
|
|
|
final class Clazz {
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $y = 0;
|
|
|
|
|
|
|
|
public function func1 (): bool {
|
|
|
|
if ($this->y) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function func2 (): int {
|
|
|
|
if ($this->y) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-02-19 03:31:08 +01:00
|
|
|
public function __construct () {
|
|
|
|
$this->x = false;
|
|
|
|
if ($this->func1()) {
|
|
|
|
$this->y = $this->func2();
|
|
|
|
}
|
|
|
|
$this->func2();
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-19 03:31:08 +01:00
|
|
|
],
|
|
|
|
'noRedundantConditionWhenCheckingInitializationsEdgeCases' => [
|
|
|
|
'<?php
|
|
|
|
final class Clazz {
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $y = 0;
|
|
|
|
|
|
|
|
public function func1 (): bool {
|
|
|
|
if ($this->y !== 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function func2 (): int {
|
|
|
|
if ($this->y !== 0) {
|
|
|
|
return $this->y;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-02-17 02:27:42 +01:00
|
|
|
public function __construct () {
|
|
|
|
$this->x = false;
|
|
|
|
if ($this->func1()) {
|
|
|
|
$this->y = $this->func2();
|
|
|
|
}
|
|
|
|
$this->func2();
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-17 02:27:42 +01:00
|
|
|
],
|
2019-03-01 23:30:55 +01:00
|
|
|
'propertySetInProtectedMethodWithConstant' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function foo(): void {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
const HELLO = "HELLO";
|
|
|
|
|
|
|
|
protected function foo() : void {
|
|
|
|
$this->a = 6;
|
|
|
|
|
|
|
|
echo self::HELLO;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-03-20 00:43:12 +01:00
|
|
|
'setPropertyInParentProtectedMethodExplicitCall' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
public function __construct() {
|
|
|
|
$this->overriddenByB();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function overriddenByB(): void {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var int */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function overriddenByB(): void {
|
|
|
|
$this->foo = 1;
|
|
|
|
$this->bar = 1;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'setPropertyInParentProtectedMethodImplicitCall' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
public function __construct() {
|
|
|
|
$this->overriddenByB();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function overriddenByB(): void {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var int */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $bar;
|
|
|
|
|
|
|
|
protected function overriddenByB(): void {
|
|
|
|
$this->foo = 1;
|
|
|
|
$this->bar = 1;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-04-08 17:01:16 +02:00
|
|
|
'setPropertyInParentWithPrivateConstructor' => [
|
|
|
|
'<?php
|
|
|
|
namespace NS;
|
|
|
|
|
|
|
|
class Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $a;
|
|
|
|
|
|
|
|
private function __construct()
|
|
|
|
{
|
|
|
|
$this->setA();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setA() : void {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getInstance(): self { return new static; }
|
|
|
|
}
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
class Concrete extends Base {}',
|
2019-04-08 17:01:16 +02:00
|
|
|
],
|
2019-04-14 18:19:07 +02:00
|
|
|
'preventCrashWhenCallingInternalMethodInPropertyInitialisationChecks' => [
|
|
|
|
'<?php
|
|
|
|
class Foo extends \RuntimeException {
|
|
|
|
/** @var array */
|
|
|
|
protected $serializableTrace;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct("hello", 0);
|
|
|
|
$this->serializableTrace = $this->getTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar extends Foo {}',
|
|
|
|
],
|
2019-06-09 15:17:45 +02:00
|
|
|
'parentSetsWiderTypeInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {}
|
|
|
|
|
|
|
|
interface FooMore extends Foo {
|
|
|
|
public function something(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
/** @var Foo */
|
|
|
|
protected $foo;
|
|
|
|
|
|
|
|
public function __construct(Foo $foo) {
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class BarMore extends Bar {
|
|
|
|
/** @var FooMore */
|
|
|
|
protected $foo;
|
|
|
|
|
|
|
|
public function __construct(FooMore $foo) {
|
|
|
|
parent::__construct($foo);
|
|
|
|
$this->foo->something();
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-09 15:17:45 +02:00
|
|
|
],
|
2019-06-10 18:00:32 +02:00
|
|
|
'inferPropertyTypesForSimpleConstructors' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private $foo;
|
|
|
|
private $bar;
|
|
|
|
|
|
|
|
public function __construct(int $foot, string $bart) {
|
|
|
|
$this->foo = $foot;
|
|
|
|
$this->bar = $bart;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFoo() : int {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar() : string {
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-06-14 22:53:40 +02:00
|
|
|
'nullableDocblockTypedPropertyNoConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nullableDocblockTypedPropertyEmptyConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct() {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nullableDocblockTypedPropertyUseBeforeInitialised' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-06-21 01:09:10 +02:00
|
|
|
'dontAlterClosureParams' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/** @var array */
|
|
|
|
public $i;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->i = [
|
|
|
|
function (Exception $e): void {},
|
|
|
|
function (LogicException $e): void {},
|
|
|
|
];
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-21 01:09:10 +02:00
|
|
|
],
|
2019-06-29 04:28:35 +02:00
|
|
|
'inferSpreadParamType' => [
|
|
|
|
'<?php
|
|
|
|
class Tag {}
|
|
|
|
class EntityTags {
|
|
|
|
private $tags;
|
|
|
|
|
|
|
|
public function __construct(Tag ...$tags) {
|
|
|
|
$this->tags = $tags;
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-06-29 04:28:35 +02:00
|
|
|
],
|
2019-08-11 22:01:37 +02:00
|
|
|
'readonlyPropertySetInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A)->bar;'
|
|
|
|
],
|
2020-02-02 21:35:12 +01:00
|
|
|
'readonlyWithPrivateMutationsAllowedPropertySetInAnotherMEthod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @psalm-allow-private-mutation
|
|
|
|
*/
|
|
|
|
public ?string $bar = null;
|
|
|
|
|
|
|
|
public function setBar(string $s) : void {
|
|
|
|
$this->bar = $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A)->bar;'
|
|
|
|
],
|
2020-02-02 21:56:08 +01:00
|
|
|
'readonlyPublicPropertySetInAnotherMEthod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @psalm-readonly-allow-private-mutation
|
|
|
|
*/
|
|
|
|
public ?string $bar = null;
|
|
|
|
|
|
|
|
public function setBar(string $s) : void {
|
|
|
|
$this->bar = $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A)->bar;'
|
|
|
|
],
|
2019-08-11 22:01:37 +02:00
|
|
|
'readonlyPropertySetChildClass' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new B)->bar;'
|
|
|
|
],
|
2019-08-12 20:29:14 +02:00
|
|
|
'staticPropertyDefaultWithStaticType' => [
|
|
|
|
'<?php
|
|
|
|
class Test {
|
|
|
|
/** @var array<int, static> */
|
|
|
|
private static $t1 = [];
|
|
|
|
|
|
|
|
/** @var array<int, static> */
|
|
|
|
private $t2 = [];
|
|
|
|
}'
|
2019-08-23 19:27:38 +02:00
|
|
|
],
|
|
|
|
'propagateIgnoreNullableOnPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public string $s = "hey";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-ignore-nullable-return
|
|
|
|
*/
|
|
|
|
function foo() : ?A {
|
|
|
|
return rand(0, 1) ? new A : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesString(string $_s) : void {}
|
|
|
|
|
|
|
|
$foo = foo();
|
|
|
|
|
|
|
|
if ($foo->s !== null) {}
|
|
|
|
echo $foo->s ?? "bar";
|
|
|
|
takesString($foo->s);',
|
|
|
|
],
|
2019-08-27 16:37:39 +02:00
|
|
|
'noMissingPropertyWhenArrayTypeProvided' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
private $bar;
|
|
|
|
|
|
|
|
/** @psalm-param array{key: string} $bar */
|
|
|
|
public function __construct(array $bar) {
|
|
|
|
$this->bar = $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar(): void {
|
|
|
|
echo $this->bar["key"];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-09-08 00:34:18 +02:00
|
|
|
'rememberThisPropertyAsssignmentsInMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public bool $foo = false;
|
|
|
|
|
|
|
|
public function bar() : void {
|
|
|
|
$this->foo = false;
|
|
|
|
$this->maybeChange();
|
|
|
|
|
|
|
|
if ($this->foo) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function maybeChange() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->foo = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'testRemoveClauseAfterReassignment' => [
|
|
|
|
'<?php
|
|
|
|
class Test {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function run(): void {
|
|
|
|
$this->foo = false;
|
|
|
|
$this->bar();
|
|
|
|
if ($this->foo === true) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function bar(): void {
|
|
|
|
if (mt_rand(0, 1)) {
|
|
|
|
$this->foo = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-11-28 15:11:51 +01:00
|
|
|
'allowIssetOnTypedProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public string $a;
|
|
|
|
|
|
|
|
public function __construct(bool $b) {
|
|
|
|
if ($b) {
|
|
|
|
$this->a = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->a)) {
|
|
|
|
echo $this->a;
|
|
|
|
$this->a = "bello";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->a = "bar";
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-01-22 15:57:59 +01:00
|
|
|
'allowGoodArrayPushOnArrayValue' => [
|
|
|
|
'<?php
|
|
|
|
class MyClass {
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $prop = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
array_push($this->prop, 5);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2017-04-15 03:32:14 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'undefinedPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
(new A)->foo = "cool";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo (new A)->foo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedThisPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->foo = "cool";
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedThisPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'undefinedStaticPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function barBar(): void
|
|
|
|
{
|
|
|
|
/** @psalm-suppress UndefinedPropertyFetch */
|
|
|
|
self::$foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'undefinedThisPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedThisPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2019-08-11 06:22:28 +02:00
|
|
|
public $foo = null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function assignToFoo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:32 - Property A::$foo does not have a ' .
|
2018-05-18 17:02:50 +02:00
|
|
|
'declared type - consider int|null',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInit' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-04-21 23:20:21 +02:00
|
|
|
public function __construct() {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:32 - Property A::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider int',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInitAndNull' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-04-21 23:20:21 +02:00
|
|
|
public function __construct() {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->foo = 5;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function makeNull(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->foo = null;
|
|
|
|
}
|
|
|
|
}',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:32 - Property A::$foo does not have a ' .
|
2018-05-18 17:02:50 +02:00
|
|
|
'declared type - consider int|null',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInitAndNullDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo = null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-04-21 23:20:21 +02:00
|
|
|
public function __construct() {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:32 - Property A::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider int|null',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'badAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function barBar(): void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
2018-01-10 16:56:43 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'badStaticAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public static $foo = "a";
|
|
|
|
|
|
|
|
public static function barBar(): void
|
|
|
|
{
|
|
|
|
self::$foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'typeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var B|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function barBar(A $a): void
|
|
|
|
{
|
|
|
|
$this->foo = $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'PropertyTypeCoercion',
|
2018-06-10 16:02:46 +02:00
|
|
|
],
|
|
|
|
'mixedTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<int, A> */
|
|
|
|
public $foo = [];
|
|
|
|
|
|
|
|
/** @param A[] $arr */
|
|
|
|
public function barBar(array $arr): void
|
|
|
|
{
|
|
|
|
$this->foo = $arr;
|
|
|
|
}
|
|
|
|
}',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'MixedPropertyTypeCoercion',
|
2018-06-10 16:02:46 +02:00
|
|
|
],
|
|
|
|
'staticTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var B|null */
|
|
|
|
public static $foo;
|
|
|
|
|
|
|
|
public static function barBar(A $a): void
|
|
|
|
{
|
|
|
|
self::$foo = $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'PropertyTypeCoercion',
|
2018-06-10 16:02:46 +02:00
|
|
|
],
|
|
|
|
'staticMixedTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<int, A> */
|
|
|
|
public static $foo = [];
|
|
|
|
|
|
|
|
/** @param A[] $arr */
|
|
|
|
public static function barBar(array $arr): void
|
|
|
|
{
|
|
|
|
self::$foo = $arr;
|
|
|
|
}
|
|
|
|
}',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'MixedPropertyTypeCoercion',
|
2018-06-10 16:02:46 +02:00
|
|
|
],
|
2018-03-21 04:55:26 +01:00
|
|
|
'possiblyBadAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function barBar(): void
|
|
|
|
{
|
|
|
|
$this->foo = rand(0, 1) ? 5 : "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidPropertyAssignmentValue',
|
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'possiblyBadStaticAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public static $foo = "a";
|
|
|
|
|
|
|
|
public function barBar(): void
|
|
|
|
{
|
|
|
|
self::$foo = rand(0, 1) ? 5 : "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidPropertyAssignmentValue',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'badAssignmentAsWell' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
$a->foo = "bar";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'badFetch' => [
|
|
|
|
'<?php
|
|
|
|
$a = "hello";
|
|
|
|
echo $a->foo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-15 17:44:13 +01:00
|
|
|
'possiblyBadFetch' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 5) > 3 ? "hello" : new stdClass;
|
|
|
|
echo $a->foo;',
|
|
|
|
'error_message' => 'PossiblyInvalidPropertyFetch',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'mixedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $a->foo;',
|
|
|
|
'error_message' => 'MixedPropertyFetch',
|
|
|
|
'error_levels' => [
|
|
|
|
'MissingPropertyType',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mixedPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a->foo = "hello";',
|
2017-11-07 22:21:39 +01:00
|
|
|
'error_message' => 'MixedPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
'error_levels' => [
|
|
|
|
'MissingPropertyType',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyNullablePropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = rand(0, 10) ? new Foo(): null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a->foo = "hello";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullablePropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a->foo = "hello";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'NullPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyNullablePropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
$a = rand(0, 10) ? new Foo(): null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $a->foo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullablePropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $a->foo;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'NullPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'badArrayProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C {
|
|
|
|
/** @var array<B> */
|
|
|
|
public $bb;
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$c = new C;
|
|
|
|
$c->bb = [new A, new B];',
|
2018-01-10 16:56:43 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-11-15 17:15:03 +01:00
|
|
|
],
|
|
|
|
'possiblyBadArrayProperty' => [
|
|
|
|
'<?php
|
2018-01-29 06:39:21 +01:00
|
|
|
class A {
|
|
|
|
/** @var int[] */
|
|
|
|
public $bb = [];
|
2017-11-15 17:15:03 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 06:39:21 +01:00
|
|
|
class B {
|
|
|
|
/** @var string[] */
|
2017-11-15 17:15:03 +01:00
|
|
|
public $bb;
|
|
|
|
}
|
|
|
|
|
2018-01-29 06:39:21 +01:00
|
|
|
$c = rand(0, 1) ? new A : new B;
|
|
|
|
$c->bb = ["hello", "world"];',
|
2018-01-10 16:56:43 +01:00
|
|
|
'error_message' => 'PossiblyInvalidPropertyAssignmentValue',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'notSetInEmptyConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() { }
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'noConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MissingConstructor',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-07-09 03:19:16 +02:00
|
|
|
'abstractClassInheritsNoConstructor' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
|
|
|
'error_message' => 'MissingConstructor',
|
|
|
|
],
|
2017-07-09 04:01:26 +02:00
|
|
|
'abstractClassInheritsPrivateConstructor' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
2018-02-05 21:53:39 +01:00
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
2017-07-09 04:01:26 +02:00
|
|
|
private function __construct() {
|
2018-02-05 21:53:39 +01:00
|
|
|
$this->foo = "hello";
|
2017-07-09 04:01:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 03:51:18 +02:00
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();',
|
|
|
|
'error_message' => 'InaccessibleMethod - src' . DIRECTORY_SEPARATOR . 'somefile.php:13',
|
2017-07-09 04:01:26 +02:00
|
|
|
],
|
|
|
|
'classInheritsPrivateConstructorWithImplementedConstructor' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
private function __construct() {
|
|
|
|
$this->foo = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function __construct() {}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:11',
|
2017-07-09 04:01:26 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'notSetInAllBranchesOfIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertySetInProtectedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
protected function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->a = 5;
|
|
|
|
}
|
2019-03-01 23:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
protected function foo() : void {}
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:15',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'definedInTraitNotSetInEmptyConstructor' => [
|
|
|
|
'<?php
|
|
|
|
trait A {
|
|
|
|
/** @var string **/
|
|
|
|
public $a;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
use A;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:6',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertySetInPrivateMethodWithIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
private function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-07-22 01:56:26 +02:00
|
|
|
'privatePropertySameNameNotSetInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $b;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->b = "foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string */
|
|
|
|
private $b;
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:13',
|
2018-07-22 01:56:26 +02:00
|
|
|
],
|
|
|
|
'privateMethodCalledInParentConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
abstract class B extends A {
|
|
|
|
/** @var string */
|
|
|
|
private $b;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
$this->publicMethod();
|
|
|
|
}
|
|
|
|
|
2018-07-22 02:29:39 +02:00
|
|
|
public function publicMethod() : void {
|
2018-07-22 01:56:26 +02:00
|
|
|
$this->privateMethod();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function privateMethod() : void {}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:2',
|
2018-07-22 01:56:26 +02:00
|
|
|
],
|
|
|
|
'privatePropertySetInParentConstructorReversedOrder' => [
|
|
|
|
'<?php
|
|
|
|
class B extends A {
|
|
|
|
/** @var string */
|
|
|
|
private $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
if ($this instanceof B) {
|
|
|
|
$this->b = "foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2019-03-01 23:30:55 +01:00
|
|
|
'error_message' => 'InaccessibleProperty',
|
2018-07-22 01:56:26 +02:00
|
|
|
],
|
|
|
|
'privatePropertySetInParentConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __construct() {
|
|
|
|
if ($this instanceof B) {
|
|
|
|
$this->b = "foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/** @var string */
|
|
|
|
private $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'undefinedPropertyClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var B */
|
|
|
|
public $foo;
|
|
|
|
}',
|
2019-05-16 00:41:26 +02:00
|
|
|
'error_message' => 'UndefinedDocblockClass',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-06-30 07:24:45 +02:00
|
|
|
'abstractClassWithNoConstructorButChild' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
2019-04-14 18:48:59 +02:00
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
2017-06-30 07:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2019-04-14 18:48:59 +02:00
|
|
|
public function __construct() {}
|
2017-06-30 07:24:45 +02:00
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:7',
|
2017-06-30 07:24:45 +02:00
|
|
|
],
|
2017-12-19 04:35:03 +01:00
|
|
|
'badAssignmentToUndefinedVars' => [
|
|
|
|
'<?php
|
|
|
|
$x->$y = 4;',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'echoUndefinedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
echo $x->$y;',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
2018-01-03 00:01:59 +01:00
|
|
|
'toStringPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function __toString() {
|
|
|
|
return "bar";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = new B;',
|
|
|
|
'error_message' => 'ImplicitToStringCast',
|
|
|
|
],
|
2018-01-24 19:11:23 +01:00
|
|
|
'noInfiniteLoop' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->doThing();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doThing(): void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->doOtherThing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doOtherThing(): void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->doThing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4',
|
2018-01-24 19:11:23 +01:00
|
|
|
],
|
2018-01-29 03:43:26 +01:00
|
|
|
'invalidPropertyDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a = "hello";
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
2018-04-07 18:13:30 +02:00
|
|
|
'prohibitMixedAssignmentNormally' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $mixed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function setMixed($value): void
|
|
|
|
{
|
|
|
|
$this->mixed = $value;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedAssignment',
|
|
|
|
],
|
2018-04-10 07:27:26 +02:00
|
|
|
'assertPropertyTypeHasImpossibleType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?B */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class B {}
|
|
|
|
$a = new A();
|
|
|
|
if (is_string($a->foo)) {}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction',
|
|
|
|
],
|
2018-07-10 18:32:53 +02:00
|
|
|
'impossiblePropertyCheck' => [
|
|
|
|
'<?php
|
|
|
|
class Bar {}
|
|
|
|
class Foo {
|
|
|
|
/** @var Bar */
|
|
|
|
private $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = new Bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBar(): void {
|
|
|
|
if (!$this->bar) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction',
|
|
|
|
],
|
2018-08-07 20:43:05 +02:00
|
|
|
'staticPropertyOfStaticTypeMethodCallWithUndefinedMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var static|null */
|
|
|
|
public $instance;
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
if ($this->instance) {
|
|
|
|
$this->instance->bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function bar() : void {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedMethod',
|
|
|
|
],
|
2018-11-21 22:57:54 +01:00
|
|
|
'misnamedPropertyByVariable' => [
|
|
|
|
'<?php
|
|
|
|
class B {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function bar(string $var_name) : ?string {
|
|
|
|
if ($var_name === "bar") {
|
|
|
|
return $this->$var_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedThisPropertyFetch',
|
|
|
|
],
|
2019-01-19 16:09:26 +01:00
|
|
|
'inheritDocPropertyTypesIncorrectAssignmentToInstanceProperty' => [
|
|
|
|
'<?php
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Y extends X {
|
|
|
|
public $a = "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
(new Y)->a = 5;
|
|
|
|
echo (new Y)->a;
|
|
|
|
Y::$b = "bar";
|
|
|
|
echo Y::$b;',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'inheritDocPropertyTypesIncorrectAssignmentToStaticProperty' => [
|
|
|
|
'<?php
|
|
|
|
class X {
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public static $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Y extends X {
|
|
|
|
public static $b = "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
Y::$b = 5;',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
2019-02-13 19:32:19 +01:00
|
|
|
'unitializedProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo strlen($this->foo);
|
|
|
|
$this->foo = "foo";
|
|
|
|
}
|
|
|
|
}',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'UninitializedProperty',
|
2019-02-13 19:32:19 +01:00
|
|
|
],
|
2019-08-11 06:22:28 +02:00
|
|
|
'unitializedPropertyWithoutType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo strlen($this->foo);
|
|
|
|
$this->foo = "foo";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UninitializedProperty',
|
|
|
|
['MixedArgument', 'MissingPropertyType']
|
|
|
|
],
|
2019-02-13 19:32:19 +01:00
|
|
|
'unitializedObjectProperty' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var int */
|
|
|
|
public $bar = 5;
|
|
|
|
}
|
|
|
|
function takesInt(int $i) : void {}
|
|
|
|
class A {
|
|
|
|
/** @var Foo */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct(Foo $foo) {
|
|
|
|
takesInt($this->foo->bar);
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
}',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'UninitializedProperty',
|
2019-02-13 19:32:19 +01:00
|
|
|
],
|
|
|
|
'possiblyNullArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo strlen($this->foo);
|
|
|
|
$this->foo = "foo";
|
|
|
|
}
|
|
|
|
}',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'PossiblyNullArgument',
|
2019-02-13 19:32:19 +01:00
|
|
|
],
|
2019-02-18 22:04:27 +01:00
|
|
|
'noCrashOnMagicCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setA();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call(string $var, array $args) {}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:4',
|
2019-04-14 18:48:59 +02:00
|
|
|
],
|
|
|
|
'reportGoodLocationForPropertyError' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
2019-04-14 19:12:21 +02:00
|
|
|
/** @var string */
|
|
|
|
public $s;
|
2019-04-14 18:48:59 +02:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setS();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setS() : void {
|
|
|
|
$this->s = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D extends C {
|
|
|
|
public function setS() : void {
|
|
|
|
// nothing happens here
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor - src' . DIRECTORY_SEPARATOR . 'somefile.php:15',
|
2019-02-18 22:04:27 +01:00
|
|
|
],
|
2019-06-01 01:49:24 +02:00
|
|
|
'noCrashWhenUnsettingPropertyWithoutDefaultInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var bool */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
unset($this->foo);
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
2019-06-01 01:49:24 +02:00
|
|
|
],
|
2019-06-09 15:17:45 +02:00
|
|
|
'parentSetsWiderTypeInConstructor' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {}
|
|
|
|
|
|
|
|
interface FooMore extends Foo {}
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
/** @var Foo */
|
|
|
|
protected $foo;
|
|
|
|
|
|
|
|
public function __construct(Foo $foo) {
|
|
|
|
$this->foo = $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BarMore extends Bar {
|
|
|
|
/** @var FooMore */
|
|
|
|
protected $foo;
|
|
|
|
|
|
|
|
public function __construct(FooMore $foo) {
|
|
|
|
parent::__construct($foo);
|
|
|
|
$this->foo->something();
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'UndefinedInterfaceMethod',
|
2019-06-09 15:17:45 +02:00
|
|
|
],
|
2019-06-14 22:53:40 +02:00
|
|
|
'nullableTypedPropertyNoConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private ?bool $foo;
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'MissingConstructor',
|
2019-06-14 22:53:40 +02:00
|
|
|
],
|
|
|
|
'nullableTypedPropertyEmptyConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private ?bool $foo;
|
|
|
|
|
|
|
|
public function __construct() {}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
2019-06-14 22:53:40 +02:00
|
|
|
],
|
|
|
|
'nullableTypedPropertyUseBeforeInitialised' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private ?bool $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'UninitializedProperty',
|
2019-06-14 22:53:40 +02:00
|
|
|
],
|
|
|
|
'nullableTypedPropertyNoConstructorWithDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private ?bool $foo;
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'MissingConstructor',
|
2019-06-14 22:53:40 +02:00
|
|
|
],
|
|
|
|
'nullableTypedPropertyEmptyConstructorWithDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private ?bool $foo;
|
|
|
|
|
|
|
|
public function __construct() {}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
2019-06-14 22:53:40 +02:00
|
|
|
],
|
|
|
|
'nullableTypedPropertyUseBeforeInitialisedWithDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private ?bool $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
2019-07-05 22:24:00 +02:00
|
|
|
'error_message' => 'UninitializedProperty',
|
2019-06-14 22:53:40 +02:00
|
|
|
],
|
2019-07-30 04:18:46 +02:00
|
|
|
'badStaticPropertyDefault' => [
|
|
|
|
'<?php
|
|
|
|
class TestStatic {
|
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
|
|
|
public static $test = ["string-key" => 1];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue'
|
|
|
|
],
|
2019-08-11 22:01:37 +02:00
|
|
|
'readonlyPropertySetInConstructorAndAlsoAnotherMethodInsideClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBar() : void {
|
|
|
|
$this->bar = "goodbye";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
|
|
|
'readonlyPropertySetInConstructorAndAlsoAnotherMethodInSublass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function setBar() : void {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
|
|
|
'readonlyPropertySetInConstructorAndAlsoOutsideClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->bar = "goodbye";',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
2020-02-02 21:37:37 +01:00
|
|
|
'readonlyPropertySetInConstructorAndAlsoOutsideClassWithAllowPrivate' => [
|
2020-02-02 21:35:12 +01:00
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @psalm-allow-private-mutation
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAgain() : void {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->bar = "goodbye";',
|
|
|
|
'error_message' => 'InaccessibleProperty - src/somefile.php:19:21',
|
|
|
|
],
|
2020-02-02 21:56:08 +01:00
|
|
|
'readonlyPublicPropertySetInConstructorAndAlsoOutsideClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @psalm-readonly-allow-private-mutation
|
|
|
|
*/
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAgain() : void {
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->bar = "goodbye";',
|
|
|
|
'error_message' => 'InaccessibleProperty - src/somefile.php:18:21',
|
|
|
|
],
|
2019-08-23 19:27:38 +02:00
|
|
|
'addNullToMixedAfterNullablePropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesString(string $s) : void {}
|
|
|
|
|
|
|
|
function takesA(?A $a) : void {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress PossiblyNullPropertyFetch
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
takesString($a->foo);
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyNullArgument',
|
|
|
|
],
|
2019-12-27 16:34:51 +01:00
|
|
|
'catchBadArrayStaticProperty' => [
|
|
|
|
'<?php
|
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
class Foo {}
|
|
|
|
class A {
|
|
|
|
/** @var array<string, object> */
|
|
|
|
public array $map = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*/
|
|
|
|
public function get(string $class) : void {
|
|
|
|
$this->map[$class] = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue'
|
|
|
|
],
|
2020-01-22 15:57:59 +01:00
|
|
|
'preventArrayPushOnArrayValue' => [
|
|
|
|
'<?php
|
|
|
|
class MyClass {
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $prop = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
array_push($this->prop, "bad");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|