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

PhanPossiblyUndeclaredVariable for variables defined in try blocks without catch #4419

Open
DannyS712 opened this issue May 24, 2021 · 3 comments

Comments

@DannyS712
Copy link
Contributor

Basic reproduction case for phan 4.0.6 via https://phan.github.io/demo/

<?php

function doStuff() {
    try {
        $str = 'abc';
    } finally {
    }

    echo ( $str );
}

doStuff();

Running Analyze includes the warning input:9: PhanPossiblyUndeclaredVariable Variable $str is possibly undeclared
If there was a catch block that could handle any errors and still allow later code to be executed, it would make sense to treat $str as undeclared, because an error in creating $str would not prevent subsequent code that tries to use it from running. But, when there is no catch block, any error in declaring $str would bubble up and later code would not be executed, so if the later code is reached then the variable must be declared.

@DannyS712
Copy link
Contributor Author

Sorry if this is a duplicate, I searched for PhanPossiblyUndeclaredVariable in open tasks and didn't see any

@TysonAndre
Copy link
Member

It's similar to #3403 which may have predated PhanPossiblyUndeclaredVariable or been on a project that didn't enable it

I think the finally would need to be analyzed twice

  1. Once without the try block, to analyze what would happen if any variable definitions were missing when the finally ran (approximately)
  2. Once with the try block fully executed, to analyze what would happen for success (approximately)

And only the result of the second would be merged into the outer context.

@umherirrender
Copy link

A similar case with catch statements, but the scopes of the catches could be merged to the function scope, because the try block returns or the catch statements are running.

function doStuff() {
	try {
		return true;
	} catch ( \LogicException $e ) {
		$t = 'logic';
	} catch ( \Exception $e ) {
		$t = 'exception';
	}
	echo $t;  # PhanPossiblyUndeclaredVariable Variable $t is possibly undeclared
}

TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 21, 2021
TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 21, 2021
TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants