Skip to content

Commit

Permalink
Merge pull request #7662 from orklah/enumAssertions
Browse files Browse the repository at this point in the history
Enum assertions
  • Loading branch information
orklah committed Feb 13, 2022
2 parents 59d3d2a + 093d9b2 commit 56504b8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
4 changes: 0 additions & 4 deletions src/Psalm/Internal/Provider/StatementsProvider.php
Expand Up @@ -155,10 +155,6 @@ public function getStatementsForFile(

$existing_statements = $this->parser_cache_provider->loadExistingStatementsFromCache($file_path);

if ($existing_statements && !$existing_statements[0] instanceof PhpParser\Node\Stmt) {
$existing_statements = null;
}

$existing_file_contents = $this->parser_cache_provider->loadExistingFileContentsFromCache($file_path);

// this happens after editing temporary file
Expand Down
13 changes: 11 additions & 2 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Expand Up @@ -16,7 +16,6 @@
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEmptyMixed;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
Expand Down Expand Up @@ -162,6 +161,14 @@ public static function reconcile(
return $existing_var_type;
}

if (!$is_equality && $assertion_type instanceof TNamedObject) {
foreach ($existing_var_type->getAtomicTypes() as $key => $type) {
if ($type instanceof TEnumCase && $type->value === $assertion_type->value) {
$existing_var_type->removeType($key);
}
}
}

$codebase = $statements_analyzer->getCodebase();

if ($assertion_type instanceof TNamedObject
Expand Down Expand Up @@ -291,7 +298,9 @@ public static function reconcile(

$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;

return new Union([new TEmptyMixed]);
return $existing_var_type->from_docblock
? Type::getMixed()
: Type::getNever();
}

return $existing_var_type;
Expand Down
29 changes: 29 additions & 0 deletions tests/EnumTest.php
Expand Up @@ -405,6 +405,35 @@ function foo(): int|Code|null
'ignored_issues' => [],
'php_version' => '8.1',
],
'EnumCaseReconciliation' => [
'code' => '<?php
enum Code: int
{
case Ok = 0;
case Fatal = 1;
}
function foo(): Code|null
{
return null;
}
$code = foo();
$code1 = null;
$code2 = null;
if($code instanceof Code){
$code1 = $code;
}
if(!$code instanceof Code){
$code2 = $code;
}',
'assertions' => [
'$code1' => 'Code|null',
'$code2' => 'null',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}

Expand Down

0 comments on commit 56504b8

Please sign in to comment.