FR: report overriding outer scope var in `catch` statement

Open
#108 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
devtools

Research direction

The issue provides no repository file or test entry point; begin by locating existing strict rules for catch variables and their tests. Reproduce the shown PHP example, then add a diagnostic for the outer-scope variable override and verify it using the project's rule-test conventions.

Written by the indexing model from the issue text.

Description

	private static function printExceptionMessage(\Throwable $e) : string{
		$errstr = preg_replace('/\s+/', ' ', trim($e->getMessage()));

		$errno = $e->getCode();
		try{
			$errno = ErrorTypeToStringMap::get($errno);
		}catch(\InvalidArgumentException $e){
			//pass
		}

		$errfile = Filesystem::cleanPath($e->getFile());
		$errline = $e->getLine();

		return get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline";
	}

This code is sampled from a bigger project in which I discovered a bizarre bug today: this function always reports that the passed exception came from ErrorTypeToStringMap if the exception code was non-zero.
This happened because the catch block overrides the variable $e, and the change persists outside of the catch.

Dominant language
PHP
Stars
709
Forks
62
Avg merge
5d 19h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-strict-rules

All issues in phpstan/phpstan-strict-rules

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.