2016-07-12 06:53:36 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm\Tests;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
use DateTime;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Config;
|
2018-01-21 16:22:04 +01:00
|
|
|
use Psalm\Context;
|
2021-12-03 20:29:06 +01:00
|
|
|
use Psalm\Exception\CodeException;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2016-07-12 06:53:36 +02:00
|
|
|
|
2021-06-08 04:55:21 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class PropertyTypeTest extends TestCase
|
2016-07-12 06:53:36 +02:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-04-15 03:32:14 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testForgetPropertyAssignments(): void
|
2017-04-15 03:32:14 +02:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('NullableReturnStatement');
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(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 {
|
2020-12-08 22:39:06 +01:00
|
|
|
public ?int $x = null;
|
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;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2018-04-13 17:24:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testForgetPropertyAssignmentsPassesNormally(): void
|
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 {
|
2020-12-08 22:39:06 +01:00
|
|
|
public ?int $x = null;
|
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;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2017-04-15 03:32:14 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:39:06 +01:00
|
|
|
public function testForgetPropertyAssignmentsInBranch(): void
|
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 {
|
2020-12-08 22:39:06 +01:00
|
|
|
public ?int $x = null;
|
|
|
|
}
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2020-12-08 22:39:06 +01:00
|
|
|
function testX(X $x): void {
|
|
|
|
$x->x = 5;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
XCollector::modify();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($x->x === null) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-08 22:39:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2021-02-26 00:11:58 +01:00
|
|
|
public function testFooBar(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private ?int $bar = null;
|
|
|
|
|
|
|
|
public function baz(): void
|
|
|
|
{
|
|
|
|
$this->bar = null;
|
|
|
|
|
|
|
|
foreach (range(1, 5) as $part) {
|
|
|
|
if ($part === 3) {
|
|
|
|
$this->foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->bar === null) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function foo() : void {
|
|
|
|
$this->bar = 5;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-26 00:11:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-12-08 22:39:06 +01:00
|
|
|
public function testForgetFinalMethodCalls(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class XCollector {
|
|
|
|
/** @var X[] */
|
|
|
|
private static array $xs = [];
|
|
|
|
|
|
|
|
public static function modify() : void {
|
|
|
|
foreach (self::$xs as $x) {
|
|
|
|
$x->x = null;
|
2019-09-08 00:34:18 +02:00
|
|
|
}
|
2020-12-08 22:39:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
public ?int $x = null;
|
2018-02-07 00:44:53 +01:00
|
|
|
|
2020-12-08 22:39:06 +01:00
|
|
|
public function __construct(?int $x) {
|
|
|
|
$this->x = $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final function getX() : ?int {
|
2019-09-08 00:34:18 +02:00
|
|
|
return $this->x;
|
|
|
|
}
|
2020-12-08 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function testX(X $x): void {
|
|
|
|
if ($x->getX()) {
|
|
|
|
XCollector::modify();
|
|
|
|
if ($x->getX() === null) {}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-08 22:39:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberImmutableMethodCalls(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('TypeDoesNotContainNull - somefile.php:22:29');
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2020-12-08 22:39:06 +01:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class XCollector {
|
|
|
|
public static function modify() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-immutable */
|
|
|
|
class X {
|
|
|
|
public ?int $x = null;
|
|
|
|
|
|
|
|
public function __construct(?int $x) {
|
|
|
|
$this->x = $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getX() : ?int {
|
|
|
|
return $this->x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testX(X $x): void {
|
|
|
|
if ($x->getX()) {
|
|
|
|
XCollector::modify();
|
|
|
|
if ($x->getX() === null) {}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-08 22:39:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRememberImmutableProperties(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->expectExceptionMessage('TypeDoesNotContainNull - somefile.php:18:29');
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2020-12-08 22:39:06 +01:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class XCollector {
|
|
|
|
public static function modify() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-immutable */
|
|
|
|
class X {
|
|
|
|
public ?int $x = null;
|
|
|
|
|
|
|
|
public function __construct(?int $x) {
|
|
|
|
$this->x = $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testX(X $x): void {
|
|
|
|
if ($x->x) {
|
|
|
|
XCollector::modify();
|
|
|
|
if ($x->x === null) {}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-08 00:34:18 +02:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2020-12-10 19:51:49 +01:00
|
|
|
public function testNoCrashInTryCatch(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function maybeMutates() : void {}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
public int $f = 0;
|
|
|
|
|
|
|
|
public function validate(): void {
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
$this->f = 1;
|
|
|
|
maybeMutates();
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-10 19:51:49 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2022-02-09 22:37:15 +01:00
|
|
|
public function testAssertionInsideWhileOne(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public array $a = [];
|
|
|
|
public array $b = [];
|
|
|
|
public array $c = [];
|
|
|
|
|
|
|
|
public function one(): bool {
|
|
|
|
$has_changes = false;
|
|
|
|
|
|
|
|
while ($this->a) {
|
|
|
|
$has_changes = true;
|
|
|
|
$this->alter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alter() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->c);
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-02-09 22:37:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssertionInsideWhileTwo(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public array $a = [];
|
|
|
|
public array $b = [];
|
|
|
|
|
|
|
|
public function two(): bool {
|
|
|
|
$has_changes = false;
|
|
|
|
|
|
|
|
while ($this->a || $this->b) {
|
|
|
|
$has_changes = true;
|
|
|
|
$this->alter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_changes;
|
|
|
|
}
|
2022-11-05 22:34:42 +01:00
|
|
|
|
2022-02-09 22:37:15 +01:00
|
|
|
public function alter() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->b);
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-02-09 22:37:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssertionInsideWhileThree(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public array $a = [];
|
|
|
|
public array $b = [];
|
|
|
|
public array $c = [];
|
|
|
|
|
|
|
|
public function three(): bool {
|
|
|
|
$has_changes = false;
|
|
|
|
|
|
|
|
while ($this->a || $this->b || $this->c) {
|
|
|
|
$has_changes = true;
|
|
|
|
$this->alter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alter() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->c);
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-02-09 22:37:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssertionInsideWhileFour(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public array $a = [];
|
|
|
|
public array $b = [];
|
|
|
|
public array $c = [];
|
|
|
|
|
|
|
|
public function four(): bool {
|
|
|
|
$has_changes = false;
|
|
|
|
|
|
|
|
while (($this->a && $this->b) || $this->c) {
|
|
|
|
$has_changes = true;
|
|
|
|
$this->alter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alter() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->c);
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-02-09 22:37:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssertionInsideWhileFive(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->remember_property_assignments_after_call = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public array $a = [];
|
|
|
|
public array $b = [];
|
|
|
|
public array $c = [];
|
|
|
|
|
|
|
|
public function five(): bool {
|
|
|
|
$has_changes = false;
|
|
|
|
|
|
|
|
while ($this->a || ($this->b && $this->c)) {
|
|
|
|
$has_changes = true;
|
|
|
|
$this->alter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alter() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
array_pop($this->c);
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-02-09 22:37:15 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-10-07 15:56:21 +02:00
|
|
|
public function testUniversalObjectCrates(): void
|
|
|
|
{
|
2021-12-03 21:40:18 +01:00
|
|
|
Config::getInstance()->addUniversalObjectCrate(DateTime::class);
|
2020-10-07 15:56:21 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
2020-10-20 23:17:49 +02:00
|
|
|
$f = new \DateTime();
|
2020-10-07 15:56:21 +02:00
|
|
|
// reads are fine
|
|
|
|
$f->bar;
|
|
|
|
|
|
|
|
// sets are fine
|
|
|
|
$f->buzz = false;
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2020-10-07 15:56:21 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testForgetPropertyAssignmentsInBranchWithThrowNormally(): void
|
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
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
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
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'newVarInIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'MissingPropertyType',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'propertyWithoutTypeSuppressingIssueAndAssertingNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo = 0;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2021-12-05 18:51:26 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $foo = 0;
|
|
|
|
}
|
|
|
|
class B {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2021-12-05 18:51:26 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
|
2021-12-05 18:51:26 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class Foo {
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2020-06-11 18:19:27 +02:00
|
|
|
$a = new \ReflectionMethod(Foo::class, "__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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = new DOMElement("foo");
|
|
|
|
$owner = $a->ownerDocument;',
|
|
|
|
'assertions' => [
|
2020-04-06 19:17:02 +02:00
|
|
|
'$owner' => 'DOMDocument|null',
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-10-10 06:54:50 +02:00
|
|
|
function foo(DOMElement $e) : void {
|
|
|
|
echo $e->attributes->length;
|
|
|
|
}',
|
|
|
|
],
|
2020-01-29 14:39:07 +01:00
|
|
|
'genericTypeFromPropertyMap' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-29 14:39:07 +01:00
|
|
|
function foo(DOMElement $e) : ?DOMAttr {
|
|
|
|
return $e->attributes->item(0);
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-01-29 14:39:07 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'goodArrayProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'issetPropertyDoesNotExist' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-21 07:25:41 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-30 07:24:45 +02:00
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'abstractClassConstructorAndChildConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-30 07:24:45 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-30 07:24:45 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-09-13 17:32:13 +02:00
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor */
|
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() { }
|
|
|
|
}',
|
|
|
|
],
|
2017-09-24 14:17:27 +02:00
|
|
|
'extendsClassWithPrivateConstructorSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-09-14 05:57:11 +02:00
|
|
|
interface I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void;
|
2017-09-14 05:57:11 +02:00
|
|
|
}
|
|
|
|
|
2022-01-13 20:38:17 +01:00
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor */
|
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";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-03-01 23:30:55 +01:00
|
|
|
'callsPrivateParentMethodThenUsesParentInitializedProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-01 23:30:55 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-30 05:46:56 +01:00
|
|
|
class C
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $b;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
2022-11-12 02:14:21 +01:00
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
2017-11-30 05:46:56 +01:00
|
|
|
*/
|
|
|
|
public function __construct(array $opts)
|
|
|
|
{
|
|
|
|
$this->setOptions($opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
2022-11-12 02:14:21 +01:00
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
2017-11-30 05:46:56 +01:00
|
|
|
*/
|
|
|
|
final public function setOptions(array $opts): void
|
|
|
|
{
|
|
|
|
$this->a = $opts["a"] ?? "defaultA";
|
|
|
|
$this->b = $opts["b"] ?? "defaultB";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'setInFinalClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-30 05:46:56 +01:00
|
|
|
final class C
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $a;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $b;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
2022-11-12 02:14:21 +01:00
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
2017-11-30 05:46:56 +01:00
|
|
|
*/
|
|
|
|
public function __construct(array $opts)
|
|
|
|
{
|
|
|
|
$this->setOptions($opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $opts
|
2022-11-12 02:14:21 +01:00
|
|
|
* @psalm-param array{a:string,b:string} $opts
|
2017-11-30 05:46:56 +01:00
|
|
|
*/
|
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-14 04:29:38 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-27 12:27:59 +01:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
* @psalm-suppress PropertyNotSetInConstructor
|
|
|
|
*/
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() { }
|
|
|
|
}',
|
|
|
|
],
|
2017-12-30 16:42:06 +01:00
|
|
|
'analyzePropertyMappedClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-30 16:42:06 +01:00
|
|
|
namespace PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
|
|
|
|
class Finally_ extends Node\Stmt
|
|
|
|
{
|
2021-03-23 06:30:51 +01:00
|
|
|
/** @var list<Node\Stmt> Statements */
|
2017-12-30 16:42:06 +01:00
|
|
|
public $stmts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a finally node.
|
|
|
|
*
|
2021-03-23 06:30:51 +01:00
|
|
|
* @param list<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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-13 07:52:46 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-13 07:52:46 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-17 17:00:58 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-22 01:34:21 +01:00
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
/** @param mixed $a */
|
|
|
|
public function barBar($a): void
|
|
|
|
{
|
|
|
|
$this->foo = $a;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2018-02-22 01:34:21 +01:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'propertyAssignmentToMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-22 01:34:21 +01:00
|
|
|
class C {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $a */
|
|
|
|
function barBar(C $c, $a): void
|
|
|
|
{
|
|
|
|
$c->foo = $a;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2018-02-22 01:34:21 +01:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
2018-03-08 15:29:32 +01:00
|
|
|
'propertySetInBothIfBranches' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-08 15:29:32 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
private $status;
|
|
|
|
|
|
|
|
public function __construct(int $in)
|
|
|
|
{
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->status = 1;
|
|
|
|
} else {
|
|
|
|
$this->status = $in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'propertySetInPrivateMethodWithIfAndElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-08 15:29:32 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-07 18:13:30 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 18:54:31 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 18:54:31 +02:00
|
|
|
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)) {}',
|
|
|
|
],
|
2023-07-25 14:59:00 +02:00
|
|
|
'setPropertiesOfStdClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
$a = new stdClass();
|
2023-07-25 14:59:00 +02:00
|
|
|
$a->b = "c";',
|
2018-06-10 16:02:46 +02:00
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'stdClass',
|
|
|
|
'$a->b' => 'string',
|
2023-07-25 14:59:00 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'getPropertiesOfSimpleXmlElement' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$a = new SimpleXMLElement("<person><child role=\"son\"></child></person>");
|
|
|
|
$b = $a->b;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'SimpleXMLElement',
|
|
|
|
'$a->b' => 'SimpleXMLElement|null',
|
|
|
|
'$b' => 'SimpleXMLElement|null',
|
2018-06-10 16:02:46 +02:00
|
|
|
],
|
|
|
|
],
|
2018-07-12 22:57:24 +02:00
|
|
|
'allowLessSpecificReturnTypeForOverriddenMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-12 22:57:24 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-12 22:57:24 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-07 20:43:05 +02:00
|
|
|
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 {}
|
|
|
|
}
|
|
|
|
|
2021-07-18 01:39:23 +02:00
|
|
|
if (A::$instance) {
|
|
|
|
A::$instance->bar();
|
|
|
|
echo A::$instance->bat;
|
2018-08-07 20:43:05 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nonStaticPropertyMethodCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-07 20:43:05 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-07 20:43:05 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-09 03:31:13 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-10-10 17:45:56 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-09 14:35:53 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-09 14:35:53 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2019-01-09 14:35:53 +01:00
|
|
|
],
|
2019-01-19 16:09:26 +01:00
|
|
|
'inheritDocPropertyTypes' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-19 16:09:26 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-20 17:20:39 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-31 23:48:48 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-01 14:58:52 +01:00
|
|
|
class Foo {
|
|
|
|
/** @var self */
|
|
|
|
public static $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = Foo::$current;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2019-02-01 14:58:52 +01:00
|
|
|
'$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-05 06:36:57 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-10 21:01:10 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-10 21:01:10 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-13 19:32:19 +01:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => ['PropertyNotSetInConstructor'],
|
2019-02-13 19:32:19 +01:00
|
|
|
],
|
2023-05-30 22:31:02 +02:00
|
|
|
'unitializedPropertySuppressPropertyNotSetInAbstractConstructor' => [
|
|
|
|
'code' => '<?php
|
|
|
|
abstract class A {
|
|
|
|
/** @readonly */
|
|
|
|
public string $s;
|
|
|
|
|
|
|
|
abstract public function __construct(string $s);
|
|
|
|
}',
|
|
|
|
],
|
2020-08-30 17:44:14 +02:00
|
|
|
'setTKeyedArrayPropertyType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-15 20:42:17 +01:00
|
|
|
class Foo {
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @psalm-var array{from:bool, to:bool}
|
2019-02-15 20:42:17 +01:00
|
|
|
*/
|
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-17 02:27:42 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-19 03:31:08 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-01 23:30:55 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-20 00:43:12 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-04-21 06:04:47 +02:00
|
|
|
protected final function overriddenByB(): void {
|
2019-03-20 00:43:12 +01:00
|
|
|
$this->foo = 1;
|
|
|
|
$this->bar = 1;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'setPropertyInParentProtectedMethodImplicitCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-20 00:43:12 +01:00
|
|
|
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;
|
|
|
|
|
2020-04-21 06:04:47 +02:00
|
|
|
protected final function overriddenByB(): void {
|
2019-03-20 00:43:12 +01:00
|
|
|
$this->foo = 1;
|
|
|
|
$this->bar = 1;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-04-08 17:01:16 +02:00
|
|
|
'setPropertyInParentWithPrivateConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-08 17:01:16 +02:00
|
|
|
namespace NS;
|
|
|
|
|
|
|
|
class Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $a;
|
|
|
|
|
2020-08-06 01:39:27 +02:00
|
|
|
final private function __construct()
|
2019-04-08 17:01:16 +02:00
|
|
|
{
|
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-14 18:19:07 +02:00
|
|
|
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-10 18:00:32 +02:00
|
|
|
'inferPropertyTypesForSimpleConstructors' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-10 18:00:32 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nullableDocblockTypedPropertyEmptyConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct() {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nullableDocblockTypedPropertyUseBeforeInitialised' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
class A {
|
|
|
|
/** @var ?bool */
|
|
|
|
private $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo $this->foo;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-06-21 01:09:10 +02:00
|
|
|
'dontAlterClosureParams' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-21 01:09:10 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-29 04:28:35 +02:00
|
|
|
class Tag {}
|
|
|
|
class EntityTags {
|
|
|
|
private $tags;
|
|
|
|
|
2021-03-23 00:58:22 +01:00
|
|
|
/** @no-named-arguments */
|
2019-06-29 04:28:35 +02:00
|
|
|
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-12 20:29:14 +02:00
|
|
|
'staticPropertyDefaultWithStaticType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-12 20:29:14 +02:00
|
|
|
class Test {
|
|
|
|
/** @var array<int, static> */
|
|
|
|
private static $t1 = [];
|
|
|
|
|
|
|
|
/** @var array<int, static> */
|
|
|
|
private $t2 = [];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-08-23 19:27:38 +02:00
|
|
|
],
|
|
|
|
'propagateIgnoreNullableOnPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-23 19:27:38 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-27 16:37:39 +02:00
|
|
|
|
|
|
|
class Foo {
|
|
|
|
private $bar;
|
|
|
|
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @psalm-param array{key: string} $bar */
|
2019-08-27 16:37:39 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-08 00:34:18 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-08 00:34:18 +02:00
|
|
|
],
|
|
|
|
'testRemoveClauseAfterReassignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-08 00:34:18 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-11-28 15:11:51 +01:00
|
|
|
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";
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-11-28 15:11:51 +01:00
|
|
|
],
|
2020-01-22 15:57:59 +01:00
|
|
|
'allowGoodArrayPushOnArrayValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-22 15:57:59 +01:00
|
|
|
class MyClass {
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $prop = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
array_push($this->prop, 5);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-03-06 15:42:23 +01:00
|
|
|
'someConditionalCallToParentConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-06 15:42:23 +01:00
|
|
|
class GrandParentClassDoesNotDefine {
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentClassDefinesVar extends GrandParentClassDoesNotDefine {
|
|
|
|
protected string $val;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->val = "hello";
|
2020-03-16 03:23:31 +01:00
|
|
|
if (rand(0, 1)) {
|
2020-03-06 15:42:23 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChildClass extends ParentClassDefinesVar {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-03-06 15:42:23 +01:00
|
|
|
],
|
|
|
|
'noConditionalCallToParentConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-06 15:42:23 +01:00
|
|
|
class GrandParentClassDoesNotDefine {
|
|
|
|
public function __construct() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentClassDefinesVar extends GrandParentClassDoesNotDefine {
|
|
|
|
protected string $val;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->val = "hello";
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChildClass extends ParentClassDefinesVar {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-03-06 15:42:23 +01:00
|
|
|
],
|
2020-03-22 00:23:24 +01:00
|
|
|
'allowByReferenceAssignmentToUninitializedNullableProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-22 00:23:24 +01:00
|
|
|
class C {
|
|
|
|
private ?\Closure $onCancel;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->foo($this->onCancel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $onCancel
|
|
|
|
* @param-out \Closure $onCancel
|
|
|
|
*/
|
|
|
|
public function foo(&$onCancel) : void {
|
|
|
|
$onCancel = function (): void {};
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-03-22 00:23:24 +01:00
|
|
|
],
|
2020-04-08 07:03:37 +02:00
|
|
|
'dontCarryAssertionsOver' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-08 07:03:37 +02:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
private string $network;
|
|
|
|
|
|
|
|
public function __construct(string $s)
|
|
|
|
{
|
|
|
|
$this->network = $s;
|
|
|
|
$this->firstCheck();
|
|
|
|
$this->secondCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function firstCheck(): void
|
|
|
|
{
|
|
|
|
if ($this->network === "x") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function secondCheck(): void
|
|
|
|
{
|
|
|
|
if ($this->network === "x") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-04-08 07:03:37 +02:00
|
|
|
],
|
2020-05-29 15:28:34 +02:00
|
|
|
'useVariableAccessInStatic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-29 15:28:34 +02:00
|
|
|
class A2 {
|
|
|
|
public static string $title = "foo";
|
|
|
|
public static string $label = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
$model = new A2();
|
|
|
|
$message = $model::$title;
|
|
|
|
$message .= $model::$label;
|
2022-12-18 17:15:15 +01:00
|
|
|
echo $message;',
|
2020-05-29 15:28:34 +02:00
|
|
|
],
|
2020-07-25 23:26:07 +02:00
|
|
|
'staticPropertyInFinalMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-25 23:26:07 +02:00
|
|
|
abstract class Foo {
|
|
|
|
/** @var static */
|
|
|
|
protected Foo $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
final class Bar extends Foo {
|
|
|
|
public function __construct(Bar $bar) {
|
|
|
|
$this->foo = $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function baz(): Bar {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-07-26 20:46:52 +02:00
|
|
|
],
|
|
|
|
'aliasedFinalMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-26 20:46:52 +02:00
|
|
|
trait A {
|
|
|
|
private int $prop;
|
|
|
|
public final function setProp(int $prop): void {
|
|
|
|
$this->prop = $prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use A {
|
|
|
|
setProp as setPropFinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setPropFinal(1);
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-07-26 20:46:52 +02:00
|
|
|
],
|
|
|
|
'aliasedAsFinalMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-26 20:46:52 +02:00
|
|
|
trait A {
|
|
|
|
private int $prop;
|
|
|
|
public function setProp(int $prop): void {
|
|
|
|
$this->prop = $prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
use A {
|
|
|
|
setProp as final setPropFinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->setPropFinal(1);
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-07-31 18:44:01 +02:00
|
|
|
],
|
|
|
|
'staticPropertyAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-31 18:44:01 +02:00
|
|
|
class Foo {
|
|
|
|
/** @var int */
|
|
|
|
private static $transactionDepth;
|
|
|
|
|
|
|
|
function bar(): void {
|
|
|
|
if (self::$transactionDepth === 0) {
|
|
|
|
} else {
|
|
|
|
--self::$transactionDepth;
|
|
|
|
|
|
|
|
if (self::$transactionDepth === 0) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-08-03 08:06:42 +02:00
|
|
|
],
|
|
|
|
'dontMemoizePropertyTypeAfterRootVarAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-03 08:06:42 +02:00
|
|
|
class A {
|
|
|
|
public string $i = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public int $i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param A|B $o */
|
|
|
|
function takesAorB(object $o) : void {
|
|
|
|
echo $o->i;
|
|
|
|
if ($o instanceof A) {
|
|
|
|
echo strlen($o->i);
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-07-25 23:26:07 +02:00
|
|
|
],
|
2020-08-30 19:16:37 +02:00
|
|
|
'unionPropertyType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-30 19:16:37 +02:00
|
|
|
class A {
|
|
|
|
public string|int $i;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->i = 5;
|
|
|
|
$this->i = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
if ($a->i === 3) {}
|
2022-01-22 01:00:45 +01:00
|
|
|
if ($a->i === "foo") {}',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2020-08-30 19:16:37 +02:00
|
|
|
],
|
2020-09-20 15:18:27 +02:00
|
|
|
'setClassStringOfStatic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-20 15:18:27 +02:00
|
|
|
class A {
|
|
|
|
public static array $stack = [];
|
|
|
|
|
|
|
|
public static function foo() : void {
|
|
|
|
$class = get_called_class();
|
|
|
|
$class::$stack[] = 1;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-09-20 15:18:27 +02:00
|
|
|
],
|
2020-10-03 01:44:58 +02:00
|
|
|
'promotedPublicPropertyWithDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-03 00:31:32 +02:00
|
|
|
class A {
|
|
|
|
public function __construct(public int $foo = 5) {}
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
echo (new A)->foo;',
|
2020-10-03 00:31:32 +02:00
|
|
|
],
|
2020-10-03 01:44:58 +02:00
|
|
|
'promotedPublicPropertyWitoutDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-03 01:44:58 +02:00
|
|
|
class A {
|
|
|
|
public function __construct(public int $foo) {}
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
echo (new A(5))->foo;',
|
2020-10-03 01:44:58 +02:00
|
|
|
],
|
2020-10-03 00:31:32 +02:00
|
|
|
'promotedProtectedProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-03 00:31:32 +02:00
|
|
|
class A {
|
2020-10-03 01:44:58 +02:00
|
|
|
public function __construct(protected int $foo) {}
|
2020-10-03 00:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class AChild extends A {
|
|
|
|
public function bar() : int {
|
|
|
|
return $this->foo;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-10-03 00:31:32 +02:00
|
|
|
],
|
2020-11-13 05:56:29 +01:00
|
|
|
'skipConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-13 05:56:29 +01:00
|
|
|
class A {
|
|
|
|
protected string $s;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->s = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class C extends B {
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
echo $this->s;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-11-13 05:56:29 +01:00
|
|
|
],
|
2020-11-29 15:26:39 +01:00
|
|
|
'getPropertyThatMayNotBeSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-29 15:26:39 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingConstructor
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public string $bar;
|
|
|
|
|
|
|
|
public function getBar() : string {
|
2020-11-29 17:57:20 +01:00
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */
|
2020-11-29 15:26:39 +01:00
|
|
|
if (!isset($this->bar)) {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBarAgain() : string {
|
2020-11-29 17:57:20 +01:00
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */
|
2020-11-29 15:26:39 +01:00
|
|
|
if (isset($this->bar)) {
|
|
|
|
return $this->bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-12-01 23:25:45 +01:00
|
|
|
'memoizePropertyAfterSetting' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-01 23:25:45 +01:00
|
|
|
class A {
|
|
|
|
public function foo() : void {
|
|
|
|
/** @psalm-suppress UndefinedThisPropertyAssignment */
|
|
|
|
$this->b = "c";
|
|
|
|
echo strlen($this->b);
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-01 23:25:45 +01:00
|
|
|
],
|
2020-12-03 16:12:54 +01:00
|
|
|
'noErrorForSplatArgs' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-03 16:12:54 +01:00
|
|
|
class Foo {
|
|
|
|
protected array $b;
|
|
|
|
|
|
|
|
protected function __construct(?string ...$bb) {
|
|
|
|
$this->b = $bb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
class Bar extends Foo {}',
|
2020-12-03 16:12:54 +01:00
|
|
|
],
|
2020-12-03 17:13:10 +01:00
|
|
|
'noUndefinedPropertyIssueAfterSuppressingOnInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-03 17:13:10 +01:00
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function bar(I $i) : void {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress NoInterfaceProperties
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
echo $i->foo;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-03 17:13:10 +01:00
|
|
|
],
|
2020-12-06 17:07:59 +01:00
|
|
|
'noRedundantCastWhenCheckingProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-06 17:07:59 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public array $map;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->map = [];
|
|
|
|
$this->map["test"] = "test";
|
|
|
|
|
|
|
|
$this->useMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function useMap(): void
|
|
|
|
{
|
|
|
|
$keys = array_keys($this->map);
|
|
|
|
$key = reset($keys);
|
|
|
|
echo (string) $key;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-06 17:07:59 +01:00
|
|
|
],
|
2020-12-10 07:29:07 +01:00
|
|
|
'ignoreUndefinedMethodOnUnion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-10 07:29:07 +01:00
|
|
|
class NullObject {
|
|
|
|
/**
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function __get(string $s) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class User {
|
|
|
|
public string $name = "Dave";
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:40:20 +01:00
|
|
|
function takesNullableUser(User|NullObject $user) : ?string {
|
|
|
|
$name = $user->name;
|
|
|
|
|
|
|
|
if ($name === null) {}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.0',
|
2020-12-10 07:29:07 +01:00
|
|
|
],
|
2021-01-17 18:08:19 +01:00
|
|
|
'dynamicPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-17 18:08:19 +01:00
|
|
|
class Foo {
|
|
|
|
public int $a = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesFoo(?Foo $foo, string $b) : void {
|
2021-04-25 21:22:32 +02:00
|
|
|
/** @psalm-suppress MixedArgument */
|
2021-01-17 18:08:19 +01:00
|
|
|
echo $foo->{$b} ?? null;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-01-17 18:08:19 +01:00
|
|
|
],
|
2021-01-27 05:10:57 +01:00
|
|
|
'nullCoalesceWithNullablePropertyAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-27 05:10:57 +01:00
|
|
|
class Bar {
|
|
|
|
public ?string $a = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesBar(?Bar $bar) : string {
|
|
|
|
return $bar?->a ?? "default";
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.0',
|
2021-01-27 05:10:57 +01:00
|
|
|
],
|
2021-02-08 04:56:37 +01:00
|
|
|
'possiblyNullOnFunctionCallCoalesced' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-08 04:56:37 +01:00
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var int */
|
|
|
|
public $a = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function accessOnVar(?Foo $bar, string $b) : void {
|
2021-04-25 21:22:32 +02:00
|
|
|
/** @psalm-suppress MixedArgument */
|
2021-02-08 04:56:37 +01:00
|
|
|
echo $bar->{$b} ?? null;
|
|
|
|
}',
|
|
|
|
],
|
2021-02-09 16:23:22 +01:00
|
|
|
'dontMemoizeConditionalAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-09 16:23:22 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
protected ?A $a = null;
|
|
|
|
|
|
|
|
public function test(): void {
|
|
|
|
if (!$this->a) {
|
|
|
|
$this->mayBeSetA();
|
|
|
|
}
|
|
|
|
if ($this->a instanceof A) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mayBeSetA(): void {
|
|
|
|
if (mt_rand(0, 1)) {
|
|
|
|
$this->a = new A();
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-09 16:23:22 +01:00
|
|
|
],
|
2021-02-11 15:38:04 +01:00
|
|
|
'allowDefaultForTemplatedProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-11 15:38:04 +01:00
|
|
|
/**
|
|
|
|
* @template T as string|null
|
|
|
|
*/
|
|
|
|
abstract class A {
|
|
|
|
/** @var list<T> */
|
|
|
|
public $foo = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @extends A<string>
|
|
|
|
*/
|
|
|
|
class AChild extends A {
|
|
|
|
public $foo = ["hello"];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-11 15:38:04 +01:00
|
|
|
],
|
2021-02-11 20:12:15 +01:00
|
|
|
'allowBuiltinPropertyDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-11 20:12:15 +01:00
|
|
|
class FooException extends LogicException {
|
|
|
|
/** @var int */
|
|
|
|
protected $code = 404;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-11 20:12:15 +01:00
|
|
|
],
|
2021-03-05 06:39:19 +01:00
|
|
|
'dontMemoizeFinalMutationFreeInferredMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-05 06:39:19 +01:00
|
|
|
final class ExecutionMode
|
|
|
|
{
|
|
|
|
private bool $isAutoCommitEnabled = true;
|
|
|
|
|
|
|
|
public function enableAutoCommit(): void
|
|
|
|
{
|
|
|
|
$this->isAutoCommitEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function disableAutoCommit(): void
|
|
|
|
{
|
|
|
|
$this->isAutoCommitEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAutoCommitEnabled(): bool
|
|
|
|
{
|
|
|
|
return $this->isAutoCommitEnabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mode = new ExecutionMode();
|
|
|
|
$mode->disableAutoCommit();
|
|
|
|
assert($mode->isAutoCommitEnabled() === false);
|
|
|
|
|
|
|
|
$mode->enableAutoCommit();
|
2022-12-18 17:15:15 +01:00
|
|
|
assert($mode->isAutoCommitEnabled() === true);',
|
2021-03-05 06:39:19 +01:00
|
|
|
],
|
2021-05-02 16:45:55 +02:00
|
|
|
'promotedInheritedPropertyWithDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-02 16:45:55 +02:00
|
|
|
abstract class A {
|
|
|
|
/** @var array */
|
|
|
|
public array $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
final class B extends A {
|
|
|
|
/** @param array $arr */
|
|
|
|
protected function __construct(public array $arr){}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-05-02 16:45:55 +02:00
|
|
|
],
|
2021-06-01 22:06:12 +02:00
|
|
|
'nullsafeShortCircuit' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-06-01 22:06:12 +02:00
|
|
|
class Foo {
|
|
|
|
private ?self $nullableSelf = null;
|
|
|
|
|
|
|
|
public function __construct(private self $self) {}
|
|
|
|
|
|
|
|
public function doBar(): ?self
|
|
|
|
{
|
|
|
|
return $this->nullableSelf?->self->self;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.0',
|
2021-06-01 22:06:12 +02:00
|
|
|
],
|
2022-12-04 20:31:56 +01:00
|
|
|
'impossibleIntersection' => [
|
|
|
|
'code' => '<?php
|
|
|
|
class Foo {}
|
|
|
|
class Bar {}
|
|
|
|
/** @psalm-suppress MissingConstructor */
|
|
|
|
class Baz
|
|
|
|
{
|
|
|
|
private Foo&Bar $foobar;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
2022-12-18 17:15:15 +01:00
|
|
|
'php_version' => '8.1',
|
2022-12-04 20:31:56 +01:00
|
|
|
],
|
2023-04-21 12:33:08 +02:00
|
|
|
'intersectionPropertyAccess' => [
|
|
|
|
'code' => '<?php
|
|
|
|
|
|
|
|
/** @property int $test1 */
|
|
|
|
class a {
|
|
|
|
public function __get(string $name)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var a&object{test2: "lmao"} */
|
|
|
|
$r = null;
|
|
|
|
|
|
|
|
$test1 = $r->test1;
|
|
|
|
$test2 = $r->test2;',
|
|
|
|
'assertions' => [
|
|
|
|
'$test1===' => 'int',
|
|
|
|
'$test2===' => "'lmao'",
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2017-04-15 03:32:14 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'undefinedPropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
],
|
2020-07-08 17:46:55 +02:00
|
|
|
'missingPropertyTypeWithConstructorInitConditionallySet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-08 17:46:55 +02:00
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->foo = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:32 - Property A::$foo does not have a ' .
|
|
|
|
'declared type - consider int|null',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'badAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
class A {
|
|
|
|
/** @var string */
|
|
|
|
public static $foo = "a";
|
|
|
|
|
|
|
|
public static function barBar(): void
|
|
|
|
{
|
|
|
|
self::$foo = 5;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'typeCoercion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-21 04:55:26 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-15 17:44:13 +01:00
|
|
|
$a = rand(0, 5) > 3 ? "hello" : new stdClass;
|
|
|
|
echo $a->foo;',
|
|
|
|
'error_message' => 'PossiblyInvalidPropertyFetch',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'mixedPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'MissingPropertyType',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mixedPropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'MissingPropertyType',
|
2017-05-27 02:05:57 +02:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyNullablePropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2021-12-05 18:51:26 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class Foo {
|
|
|
|
/** @var string */
|
|
|
|
public $foo = "";
|
|
|
|
}
|
2017-06-21 07:25:41 +02:00
|
|
|
|
2021-12-05 18:51:26 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-07-09 03:19:16 +02:00
|
|
|
abstract class A {
|
|
|
|
/** @var string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}',
|
|
|
|
'error_message' => 'MissingConstructor',
|
|
|
|
],
|
2017-07-09 04:01:26 +02:00
|
|
|
'abstractClassInheritsPrivateConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-07-09 04:01:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-07-09 04:01:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-22 01:56:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-22 01:56:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-22 01:56:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-22 01:56:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-30 07:24:45 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-19 04:35:03 +01:00
|
|
|
$x->$y = 4;',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'echoUndefinedPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-19 04:35:03 +01:00
|
|
|
echo $x->$y;',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
2018-01-03 00:01:59 +01:00
|
|
|
'toStringPropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-03 00:01:59 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-24 19:11:23 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-29 03:43:26 +01:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $a = "hello";
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
2018-04-07 18:13:30 +02:00
|
|
|
'prohibitMixedAssignmentNormally' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-07 18:13:30 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-10 07:27:26 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-07-10 18:32:53 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-07 20:43:05 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-21 22:57:54 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-19 16:09:26 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-19 16:09:26 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-13 19:32:19 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-11 06:22:28 +02:00
|
|
|
class A {
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
echo strlen($this->foo);
|
|
|
|
$this->foo = "foo";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UninitializedProperty',
|
2022-12-18 17:15:15 +01:00
|
|
|
'ignored_issues' => ['MixedArgument', 'MissingPropertyType'],
|
2019-08-11 06:22:28 +02:00
|
|
|
],
|
2019-02-13 19:32:19 +01:00
|
|
|
'unitializedObjectProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-13 19:32:19 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-13 19:32:19 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-18 22:04:27 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-14 18:48:59 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-01 01:49:24 +02:00
|
|
|
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-14 22:53:40 +02:00
|
|
|
'nullableTypedPropertyNoConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-14 22:53:40 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-30 04:18:46 +02:00
|
|
|
class TestStatic {
|
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
|
|
|
public static $test = ["string-key" => 1];
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2019-07-30 04:18:46 +02:00
|
|
|
],
|
2019-08-23 19:27:38 +02:00
|
|
|
'addNullToMixedAfterNullablePropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-23 19:27:38 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-27 16:34:51 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2019-12-27 16:34:51 +01:00
|
|
|
],
|
2020-01-22 15:57:59 +01:00
|
|
|
'preventArrayPushOnArrayValue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-22 15:57:59 +01:00
|
|
|
class MyClass {
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $prop = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function foo() {
|
|
|
|
array_push($this->prop, "bad");
|
|
|
|
}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2020-01-22 15:57:59 +01:00
|
|
|
],
|
2020-04-21 06:04:47 +02:00
|
|
|
'overriddenConstructorCalledMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-21 06:04:47 +02:00
|
|
|
class ParentClass {
|
|
|
|
private string $prop;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function init(): void {
|
|
|
|
$this->prop = "zxc";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChildClass extends ParentClass {
|
|
|
|
public function init(): void {}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'PropertyNotSetInConstructor',
|
2020-04-21 06:04:47 +02:00
|
|
|
],
|
2020-06-23 19:12:46 +02:00
|
|
|
'propertyWithSameNameUndefined' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-23 19:12:46 +02:00
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
class Bar {
|
|
|
|
public int $id = 3;
|
|
|
|
|
|
|
|
public function __construct(Foo $model) {
|
|
|
|
echo $model->id;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedPropertyFetch',
|
|
|
|
],
|
2020-09-13 22:50:50 +02:00
|
|
|
'missingPropertyTypeWithDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-13 22:50:50 +02:00
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @varr int
|
|
|
|
*/
|
|
|
|
public $var;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingPropertyType',
|
|
|
|
],
|
2020-10-03 00:31:32 +02:00
|
|
|
'promotedPrivateProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-03 00:31:32 +02:00
|
|
|
class A {
|
|
|
|
public function __construct(private int $foo = 5) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo (new A)->foo;',
|
|
|
|
'error_message' => 'InaccessibleProperty',
|
|
|
|
],
|
2020-12-29 17:36:54 +01:00
|
|
|
'overwritePropertyType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-29 17:36:54 +01:00
|
|
|
class A {
|
|
|
|
/** @var array */
|
|
|
|
public string $s = [];
|
|
|
|
}',
|
|
|
|
'error_message' => 'MismatchingDocblockPropertyType',
|
|
|
|
],
|
2021-02-08 04:56:37 +01:00
|
|
|
'possiblyNullOnFunctionCallNotCoalesced' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-08 04:56:37 +01:00
|
|
|
function getC() : ?C {
|
|
|
|
return rand(0, 1) ? new C() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo() : void {
|
|
|
|
echo getC()->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public int $id = 1;
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyNullPropertyFetch',
|
|
|
|
],
|
2021-03-23 04:08:38 +01:00
|
|
|
'noCrashWhenCallingMagicSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-23 04:08:38 +01:00
|
|
|
class A {
|
|
|
|
public function __set(string $s, mixed $value) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->__set("foo");',
|
|
|
|
'error_message' => 'TooFewArguments',
|
|
|
|
],
|
|
|
|
'noCrashWhenCallingMagicGet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-03-23 04:08:38 +01:00
|
|
|
class A {
|
|
|
|
public function __get(string $s) : mixed {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->__get();',
|
|
|
|
'error_message' => 'TooFewArguments',
|
|
|
|
],
|
2021-07-18 01:39:23 +02:00
|
|
|
'staticReadOfNonStaticProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-18 01:39:23 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $prop = 1;
|
|
|
|
}
|
|
|
|
echo A::$prop;
|
|
|
|
',
|
|
|
|
'error_message' => 'UndefinedPropertyFetch',
|
|
|
|
],
|
|
|
|
'staticWriteToNonStaticProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-18 01:39:23 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public $prop = 1;
|
|
|
|
}
|
|
|
|
A::$prop = 42;
|
|
|
|
',
|
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
|
|
|
],
|
|
|
|
'nonStaticReadOfStaticProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-18 01:39:23 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public static $prop = 1;
|
|
|
|
}
|
|
|
|
echo (new A)->prop;
|
|
|
|
',
|
|
|
|
'error_message' => 'UndefinedPropertyFetch',
|
|
|
|
],
|
|
|
|
'nonStaticWriteToStaticProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-18 01:39:23 +02:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
|
|
|
public static $prop = 1;
|
|
|
|
}
|
|
|
|
(new A)->prop = 42;
|
|
|
|
',
|
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2023-07-25 14:59:00 +02:00
|
|
|
'setPropertiesOfSimpleXMLElement1' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$a = new SimpleXMLElement("<person><child role=\"son\"></child></person>");
|
|
|
|
$a->b = "c";
|
|
|
|
',
|
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
|
|
|
],
|
|
|
|
'setPropertiesOfSimpleXMLElement2' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$a = new SimpleXMLElement("<person><child role=\"son\"></child></person>");
|
|
|
|
if (isset($a->b)) {
|
|
|
|
$a->b = "c";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'UndefinedPropertyAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
2016-07-12 06:53:36 +02:00
|
|
|
}
|