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
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2018-01-05 03:36:16 +01:00
|
|
|
* @expectedExceptionMessage NullableReturnStatement
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-15 03:32:14 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testForgetPropertyAssignments()
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
|
|
|
private $x;
|
|
|
|
|
|
|
|
public function getX(): int {
|
2018-04-13 17:24:59 +02:00
|
|
|
$this->x = 5;
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->modifyX();
|
2018-04-13 17:24:59 +02:00
|
|
|
|
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function modifyX(): void {
|
|
|
|
$this->x = null;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testForgetPropertyAssignmentsInBranchWithThrow()
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
|
|
|
private $x;
|
|
|
|
|
|
|
|
public function getX(): int {
|
|
|
|
$this->x = 5;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->modifyX();
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function modifyX(): void {
|
|
|
|
$this->x = null;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testRemovePropertyAfterReassignment()
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var A|null */
|
|
|
|
public $parent;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->parent = rand(0, 1) ? new A : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
if ($a->parent === null) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = $a->parent;'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$this->assertSame('A', (string) $context->vars_in_scope['$a']);
|
|
|
|
$this->assertFalse(isset($context->vars_in_scope['$a->parent']));
|
|
|
|
}
|
|
|
|
|
2018-07-12 22:57:24 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRemoveClauseAfterReassignment()
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'newVarInIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $foo;
|
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 {
|
|
|
|
public $foo;
|
|
|
|
}
|
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' => [
|
2017-10-24 04:04:07 +02:00
|
|
|
'$b' => 'null|int|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' => [
|
2017-10-24 04:04:07 +02:00
|
|
|
'$b' => 'null|int|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;
|
|
|
|
}',
|
|
|
|
],
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void
|
2017-09-14 05:57:11 +02:00
|
|
|
{
|
|
|
|
$this->bar = "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'PropertyNotSetInConstructor' => Config::REPORT_INFO,
|
|
|
|
],
|
|
|
|
],
|
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-01-05 06:15:53 +01:00
|
|
|
'$next' => 'null|Node',
|
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.
|
|
|
|
*
|
|
|
|
* @param Node[] $stmts Statements
|
|
|
|
* @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' => [],
|
|
|
|
'error_levels' => [
|
2018-01-10 16:56:43 +01:00
|
|
|
'MixedTypeCoercion',
|
2017-12-30 16:42:06 +01:00
|
|
|
'MissingReturnType',
|
|
|
|
],
|
|
|
|
],
|
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
|
|
|
|
*/
|
|
|
|
private $mixed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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";
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'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 {}
|
|
|
|
}'
|
|
|
|
],
|
2018-08-09 03:31:13 +02:00
|
|
|
'classStringPropertyType' => [
|
|
|
|
'<?php
|
|
|
|
class C {
|
|
|
|
/** @psalm-var array<class-string, int> */
|
|
|
|
public $member = [
|
|
|
|
InvalidArgumentException::class => 1,
|
|
|
|
];
|
|
|
|
}'
|
|
|
|
],
|
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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
'error_levels' => []
|
|
|
|
],
|
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-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-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);
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'propertySetInGrandparentImplicitly' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $s;
|
|
|
|
|
|
|
|
public function __construct(string $s) {
|
|
|
|
$this->s = $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends B {}'
|
|
|
|
],
|
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";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
['PropertyNotSetInConstructor']
|
|
|
|
],
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct () {
|
|
|
|
$this->x = false;
|
|
|
|
if ($this->func1()) {
|
|
|
|
$this->y = $this->func2();
|
|
|
|
}
|
|
|
|
$this->func2();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2017-04-15 03:32:14 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'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 {
|
|
|
|
public $foo;
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - 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;
|
|
|
|
}
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - 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;
|
|
|
|
}
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - 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;
|
|
|
|
}
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - 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 {}',
|
|
|
|
'error_message' => 'TypeCoercion',
|
|
|
|
],
|
|
|
|
'mixedTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<int, A> */
|
|
|
|
public $foo = [];
|
|
|
|
|
|
|
|
/** @param A[] $arr */
|
|
|
|
public function barBar(array $arr): void
|
|
|
|
{
|
|
|
|
$this->foo = $arr;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedTypeCoercion',
|
|
|
|
],
|
|
|
|
'staticTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var B|null */
|
|
|
|
public static $foo;
|
|
|
|
|
|
|
|
public static function barBar(A $a): void
|
|
|
|
{
|
|
|
|
self::$foo = $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
|
|
|
'error_message' => 'TypeCoercion',
|
|
|
|
],
|
|
|
|
'staticMixedTypeCoercion' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<int, A> */
|
|
|
|
public static $foo = [];
|
|
|
|
|
|
|
|
/** @param A[] $arr */
|
|
|
|
public static function barBar(array $arr): void
|
|
|
|
{
|
|
|
|
self::$foo = $arr;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MixedTypeCoercion',
|
|
|
|
],
|
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() { }
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
2019-02-10 21:01:10 +01:00
|
|
|
'error_message' => 'InaccessibleMethod - src/somefile.php:11',
|
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() {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
|
|
|
],
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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() {
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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;
|
|
|
|
}',
|
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
|
|
|
],
|
|
|
|
'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 {}
|
|
|
|
}',
|
2018-07-22 02:30:38 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2017-06-30 07:24:45 +02:00
|
|
|
'abstractClassWithNoConstructorButChild' => [
|
|
|
|
'<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
|
|
|
],
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
|
|
|
],
|
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";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UninitializedProperty'
|
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UninitializedProperty'
|
|
|
|
],
|
|
|
|
'possiblyNullArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo strlen($this->foo);
|
|
|
|
$this->foo = "foo";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyNullArgument'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|