From eb07c6a48ea3fef717ba0a09f31c7b1c50aab079 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 27 Jul 2025 20:40:08 +0200 Subject: [PATCH 1/2] Add non regression test --- .../Rules/Methods/CallMethodsRuleTest.php | 19 +++++++++ tests/PHPStan/Rules/Methods/data/bug-3589.php | 39 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-3589.php diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index d423d80dbb..c90d5315e3 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3608,6 +3608,25 @@ public function testBug9141(): void $this->analyse([__DIR__ . '/data/bug-9141.php'], []); } + public function testBug3589(): void + { + $this->checkThisOnly = false; + $this->checkNullables = true; + $this->checkUnionTypes = true; + $this->checkExplicitMixed = true; + + $this->analyse([__DIR__ . '/data/bug-3589.php'], [ + [ + 'Parameter #1 $fooId of method FooRepository::load() expects Id, Id given.', + 33, + ], + [ + 'Parameter #1 $fooId of method FooRepository::load() expects Id, Id given.', + 39, + ], + ]); + } + public function testBug3396(): void { $this->checkThisOnly = false; diff --git a/tests/PHPStan/Rules/Methods/data/bug-3589.php b/tests/PHPStan/Rules/Methods/data/bug-3589.php new file mode 100644 index 0000000000..59b1ba8562 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-3589.php @@ -0,0 +1,39 @@ + $fooId + */ + public function load(Id $fooId): Foo + { + // ... + return new Foo; + } +} + +$fooRepository = new FooRepository; + +// Expected behavior: no error +/** @var Id */ +$fooId = new Id; +$fooRepository->load($fooId); + +// Expected behavior: error on line 33 +/** @var Id */ +$barId = new Id; +$fooRepository->load($barId); + +// Expected behavior: errors +// - line 38 - Template Tpl is not specified +// - line 39 - Parameter #1 fooId of method FooRepository::load() expects Id, nonspecified Id given. +$unknownId = new Id; +$fooRepository->load($unknownId); From 81ff25a2bb8387e08cf901a6d13f65c67317f2d8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 27 Jul 2025 20:48:51 +0200 Subject: [PATCH 2/2] Fix --- tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php | 8 ++++---- tests/PHPStan/Rules/Methods/data/bug-3589.php | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index c90d5315e3..9e5018953e 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3617,12 +3617,12 @@ public function testBug3589(): void $this->analyse([__DIR__ . '/data/bug-3589.php'], [ [ - 'Parameter #1 $fooId of method FooRepository::load() expects Id, Id given.', - 33, + 'Parameter #1 $fooId of method Bug3589\FooRepository::load() expects Bug3589\Id, Bug3589\Id given.', + 35, ], [ - 'Parameter #1 $fooId of method FooRepository::load() expects Id, Id given.', - 39, + 'Parameter #1 $fooId of method Bug3589\FooRepository::load() expects Bug3589\Id, Bug3589\Id given.', + 41, ], ]); } diff --git a/tests/PHPStan/Rules/Methods/data/bug-3589.php b/tests/PHPStan/Rules/Methods/data/bug-3589.php index 59b1ba8562..f82e1bbcdd 100644 --- a/tests/PHPStan/Rules/Methods/data/bug-3589.php +++ b/tests/PHPStan/Rules/Methods/data/bug-3589.php @@ -1,5 +1,7 @@