Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum assertions #7662

Merged
merged 4 commits into from Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Collaborator Author

@orklah orklah Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a check that went a little too far so I had to revert it, but it saw this line as redundant. I couldn't find the reason for this, it seems redundant indeed

$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;
}',
Comment on lines +421 to +429
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it actually test reconciliation with Enum|Enum::Case? It doesn't seem like an individual enum case would ever appear in this test case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I wanted to test was the enum case not leaking to the negated if like in here: #7654 (comment)

The test would be cleaner without null values, (it showed Code|Code::Ok and never on the other var before) but assertions doesn't work if the var appears only in an if

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, this test passes on 5.0a1 (I just tested that). So it doesn't cover changes introduced in this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh :( I'll try to design a better test then!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you could do instead is to abort (exit or throw) in the if body, and then assert the type as it was changed below the if. E.g.

[
            'EnumCaseReconciliation' => [
                'code' => '<?php
                    enum Code: int
                    {
                        case Ok = 0;
                        case Fatal = 1;
                    }

                    function foo(): Code|null|int
                    {
                        return null;
                    }

                    $code = foo() ?? Code::Ok;
                    if (!$code instanceof Code) throw new RuntimeException;
                    ',
                'assertions' => [
                    '$code===' => 'Code',
                ],
                'ignored_issues' => [],
                'php_version' => '8.1',
            ],
]

'assertions' => [
'$code1' => 'Code|null',
'$code2' => 'null',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}

Expand Down