mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Fix templated assertion handling for static methods
This commit is contained in:
parent
8f421dc0bb
commit
5faebe2674
@ -907,13 +907,15 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$generic_params = $template_result->generic_params;
|
||||||
|
|
||||||
if ($method_storage->assertions) {
|
if ($method_storage->assertions) {
|
||||||
self::applyAssertionsToContext(
|
self::applyAssertionsToContext(
|
||||||
$stmt->name,
|
$stmt->name,
|
||||||
null,
|
null,
|
||||||
$method_storage->assertions,
|
$method_storage->assertions,
|
||||||
$stmt->args,
|
$stmt->args,
|
||||||
$found_generic_params ?: [],
|
$generic_params,
|
||||||
$context,
|
$context,
|
||||||
$statements_analyzer
|
$statements_analyzer
|
||||||
);
|
);
|
||||||
@ -921,8 +923,8 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
|||||||
|
|
||||||
if ($method_storage->if_true_assertions) {
|
if ($method_storage->if_true_assertions) {
|
||||||
$stmt->ifTrueAssertions = array_map(
|
$stmt->ifTrueAssertions = array_map(
|
||||||
function (Assertion $assertion) use ($found_generic_params) : Assertion {
|
function (Assertion $assertion) use ($generic_params) : Assertion {
|
||||||
return $assertion->getUntemplatedCopy($found_generic_params ?: []);
|
return $assertion->getUntemplatedCopy($generic_params);
|
||||||
},
|
},
|
||||||
$method_storage->if_true_assertions
|
$method_storage->if_true_assertions
|
||||||
);
|
);
|
||||||
@ -930,8 +932,8 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
|||||||
|
|
||||||
if ($method_storage->if_false_assertions) {
|
if ($method_storage->if_false_assertions) {
|
||||||
$stmt->ifFalseAssertions = array_map(
|
$stmt->ifFalseAssertions = array_map(
|
||||||
function (Assertion $assertion) use ($found_generic_params) : Assertion {
|
function (Assertion $assertion) use ($generic_params) : Assertion {
|
||||||
return $assertion->getUntemplatedCopy($found_generic_params ?: []);
|
return $assertion->getUntemplatedCopy($generic_params);
|
||||||
},
|
},
|
||||||
$method_storage->if_false_assertions
|
$method_storage->if_false_assertions
|
||||||
);
|
);
|
||||||
|
@ -253,10 +253,40 @@ class FunctionTemplateAssertTest extends TestCase
|
|||||||
$d = rand(0, 1) ? 4 : null;
|
$d = rand(0, 1) ? 4 : null;
|
||||||
assertSame(null, $d);
|
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);
|
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' => [
|
'allowCanBeNotSameAfterAssertion' => [
|
||||||
'<?php
|
'<?php
|
||||||
namespace Bar;
|
namespace Bar;
|
||||||
|
Loading…
Reference in New Issue
Block a user