Back to Phpstan

catch.internalClass

website/errors/catch.internalClass.md

2.2.1921 B
Original Source

Code example

php
<?php declare(strict_types = 1);

namespace Vendor {
	/** @internal */
	class InternalException extends \Exception {}
}

namespace App {
	function doFoo(): void
	{
		try {
			throw new \Exception();
		} catch (\Vendor\InternalException $e) {
		}
	}
}

Why is it reported?

A catch block is catching an exception class that is marked as @internal. Internal classes are not meant to be used outside of the package or namespace where they are defined. Catching internal exceptions creates a dependency on implementation details that may change without notice in future versions.

How to fix it

Catch a public (non-internal) parent exception class instead:

diff-php
 try {
 	throw new \Exception();
-} catch (\Vendor\InternalException $e) {
+} catch (\RuntimeException $e) {
 }

If the library provides a public exception class or interface for this purpose, catch that instead.