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

Enum assertions #7662

merged 4 commits into from Feb 13, 2022

Conversation

orklah
Copy link
Collaborator

@orklah orklah commented Feb 13, 2022

This will fix #7654

It doesn't adress the collapsing issue, but it makes sure the assertions are correctly reconciled

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Feb 13, 2022
@@ -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

@orklah orklah merged commit 56504b8 into vimeo:master Feb 13, 2022
Comment on lines +421 to +429
$code = foo();
$code1 = null;
$code2 = null;
if($code instanceof Code){
$code1 = $code;
}
if(!$code instanceof Code){
$code2 = $code;
}',
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',
            ],
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix The PR will be included in 'Fixes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants