From 21fe32a837a885c1fd09682c9236c7789bf78a73 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 12 Sep 2021 17:12:27 +0200 Subject: [PATCH] Regression tests Closes https://github.com/phpstan/phpstan/issues/5356 Closes https://github.com/phpstan/phpstan/issues/1954 --- .../CallToFunctionParametersRuleTest.php | 28 +++++++++++++++++++ .../PHPStan/Rules/Functions/data/bug-1954.php | 8 ++++++ .../PHPStan/Rules/Functions/data/bug-5356.php | 24 ++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 tests/PHPStan/Rules/Functions/data/bug-1954.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-5356.php diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 419965a1ae0..c74048d3e62 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -858,4 +858,32 @@ public function testArrayMapMultiple(bool $checkExplicitMixed): void ]); } + public function testBug5356(): void + { + if (PHP_VERSION_ID < 70400 && !self::$useStaticReflectionProvider) { + $this->markTestSkipped('Test requires PHP 7.4.'); + } + + $this->analyse([__DIR__ . '/data/bug-5356.php'], [ + [ + 'Parameter #1 $callback of function array_map expects callable(string): mixed, Closure(array): \'a\' given.', + 13, + ], + [ + 'Parameter #1 $callback of function array_map expects callable(string): mixed, Closure(array): \'a\' given.', + 21, + ], + ]); + } + + public function testBug1954(): void + { + $this->analyse([__DIR__ . '/data/bug-1954.php'], [ + [ + 'Parameter #1 $callback of function array_map expects callable(1|stdClass): mixed, Closure(string): string given.', + 7, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-1954.php b/tests/PHPStan/Rules/Functions/data/bug-1954.php new file mode 100644 index 00000000000..38e4354cbcd --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-1954.php @@ -0,0 +1,8 @@ += 7.4 + +namespace Bug5356; + +class Foo +{ + + public function doFoo(): void + { + /** @var array{name: string, collectors: string[]} $array */ + $array = []; + + array_map(static fn(array $_): string => 'a', $array['collectors']); + } + + public function doBar(): void + { + /** @var array{name: string, collectors: string[]} $array */ + $array = []; + + array_map(static function(array $_): string { return 'a'; }, $array['collectors']); + } + +}