1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix templated assertion handling for static methods

This commit is contained in:
Matthew Brown 2019-11-04 05:31:45 -05:00
parent 8f421dc0bb
commit 5faebe2674
2 changed files with 38 additions and 6 deletions

View File

@ -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
);

View File

@ -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' => [
'<?php
namespace Bar;
class Assertion {
/**
* Asserts that two variables are the same.
*
* @template T
* @param T $expected
* @param mixed $actual
* @psalm-assert =T $actual
*/
public static function assertSame($expected, $actual) : void {}
}
$a = rand(0, 1) ? "goodbye" : "hello";
$b = rand(0, 1) ? "hello" : "goodbye";
Assertion::assertSame($a, $b);',
],
'allowCanBeNotSameAfterAssertion' => [
'<?php
namespace Bar;