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

Parent call inside a try-catch block(which is always executed) isn't recognised. #187

Open
omkar-podey opened this issue May 23, 2023 · 1 comment

Comments

@omkar-podey
Copy link

omkar-podey commented May 23, 2023

So, I am overriding tearDown() and as the rule suggests i need to call the parent::teardown() inside it , the only problem here is that i want to call it from a try-catch block which phpstan-phpunit doesn't recognise as a valid call to the parent method.

/**

{@inheritdoc}
*/
protected function tearDown(): void {
  try {
    parent::tearDown();
  }
  catch (\Exception $exception) {
    if (!(get_class($exception) === get_class($this->expectedTearDownException) && $exception->getMessage() === 
    $this->expectedTearDownException->getMessage())) {
    throw $exception;
  }
}
}

phpstan doesn't have this bug i checked in independently https://phpstan.org/r/1a212fa2-3cba-4c82-80db-7c75a8346f50

The main issue seems to be in
$hasParentCall = $this->hasParentClassCall($node->getOriginalNode()->getStmts(), strtolower($methodName));

		if (!$hasParentCall) {
			return [
				RuleErrorBuilder::message(
					sprintf('Missing call to parent::%s() method.', $methodName)
				)->build(),
			];
		}

Mainly \PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule::hasParentClassCall

I think this lacks the logic to detect parent class calls inside a try-catch block.

@ondrejmirtes
Copy link
Member

Yeah, sure, feel free to fix it :) Thanks.

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

2 participants