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

Flaw in the co-dependant variables types #7249

Closed
orklah opened this issue Dec 30, 2021 · 2 comments · May be fixed by #7688
Closed

Flaw in the co-dependant variables types #7249

orklah opened this issue Dec 30, 2021 · 2 comments · May be fixed by #7688
Labels

Comments

@orklah
Copy link
Collaborator

orklah commented Dec 30, 2021

In the example here: https://psalm.dev/r/9bc618221f

Psalm doesn't detect that $type_context can't be null because we're in a scope where $completion_data is null and we already stated that both can't be null.

Psalm manages to make it work if the conditional is outside the try/catch: https://psalm.dev/r/ab2b600564.

This is pretty low priority because it's an edge case of a complex module and the code, while correct, is a little confusing anyway.

Found by: #7247

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/9bc618221f
<?php
function scope(){
    try {
        $completion_data = getThingOrNullOrThrow();
    } catch (Exception $e) {
        return new stdClass();
    }

    try {
        $type_context = getThingOrNullOrThrow();
        if (!$completion_data && !$type_context) {
        	return new stdClass(); //HERE!
    	}
    } catch (Exception $e) {
        return new stdClass();
    }

    if ($completion_data) {
        // do something
    } else {
        /** @psalm-trace $type_context */
        // $type_context can't be null here or else we would have entered the conditional "HERE!" because both variables are false
    }
}

/** 
 * @return string|null
 * @throws Exception
 */
function getThingOrNullOrThrow(){
    
}
Psalm output (using commit 7b18673):

INFO: Trace - 22:0 - $type_context: null|string

INFO: MissingReturnType - 2:10 - Method scope does not have a return type, expecting null|stdClass
https://psalm.dev/r/ab2b600564
<?php
function scope(){
    try {
        $completion_data = getThingOrNullOrThrow();
    } catch (Exception $e) {
        return new stdClass();
    }

    try {
        $type_context = getThingOrNullOrThrow();
    } catch (Exception $e) {
        return new stdClass();
    }

    if (!$completion_data && !$type_context) {
        return new stdClass(); //HERE!
    }
    
    if ($completion_data) {
        // do something
    } else {
        /** @psalm-trace $type_context */
        // $type_context can't be null here or else we would have entered the conditional "HERE!" because both variables are false
    }
}

/** 
 * @return string|null
 * @throws Exception
 */
function getThingOrNullOrThrow(){
    
}
Psalm output (using commit 7b18673):

INFO: Trace - 23:0 - $type_context: non-falsy-string

INFO: MissingReturnType - 2:10 - Method scope does not have a return type, expecting null|stdClass

@orklah orklah added the bug label Dec 30, 2021
@weirdan weirdan linked a pull request Feb 20, 2022 that will close this issue
@weirdan
Copy link
Collaborator

weirdan commented Nov 22, 2022

Psalm now infers non-falsy-string for both snippets.

@weirdan weirdan closed this as completed Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants