Skip to content

Commit

Permalink
Merge pull request #10262 from robchett/negated_class_exist_convert_c…
Browse files Browse the repository at this point in the history
…lass-string_to_string

Negated class_exist check on class-string converts to string instead of mixed
  • Loading branch information
orklah authored Oct 8, 2023
2 parents 67ac81e + c729fcd commit 16be90c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public static function reconcile(
$existing_var_type->removeType('array');
}

if ($assertion instanceof IsNotType && $assertion_type instanceof TClassString) {
$existing_var_type->removeType(TClassString::class);
$existing_var_type->addType(new TString);
}

if (!$is_equality
&& isset($existing_var_atomic_types['int'])
&& $existing_var_type->from_calculation
Expand Down
12 changes: 12 additions & 0 deletions tests/ClassLikeStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,18 @@ class A {}
new \RuntimeException();
}',
],
'convertToStringClassExistsNegated' => [
'code' => '<?php
/** @param class-string $className */
$className = stdClass::class;
if (class_exists($className)) {
throw new \RuntimeException($className);
}',
'assertions' => [
'$className===' => 'string',
],

],
'createNewObjectFromGetClass' => [
'code' => '<?php
/**
Expand Down

0 comments on commit 16be90c

Please sign in to comment.