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

Fix #4466 use better differentiation for class_exists second param

This commit is contained in:
Matt Brown 2020-11-01 13:14:17 -05:00 committed by Daniil Gentili
parent 4982a72da4
commit 2a7feef5f6
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 22 additions and 7 deletions

View File

@ -2215,11 +2215,8 @@ class AssertionFinder
} }
} elseif ($class_exists_check_type = self::hasClassExistsCheck($expr)) { } elseif ($class_exists_check_type = self::hasClassExistsCheck($expr)) {
if ($first_var_name) { if ($first_var_name) {
if ($class_exists_check_type === 2 || $prefix) { $class_string_type = ($class_exists_check_type === 1 ? 'loaded-' : '') . 'class-string';
$if_types[$first_var_name] = [[$prefix . 'class-string']]; $if_types[$first_var_name] = [[$prefix . $class_string_type]];
} else {
$if_types[$first_var_name] = [['=class-string']];
}
} }
} elseif ($class_exists_check_type = self::hasTraitExistsCheck($expr)) { } elseif ($class_exists_check_type = self::hasTraitExistsCheck($expr)) {
if ($first_var_name) { if ($first_var_name) {

View File

@ -83,6 +83,8 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$is_equality = true; $is_equality = true;
} }
$original_assertion = $assertion;
if ($assertion[0] === '>') { if ($assertion[0] === '>') {
$assertion = 'falsy'; $assertion = 'falsy';
$is_negation = true; $is_negation = true;
@ -323,6 +325,10 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
); );
} }
if ($assertion === 'loaded-class-string') {
$assertion = 'class-string';
}
$new_type = Type::parseString($assertion, null, $template_type_map); $new_type = Type::parseString($assertion, null, $template_type_map);
} }
@ -339,6 +345,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
return self::refine( return self::refine(
$statements_analyzer, $statements_analyzer,
$assertion, $assertion,
$original_assertion,
$new_type, $new_type,
$existing_var_type, $existing_var_type,
$template_type_map, $template_type_map,
@ -361,6 +368,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
private static function refine( private static function refine(
StatementsAnalyzer $statements_analyzer, StatementsAnalyzer $statements_analyzer,
string $assertion, string $assertion,
string $original_assertion,
Union $new_type, Union $new_type,
Union $existing_var_type, Union $existing_var_type,
array $template_type_map, array $template_type_map,
@ -538,6 +546,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
&& $code_location && $code_location
&& $new_type->getId() === $existing_var_type->getId() && $new_type->getId() === $existing_var_type->getId()
&& !$is_equality && !$is_equality
&& !($original_assertion === 'loaded-class-string' && $old_var_type_string === 'class-string')
&& (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer) && (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
|| ($key !== '$this' || ($key !== '$this'
&& !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString()))) && !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString())))
@ -546,7 +555,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$existing_var_type, $existing_var_type,
$old_var_type_string, $old_var_type_string,
$key, $key,
$assertion, $original_assertion,
true, true,
$negated, $negated,
$code_location, $code_location,
@ -574,7 +583,7 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$existing_var_type, $existing_var_type,
$old_var_type_string, $old_var_type_string,
$key, $key,
$assertion, $original_assertion,
true, true,
$negated, $negated,
$code_location, $code_location,

View File

@ -412,6 +412,15 @@ class ClassTest extends TestCase
return $class; return $class;
}', }',
], ],
'allowNegatingClassExistsWithoutAutloading' => [
'<?php
function specifyString(string $className): void{
if (!class_exists($className, false)) {
return;
}
new ReflectionClass($className);
}'
],
'classExistsWithFalseArgInside' => [ 'classExistsWithFalseArgInside' => [
'<?php '<?php
function foo(string $s) : void { function foo(string $s) : void {