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\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
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
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-04-15 03:32:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
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;
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class X {
|
|
|
|
/** @var ?int **/
|
|
|
|
private $x;
|
|
|
|
|
|
|
|
public function getX(): int {
|
|
|
|
if ($this->x === null) {
|
|
|
|
$this->x = 0;
|
|
|
|
}
|
|
|
|
$this->modifyX();
|
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function modifyX(): void {
|
|
|
|
$this->x = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'newVarInIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
/** @return void */
|
|
|
|
public function barBar()
|
|
|
|
{
|
|
|
|
if (rand(0,10) === 5) {
|
|
|
|
$this->foo = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
echo $this->foo->baz;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'UndefinedThisPropertyFetch',
|
|
|
|
'MixedAssignment',
|
|
|
|
'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 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new A() : (rand(0, 10) ? new B() : null);
|
|
|
|
$b = null;
|
|
|
|
|
|
|
|
if ($a instanceof A || $a instanceof B) {
|
|
|
|
$b = $a->foo;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-05-27 02:05:57 +02:00
|
|
|
['null|string|int' => '$b'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'sharedPropertyInElseIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo = 0;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
$b = null;
|
|
|
|
|
|
|
|
if (rand(0, 10) === 4) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
elseif ($a instanceof A || $a instanceof B) {
|
|
|
|
$b = $a->foo;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-05-27 02:05:57 +02:00
|
|
|
['null|string|int' => '$b'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullablePropertyCheck' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public $aa = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
/** @var A|null */
|
|
|
|
public $bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = rand(0, 10) ? new A() : new B();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
if (!$a->aa) {
|
|
|
|
$a->aa = "hello";
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public static function getFoo() : A {
|
|
|
|
if (!self::$fooFoo) {
|
|
|
|
self::$fooFoo = new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$fooFoo;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'reflectionProperties' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new \ReflectionMethod("Foo", "__construct");
|
|
|
|
|
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-05-27 02:05:57 +02:00
|
|
|
['DOMDocument' => '$owner'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'goodArrayProperties' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
|
|
|
|
|
|
|
class A1 implements I1{}
|
|
|
|
|
|
|
|
class B1 implements I1 {}
|
|
|
|
|
|
|
|
class C1 {
|
|
|
|
/** @var array<I1> */
|
|
|
|
public $is = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$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 {
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
if (isset($a->bar)) {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function foo() : void {
|
|
|
|
$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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function foo() : void {
|
|
|
|
$this->bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function bar() : void {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertyArrayIssetAssertion' => [
|
|
|
|
'<?php
|
|
|
|
function bar(string $s) : void { }
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $a = [];
|
|
|
|
|
|
|
|
private function foo() : void {
|
|
|
|
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
|
|
|
|
function bar(string $s) : void { }
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $a = [];
|
|
|
|
|
|
|
|
private function foo() : void {
|
|
|
|
$b = "hello";
|
|
|
|
|
|
|
|
if (!isset($this->a[$b])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bar($this->a[$b]);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'staticPropertyArrayIssetAssertionWithVariableOffset' => [
|
|
|
|
'<?php
|
|
|
|
function bar(string $s) : void { }
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public static $a = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo() : void {
|
|
|
|
$b = "hello";
|
|
|
|
|
|
|
|
if (!isset(A::$a[$b])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bar(A::$a[$b]);
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
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
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'undefinedPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
(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 {
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
$this->foo = "cool";
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedThisPropertyAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'undefinedThisPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function fooFoo() : void {
|
|
|
|
echo $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedThisPropertyFetch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyDeclaration' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress UndefinedPropertyAssignment */
|
|
|
|
function fooDo() : void {
|
|
|
|
(new A)->foo = "cool";
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MissingPropertyDeclaration',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyType' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function assignToFoo() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - somefile.php:3 - Property A::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider null|int',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInit' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - 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;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeNull() : void {
|
|
|
|
$this->foo = null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - somefile.php:3 - Property A::$foo does not have a ' .
|
2017-05-27 02:05:57 +02:00
|
|
|
'declared type - consider null|int',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
// Skipped. Doesn't yet work.
|
|
|
|
'SKIPPED-missingPropertyTypeWithConstructorInitInPrivateMethod' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->makeValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function makeValue() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - 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
|
|
|
],
|
|
|
|
'missingPropertyTypeWithConstructorInitAndNullDefault' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public $foo = null;
|
|
|
|
|
|
|
|
public function __construct() : void {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - 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;
|
|
|
|
|
|
|
|
public function barBar() : void
|
|
|
|
{
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidPropertyAssignment',
|
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
|
|
|
],
|
|
|
|
'mixedPropertyFetch' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
|
|
|
|
|
|
|
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 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
|
|
|
|
|
|
|
$a->foo = "hello";',
|
|
|
|
'error_message' => '',
|
|
|
|
'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 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new Foo() : null;
|
|
|
|
|
|
|
|
$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;
|
|
|
|
|
|
|
|
$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 = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new Foo() : null;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 {}
|
|
|
|
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
/** @var array<B> */
|
|
|
|
public $bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = new C;
|
|
|
|
$c->bb = [new A, new B];',
|
|
|
|
'error_message' => 'InvalidPropertyAssignment',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'notSetInEmptyConstructor' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
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
|
|
|
],
|
|
|
|
'notSetInAllBranchesOfIf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function foo() : void {
|
|
|
|
$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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function foo() : void {
|
|
|
|
$this->a = 5;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
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-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|