Skip to content

Commit

Permalink
Fix vimeo#4466 use better differentiation for class_exists second param
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug authored and danog committed Jan 29, 2021
1 parent 4982a72 commit 2a7feef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2215,11 +2215,8 @@ public static function processFunctionCall(
}
} elseif ($class_exists_check_type = self::hasClassExistsCheck($expr)) {
if ($first_var_name) {
if ($class_exists_check_type === 2 || $prefix) {
$if_types[$first_var_name] = [[$prefix . 'class-string']];
} else {
$if_types[$first_var_name] = [['=class-string']];
}
$class_string_type = ($class_exists_check_type === 1 ? 'loaded-' : '') . 'class-string';
$if_types[$first_var_name] = [[$prefix . $class_string_type]];
}
} elseif ($class_exists_check_type = self::hasTraitExistsCheck($expr)) {
if ($first_var_name) {
Expand Down
13 changes: 11 additions & 2 deletions src/Psalm/Internal/Type/AssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public static function reconcile(
$is_equality = true;
}

$original_assertion = $assertion;

if ($assertion[0] === '>') {
$assertion = 'falsy';
$is_negation = true;
Expand Down Expand Up @@ -323,6 +325,10 @@ public static function reconcile(
);
}

if ($assertion === 'loaded-class-string') {
$assertion = 'class-string';
}

$new_type = Type::parseString($assertion, null, $template_type_map);
}

Expand All @@ -339,6 +345,7 @@ public static function reconcile(
return self::refine(
$statements_analyzer,
$assertion,
$original_assertion,
$new_type,
$existing_var_type,
$template_type_map,
Expand All @@ -361,6 +368,7 @@ public static function reconcile(
private static function refine(
StatementsAnalyzer $statements_analyzer,
string $assertion,
string $original_assertion,
Union $new_type,
Union $existing_var_type,
array $template_type_map,
Expand Down Expand Up @@ -538,6 +546,7 @@ private static function refine(
&& $code_location
&& $new_type->getId() === $existing_var_type->getId()
&& !$is_equality
&& !($original_assertion === 'loaded-class-string' && $old_var_type_string === 'class-string')
&& (!($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
|| ($key !== '$this'
&& !($existing_var_type->hasLiteralClassString() && $new_type->hasLiteralClassString())))
Expand All @@ -546,7 +555,7 @@ private static function refine(
$existing_var_type,
$old_var_type_string,
$key,
$assertion,
$original_assertion,
true,
$negated,
$code_location,
Expand Down Expand Up @@ -574,7 +583,7 @@ private static function refine(
$existing_var_type,
$old_var_type_string,
$key,
$assertion,
$original_assertion,
true,
$negated,
$code_location,
Expand Down
9 changes: 9 additions & 0 deletions tests/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ function autoload(string $class) : string {
return $class;
}',
],
'allowNegatingClassExistsWithoutAutloading' => [
'<?php
function specifyString(string $className): void{
if (!class_exists($className, false)) {
return;
}
new ReflectionClass($className);
}'
],
'classExistsWithFalseArgInside' => [
'<?php
function foo(string $s) : void {
Expand Down

0 comments on commit 2a7feef

Please sign in to comment.