From fd7809737058ccd92852ccfdc2004752fc31fc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 21 Aug 2024 22:46:58 +0200 Subject: [PATCH 01/10] DEBUG local debug --- phpunit.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index b083450096..59e0468c60 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -26,8 +26,7 @@ - tests/PHPStan - tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php + tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php From 5e7b7d0598f261ba61e3ee16c6921ff0cfa29e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 10:26:31 +0200 Subject: [PATCH 02/10] copy test --- .../Rules/Properties/data/bug-3777-static.php | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/bug-3777-static.php diff --git a/tests/PHPStan/Rules/Properties/data/bug-3777-static.php b/tests/PHPStan/Rules/Properties/data/bug-3777-static.php new file mode 100644 index 0000000000..6ac99af08b --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-3777-static.php @@ -0,0 +1,172 @@ + + */ + public $dates; + + public function __construct() + { + $this->dates = new \SplObjectStorage(); + assertType('SplObjectStorage', $this->dates); + } +} + +/** @template T of object */ +class Foo +{ + + public function __construct() + { + + } + +} + +/** @template T of object */ +class Fooo +{ + +} + +class Bar +{ + + /** @var Foo<\stdClass> */ + private $foo; + + /** @var Fooo<\stdClass> */ + private $fooo; + + public function __construct() + { + $this->foo = new Foo(); + assertType('Bug3777\Foo', $this->foo); + + $this->fooo = new Fooo(); + assertType('Bug3777\Fooo', $this->fooo); + } + + public function doBar() + { + $this->foo = new Fooo(); + assertType('Bug3777\Fooo', $this->foo); + } + +} + +/** + * @template T of object + * @template U of object + */ +class Lorem +{ + + /** + * @param T $t + * @param U $u + */ + public function __construct($t, $u) + { + + } + +} + +class Ipsum +{ + + /** @var Lorem<\stdClass, \Exception> */ + private $lorem; + + /** @var Lorem<\stdClass, \Exception> */ + private $ipsum; + + public function __construct() + { + $this->lorem = new Lorem(new \stdClass, new \Exception()); + assertType('Bug3777\Lorem', $this->lorem); + $this->ipsum = new Lorem(new \Exception(), new \stdClass); + assertType('Bug3777\Lorem', $this->ipsum); + } + +} + +/** + * @template T of object + * @template U of object + */ +class Lorem2 +{ + + /** + * @param T $t + */ + public function __construct($t) + { + + } + +} + +class Ipsum2 +{ + + /** @var Lorem2<\stdClass, \Exception> */ + private $lorem2; + + /** @var Lorem2<\stdClass, \Exception> */ + private $ipsum2; + + public function __construct() + { + $this->lorem2 = new Lorem2(new \stdClass); + assertType('Bug3777\Lorem2', $this->lorem2); + $this->ipsum2 = new Lorem2(new \Exception()); + assertType('Bug3777\Lorem2', $this->ipsum2); + } + +} + +/** + * @template T of object + * @template U of object + */ +class Lorem3 +{ + + /** + * @param T $t + * @param U $u + */ + public function __construct($t, $u) + { + + } + +} + +class Ipsum3 +{ + + /** @var Lorem3<\stdClass, \Exception> */ + private $lorem3; + + /** @var Lorem3<\stdClass, \Exception> */ + private $ipsum3; + + public function __construct() + { + $this->lorem3 = new Lorem3(new \stdClass, new \Exception()); + assertType('Bug3777\Lorem3', $this->lorem3); + $this->ipsum3 = new Lorem3(new \Exception(), new \stdClass()); + assertType('Bug3777\Lorem3', $this->ipsum3); + } + +} From 92342eb92c17963dc39af425ad064980a48e3c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 10:28:05 +0200 Subject: [PATCH 03/10] integrate test --- .../TypesAssignedToPropertiesRuleTest.php | 23 +++++++++++++++++++ .../Rules/Properties/data/bug-3777-static.php | 20 ++++++++-------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 09f34d1d40..97f6f6f8a4 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -208,6 +208,29 @@ public function testBug3777(): void 168, ], ]); + + $this->analyse([__DIR__ . '/data/bug-3777-static.php'], [ + [ + 'Property Bug3777Static\Bar::$foo (Bug3777Static\Foo) does not accept Bug3777Static\Fooo.', + 58, + ], + [ + 'Property Bug3777Static\Ipsum::$ipsum (Bug3777Static\Lorem) does not accept Bug3777Static\Lorem.', + 95, + ], + [ + 'Property Bug3777Static\Ipsum2::$lorem2 (Bug3777Static\Lorem2) does not accept Bug3777Static\Lorem2.', + 129, + ], + [ + 'Property Bug3777Static\Ipsum2::$ipsum2 (Bug3777Static\Lorem2) does not accept Bug3777Static\Lorem2.', + 131, + ], + [ + 'Property Bug3777Static\Ipsum3::$ipsum3 (Bug3777Static\Lorem3) does not accept Bug3777Static\Lorem3.', + 168, + ], + ]); } public function testAppendendArrayKey(): void diff --git a/tests/PHPStan/Rules/Properties/data/bug-3777-static.php b/tests/PHPStan/Rules/Properties/data/bug-3777-static.php index 6ac99af08b..676184a7da 100644 --- a/tests/PHPStan/Rules/Properties/data/bug-3777-static.php +++ b/tests/PHPStan/Rules/Properties/data/bug-3777-static.php @@ -1,6 +1,6 @@ foo = new Foo(); - assertType('Bug3777\Foo', $this->foo); + assertType('Bug3777Static\Foo', $this->foo); $this->fooo = new Fooo(); - assertType('Bug3777\Fooo', $this->fooo); + assertType('Bug3777Static\Fooo', $this->fooo); } public function doBar() { $this->foo = new Fooo(); - assertType('Bug3777\Fooo', $this->foo); + assertType('Bug3777Static\Fooo', $this->foo); } } @@ -91,9 +91,9 @@ class Ipsum public function __construct() { $this->lorem = new Lorem(new \stdClass, new \Exception()); - assertType('Bug3777\Lorem', $this->lorem); + assertType('Bug3777Static\Lorem', $this->lorem); $this->ipsum = new Lorem(new \Exception(), new \stdClass); - assertType('Bug3777\Lorem', $this->ipsum); + assertType('Bug3777Static\Lorem', $this->ipsum); } } @@ -127,9 +127,9 @@ class Ipsum2 public function __construct() { $this->lorem2 = new Lorem2(new \stdClass); - assertType('Bug3777\Lorem2', $this->lorem2); + assertType('Bug3777Static\Lorem2', $this->lorem2); $this->ipsum2 = new Lorem2(new \Exception()); - assertType('Bug3777\Lorem2', $this->ipsum2); + assertType('Bug3777Static\Lorem2', $this->ipsum2); } } @@ -164,9 +164,9 @@ class Ipsum3 public function __construct() { $this->lorem3 = new Lorem3(new \stdClass, new \Exception()); - assertType('Bug3777\Lorem3', $this->lorem3); + assertType('Bug3777Static\Lorem3', $this->lorem3); $this->ipsum3 = new Lorem3(new \Exception(), new \stdClass()); - assertType('Bug3777\Lorem3', $this->ipsum3); + assertType('Bug3777Static\Lorem3', $this->ipsum3); } } From 0cf42395d8df84a13a567d232949d28002e97170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 10:35:58 +0200 Subject: [PATCH 04/10] adjust copied test for static testing - issue 5551 --- .../TypesAssignedToPropertiesRuleTest.php | 10 ++-- .../Rules/Properties/data/bug-3777-static.php | 58 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 97f6f6f8a4..94231200f7 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -211,23 +211,23 @@ public function testBug3777(): void $this->analyse([__DIR__ . '/data/bug-3777-static.php'], [ [ - 'Property Bug3777Static\Bar::$foo (Bug3777Static\Foo) does not accept Bug3777Static\Fooo.', + 'Static property Bug3777Static\Bar::$foo (Bug3777Static\Foo) does not accept Bug3777Static\Fooo.', 58, ], [ - 'Property Bug3777Static\Ipsum::$ipsum (Bug3777Static\Lorem) does not accept Bug3777Static\Lorem.', + 'Static property Bug3777Static\Ipsum::$ipsum (Bug3777Static\Lorem) does not accept Bug3777Static\Lorem.', 95, ], [ - 'Property Bug3777Static\Ipsum2::$lorem2 (Bug3777Static\Lorem2) does not accept Bug3777Static\Lorem2.', + 'Static property Bug3777Static\Ipsum2::$lorem2 (Bug3777Static\Lorem2) does not accept Bug3777Static\Lorem2.', 129, ], [ - 'Property Bug3777Static\Ipsum2::$ipsum2 (Bug3777Static\Lorem2) does not accept Bug3777Static\Lorem2.', + 'Static property Bug3777Static\Ipsum2::$ipsum2 (Bug3777Static\Lorem2) does not accept Bug3777Static\Lorem2.', 131, ], [ - 'Property Bug3777Static\Ipsum3::$ipsum3 (Bug3777Static\Lorem3) does not accept Bug3777Static\Lorem3.', + 'Static property Bug3777Static\Ipsum3::$ipsum3 (Bug3777Static\Lorem3) does not accept Bug3777Static\Lorem3.', 168, ], ]); diff --git a/tests/PHPStan/Rules/Properties/data/bug-3777-static.php b/tests/PHPStan/Rules/Properties/data/bug-3777-static.php index 676184a7da..0cc9c7b930 100644 --- a/tests/PHPStan/Rules/Properties/data/bug-3777-static.php +++ b/tests/PHPStan/Rules/Properties/data/bug-3777-static.php @@ -9,12 +9,12 @@ class HelloWorld /** * @var \SplObjectStorage<\DateTimeImmutable, null> */ - public $dates; + public static $dates; public function __construct() { - $this->dates = new \SplObjectStorage(); - assertType('SplObjectStorage', $this->dates); + static::$dates = new \SplObjectStorage(); + assertType('SplObjectStorage', static::$dates); } } @@ -39,24 +39,24 @@ class Bar { /** @var Foo<\stdClass> */ - private $foo; + private static $foo; /** @var Fooo<\stdClass> */ - private $fooo; + private static $fooo; public function __construct() { - $this->foo = new Foo(); - assertType('Bug3777Static\Foo', $this->foo); + static::$foo = new Foo(); + assertType('Bug3777Static\Foo', static::$foo); - $this->fooo = new Fooo(); - assertType('Bug3777Static\Fooo', $this->fooo); + static::$fooo = new Fooo(); + assertType('Bug3777Static\Fooo', static::$fooo); } public function doBar() { - $this->foo = new Fooo(); - assertType('Bug3777Static\Fooo', $this->foo); + static::$foo = new Fooo(); + assertType('Bug3777Static\Fooo', static::$foo); } } @@ -83,17 +83,17 @@ class Ipsum { /** @var Lorem<\stdClass, \Exception> */ - private $lorem; + private static $lorem; /** @var Lorem<\stdClass, \Exception> */ - private $ipsum; + private static $ipsum; public function __construct() { - $this->lorem = new Lorem(new \stdClass, new \Exception()); - assertType('Bug3777Static\Lorem', $this->lorem); - $this->ipsum = new Lorem(new \Exception(), new \stdClass); - assertType('Bug3777Static\Lorem', $this->ipsum); + static::$lorem = new Lorem(new \stdClass, new \Exception()); + assertType('Bug3777Static\Lorem', static::$lorem); + static::$ipsum = new Lorem(new \Exception(), new \stdClass); + assertType('Bug3777Static\Lorem', static::$ipsum); } } @@ -119,17 +119,17 @@ class Ipsum2 { /** @var Lorem2<\stdClass, \Exception> */ - private $lorem2; + private static $lorem2; /** @var Lorem2<\stdClass, \Exception> */ - private $ipsum2; + private static $ipsum2; public function __construct() { - $this->lorem2 = new Lorem2(new \stdClass); - assertType('Bug3777Static\Lorem2', $this->lorem2); - $this->ipsum2 = new Lorem2(new \Exception()); - assertType('Bug3777Static\Lorem2', $this->ipsum2); + static::$lorem2 = new Lorem2(new \stdClass); + assertType('Bug3777Static\Lorem2', static::$lorem2); + static::$ipsum2 = new Lorem2(new \Exception()); + assertType('Bug3777Static\Lorem2', static::$ipsum2); } } @@ -156,17 +156,17 @@ class Ipsum3 { /** @var Lorem3<\stdClass, \Exception> */ - private $lorem3; + private static $lorem3; /** @var Lorem3<\stdClass, \Exception> */ - private $ipsum3; + private static $ipsum3; public function __construct() { - $this->lorem3 = new Lorem3(new \stdClass, new \Exception()); - assertType('Bug3777Static\Lorem3', $this->lorem3); - $this->ipsum3 = new Lorem3(new \Exception(), new \stdClass()); - assertType('Bug3777Static\Lorem3', $this->ipsum3); + static::$lorem3 = new Lorem3(new \stdClass, new \Exception()); + assertType('Bug3777Static\Lorem3', static::$lorem3); + static::$ipsum3 = new Lorem3(new \Exception(), new \stdClass()); + assertType('Bug3777Static\Lorem3', static::$ipsum3); } } From 4a1ff4e59c45a13f814239c135015848ce0e194d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 10:43:05 +0200 Subject: [PATCH 05/10] fix --- src/Parser/NewAssignedToPropertyVisitor.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Parser/NewAssignedToPropertyVisitor.php b/src/Parser/NewAssignedToPropertyVisitor.php index 05df87b423..209e72730d 100644 --- a/src/Parser/NewAssignedToPropertyVisitor.php +++ b/src/Parser/NewAssignedToPropertyVisitor.php @@ -13,7 +13,10 @@ final class NewAssignedToPropertyVisitor extends NodeVisitorAbstract public function enterNode(Node $node): ?Node { if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) { - if ($node->var instanceof Node\Expr\PropertyFetch && $node->expr instanceof Node\Expr\New_) { + if ( + ($node->var instanceof Node\Expr\PropertyFetch || $node->var instanceof Node\Expr\StaticPropertyFetch) + && $node->expr instanceof Node\Expr\New_ + ) { $node->expr->setAttribute(self::ATTRIBUTE_NAME, $node->var); } } From a4c1d8c914c2609eba5ace21d470f0d4188d0a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 10:43:36 +0200 Subject: [PATCH 06/10] Revert "DEBUG local debug" --- phpunit.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 59e0468c60..b083450096 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -26,7 +26,8 @@ - tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php + tests/PHPStan + tests/PHPStan/Reflection/ReflectionProviderGoldenTest.php From ea8767227a5038ce3d7083455100042c0fab2ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 11:00:00 +0200 Subject: [PATCH 07/10] add 10686 test --- .../TypesAssignedToPropertiesRuleTest.php | 5 +++ .../Rules/Properties/data/bug-10686.php | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/bug-10686.php diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 94231200f7..19b6d3b262 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -637,6 +637,11 @@ public function testGenericsInCallableInConstructor(): void $this->analyse([__DIR__ . '/data/generics-in-callable-in-constructor.php'], []); } + public function testBug10686(): void + { + $this->analyse([__DIR__ . '/data/bug-10686.php'], []); + } + public function testBug11275(): void { if (PHP_VERSION_ID < 80000) { diff --git a/tests/PHPStan/Rules/Properties/data/bug-10686.php b/tests/PHPStan/Rules/Properties/data/bug-10686.php new file mode 100644 index 0000000000..4988a61053 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-10686.php @@ -0,0 +1,33 @@ + + */ +class WeakAnalysingMap +{ + /** @var list */ + public array $values = []; +} + +class Reference +{ + /** @var WeakAnalysingMap */ + private static WeakAnalysingMap $analysingTheirModelMap; + + public function createAnalysingTheirModel(): Model + { + if ((self::$analysingTheirModelMap ?? null) === null) { + self::$analysingTheirModelMap = new WeakAnalysingMap(); + } + + $theirModel = new Model(); + + self::$analysingTheirModelMap->values[] = $theirModel; + + return end(self::$analysingTheirModelMap->values); + } +} From 6e50e889dcd3d1a38ab7c863dabd873355b226b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 11:00:35 +0200 Subject: [PATCH 08/10] add 5551 test --- .../TypesAssignedToPropertiesRuleTest.php | 5 +++++ .../Rules/Properties/data/bug-5551.php | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/bug-5551.php diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 19b6d3b262..2bc5891593 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -271,6 +271,11 @@ public function testAppendendArrayKey(): void ]); } + public function testBug5551(): void + { + $this->analyse([__DIR__ . '/data/bug-5551.php'], []); + } + public function testBug5372Two(): void { $this->analyse([__DIR__ . '/../Arrays/data/bug-5372_2.php'], []); diff --git a/tests/PHPStan/Rules/Properties/data/bug-5551.php b/tests/PHPStan/Rules/Properties/data/bug-5551.php new file mode 100644 index 0000000000..ed80790c13 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-5551.php @@ -0,0 +1,22 @@ + + */ + protected static WeakMap $bug; + + /** + * @var WeakMap<\stdClass, \stdClass> + */ + protected WeakMap $ok; + + public function bug(): void + { + $this->ok = new WeakMap(); + static::$bug = new WeakMap(); + } +} From b80fdd5e63ae40670ad3599bcbc2c42b496868e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 11:00:42 +0200 Subject: [PATCH 09/10] Revert "add 5551 test" - does not emit an error --- .../TypesAssignedToPropertiesRuleTest.php | 5 ----- .../Rules/Properties/data/bug-5551.php | 22 ------------------- 2 files changed, 27 deletions(-) delete mode 100644 tests/PHPStan/Rules/Properties/data/bug-5551.php diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 2bc5891593..19b6d3b262 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -271,11 +271,6 @@ public function testAppendendArrayKey(): void ]); } - public function testBug5551(): void - { - $this->analyse([__DIR__ . '/data/bug-5551.php'], []); - } - public function testBug5372Two(): void { $this->analyse([__DIR__ . '/../Arrays/data/bug-5372_2.php'], []); diff --git a/tests/PHPStan/Rules/Properties/data/bug-5551.php b/tests/PHPStan/Rules/Properties/data/bug-5551.php deleted file mode 100644 index ed80790c13..0000000000 --- a/tests/PHPStan/Rules/Properties/data/bug-5551.php +++ /dev/null @@ -1,22 +0,0 @@ - - */ - protected static WeakMap $bug; - - /** - * @var WeakMap<\stdClass, \stdClass> - */ - protected WeakMap $ok; - - public function bug(): void - { - $this->ok = new WeakMap(); - static::$bug = new WeakMap(); - } -} From 19c21567aeccbdbc4ae661483a31c92ff7836ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 27 Aug 2024 11:05:42 +0200 Subject: [PATCH 10/10] fix test lint --- tests/PHPStan/Rules/Properties/data/bug-10686.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Properties/data/bug-10686.php b/tests/PHPStan/Rules/Properties/data/bug-10686.php index 4988a61053..0d6922d5da 100644 --- a/tests/PHPStan/Rules/Properties/data/bug-10686.php +++ b/tests/PHPStan/Rules/Properties/data/bug-10686.php @@ -1,4 +1,4 @@ -= 7.4 namespace Bug10686;