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

Prevent some inheritance issues

This commit is contained in:
Brown 2020-03-31 11:28:36 -04:00
parent 687ce87995
commit ddc8c50d2a
2 changed files with 58 additions and 0 deletions

View File

@ -783,6 +783,31 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$any_scalar_type_match_found = false;
if ($code_location
&& $key
&& !$is_equality
&& $new_type_part instanceof TNamedObject
&& !$new_type_has_interface
&& (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
|| ($key !== '$this'
&& !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString())))
&& TypeAnalyzer::isContainedBy(
$codebase,
$existing_var_type,
$new_type
)
) {
self::triggerIssueForImpossible(
$existing_var_type,
$old_var_type_string,
$key,
$assertion,
true,
$code_location,
$suppressed_issues
);
}
$new_type = self::filterTypeWithAnother(
$codebase,
$existing_var_type,

View File

@ -1307,6 +1307,39 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
if (!is_int(returnsInt())) {}',
'error_message' => 'TypeDoesNotContainType',
],
'classAlwaysParent' => [
'<?php
class AParent {}
class A extends AParent {
public static function load() : A {
return new A();
}
}
$a = A::load();
if ($a instanceof AParent) {}',
'error_message' => 'RedundantCondition',
],
'staticClassIsAlwaysNull' => [
'<?php
class A {
/**
* @return ?static
*/
public static function load() {
return rand(0, 1)
? null
: new static();
}
}
$a = A::load();
if ($a && $a instanceof A) {}',
'error_message' => 'RedundantConditionGivenDocblockType',
],
];
}
}