Back to Phpstan

catch.internalEnum

website/errors/catch.internalEnum.md

2.2.11006 B
Original Source

Code example

php
<?php declare(strict_types = 1);

namespace Vendor {
	/** @internal */
	enum ErrorCode: int {
		case NotFound = 404;
		case ServerError = 500;
	}
}

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

Why is it reported?

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

Enums are not throwable, so catching an enum is invalid regardless of the @internal annotation.

How to fix it

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

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

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