phpstan-strict-rules icon indicating copy to clipboard operation
phpstan-strict-rules copied to clipboard

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

Open dktapps opened this issue 4 years ago • 0 comments

	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.

dktapps avatar May 19 '20 21:05 dktapps