1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #2819 - improve negated assertions on templates

This commit is contained in:
Matthew Brown 2020-02-14 11:18:47 -05:00
parent 422bd25f04
commit fb5a0c39cb
5 changed files with 24 additions and 2 deletions

View File

@ -155,6 +155,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$is_strict_equality,
$is_loose_equality,
$existing_var_type,
$template_type_map,
$old_var_type_string,
$key,
$code_location,

View File

@ -47,6 +47,7 @@ class NegatedAssertionReconciler extends Reconciler
* @param string $assertion
* @param bool $is_strict_equality
* @param bool $is_loose_equality
* @param array<string, array<string, array{Type\Union}>> $template_type_map
* @param string $old_var_type_string
* @param string|null $key
* @param CodeLocation|null $code_location
@ -61,6 +62,7 @@ class NegatedAssertionReconciler extends Reconciler
$is_strict_equality,
$is_loose_equality,
Type\Union $existing_var_type,
array $template_type_map,
$old_var_type_string,
$key,
$code_location,
@ -359,7 +361,7 @@ class NegatedAssertionReconciler extends Reconciler
&& ($key !== '$this'
|| !($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer))
) {
$assertion = Type::parseString($assertion);
$assertion = Type::parseString($assertion, null, $template_type_map);
if ($key
&& $code_location

View File

@ -59,6 +59,14 @@ class TTemplateParamClass extends TClassString
return 'class-string<' . $this->param_name . ':' . $this->defining_class . ' as ' . $this->as . '>';
}
/**
* @return string
*/
public function getAssertionString()
{
return 'class-string<' . $this->param_name . '>';
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes

View File

@ -618,7 +618,6 @@ class ClassTemplateTest extends TestCase
*/
public function bar(Foo $object) : void
{
/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($this->getType() !== get_class($object)) {
return;
}

View File

@ -637,6 +637,18 @@ class FunctionClassStringTemplateTest extends TestCase
'$b_or_c' => 'B|C',
]
],
'allowComparisonWithoutCrash' => [
'<?php
/**
* @template T as object
*
* @param T::class $e
* @param T::class $expected
*/
function bar(string $e, string $expected) : void {
if ($e !== $expected) {}
}',
],
];
}