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

False positive issue PhanPluginRedundantAssignmentInLoop is emitted #4542

Closed
umherirrender opened this issue Sep 11, 2021 · 0 comments · Fixed by #4543
Closed

False positive issue PhanPluginRedundantAssignmentInLoop is emitted #4542

umherirrender opened this issue Sep 11, 2021 · 0 comments · Fixed by #4543
Assignees
Labels
enhancement This improves the quality of Phan's analysis of a codebase

Comments

@umherirrender
Copy link

I am using phan 5.2.0

I have two issues with the issue PhanPluginRedundantAssignmentInLoop.

  • The first problem is that PhanPluginRedundantAssignmentInLoop is issued for the wrong line number in the file, which makes it harder to find the mention assignment.
  • The second problem is, that for the example code the issue should not be emitted, because the variable is changed in a way that the assignment is needed.

Issue:
..php:33 PhanPluginRedundantAssignmentInLoop Assigning 0 to variable $consecutiveErrors which already has that value
Example

<?php

class Task {
	public function isComplete():bool {
		return (bool)mt_rand( 0, 1 );
	}
	public function getStatus(): self {
		if( mt_rand( 0 , 1 ) ) {
			return new Task();
		}
		throw new LogicException();
	}
}

class TestClass {
	private const MAX_CONSECUTIVE_ERRORS = 3;

	private function task( Task $task ) {
		$consecutiveErrors = 0;
		while ( !$task->isComplete() ) {
			try {
				$status = $task->getStatus();
			} catch ( \Exception $e ) {
				if ( ++$consecutiveErrors > self::MAX_CONSECUTIVE_ERRORS ) {
					$this->fatalError();
				}
				continue;
			}
			$consecutiveErrors = 0; # <!-- Correct context
			
			echo "something";
			
			if ( !$status->isComplete() ) {  # <!-- PhanPluginRedundantAssignmentInLoop Assigning 0 to variable $consecutiveErrors which already has that value
				sleep( 10 );
			}
		}
	}
	
	private function fatalError(): never {
		throw new LogicException();
	}
}

The example is from a long running maintenance script which runs and retry the task as long it is not failing 3 times in serie.

@TysonAndre TysonAndre self-assigned this Sep 11, 2021
TysonAndre added a commit to TysonAndre/phan that referenced this issue Sep 11, 2021
`catch` should be treated like an if statement branch for identifying
`break`/`continue` calls at the level of that branch.

Closes phan#4542
@TysonAndre TysonAndre added the enhancement This improves the quality of Phan's analysis of a codebase label Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This improves the quality of Phan's analysis of a codebase
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants