From 5faebe267483f73466ba071b763afe7586228d5d Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Mon, 4 Nov 2019 05:31:45 -0500 Subject: [PATCH] Fix templated assertion handling for static methods --- .../Expression/Call/StaticCallAnalyzer.php | 12 ++++--- tests/Template/FunctionTemplateAssertTest.php | 32 ++++++++++++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php index e5fc9f588..7c983bbe3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php @@ -907,13 +907,15 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ } } + $generic_params = $template_result->generic_params; + if ($method_storage->assertions) { self::applyAssertionsToContext( $stmt->name, null, $method_storage->assertions, $stmt->args, - $found_generic_params ?: [], + $generic_params, $context, $statements_analyzer ); @@ -921,8 +923,8 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ if ($method_storage->if_true_assertions) { $stmt->ifTrueAssertions = array_map( - function (Assertion $assertion) use ($found_generic_params) : Assertion { - return $assertion->getUntemplatedCopy($found_generic_params ?: []); + function (Assertion $assertion) use ($generic_params) : Assertion { + return $assertion->getUntemplatedCopy($generic_params); }, $method_storage->if_true_assertions ); @@ -930,8 +932,8 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ if ($method_storage->if_false_assertions) { $stmt->ifFalseAssertions = array_map( - function (Assertion $assertion) use ($found_generic_params) : Assertion { - return $assertion->getUntemplatedCopy($found_generic_params ?: []); + function (Assertion $assertion) use ($generic_params) : Assertion { + return $assertion->getUntemplatedCopy($generic_params); }, $method_storage->if_false_assertions ); diff --git a/tests/Template/FunctionTemplateAssertTest.php b/tests/Template/FunctionTemplateAssertTest.php index c70905407..abf468f4d 100644 --- a/tests/Template/FunctionTemplateAssertTest.php +++ b/tests/Template/FunctionTemplateAssertTest.php @@ -253,10 +253,40 @@ class FunctionTemplateAssertTest extends TestCase $d = rand(0, 1) ? 4 : null; assertSame(null, $d); - function foo(string $a, string $b) : void { + function assertStringsAreSame(string $a, string $b) : void { + assertSame($a, $b); + } + + /** @param mixed $a */ + function assertMaybeStringsAreSame($a, string $b) : void { + assertSame($a, $b); + } + + /** @param mixed $b */ + function alsoAssertMaybeStringsAreSame(string $a, $b) : void { assertSame($a, $b); }', ], + 'allowCanBeSameAfterStaticMethodAssertion' => [ + ' [ '