Error Identifier: catch.internalTrait
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
// In vendor/some-library/src/InternalTrait.php:
// namespace SomeLibrary;
// /** @internal */
// trait SomeInternalTrait {}
// In your code:
namespace App;
use SomeLibrary\SomeInternalTrait;
try {
// ...
} catch (SomeInternalTrait $e) { // reported
// ...
}
Why is it reported? #
A catch block references a trait that is marked as @internal by its library. Internal symbols are not meant to be used outside of the package that defines them. While catching a trait in a catch block is unusual and likely an error in itself, PHPStan specifically flags the usage of an internal trait in this context.
How to fix it #
Catch the appropriate exception class instead of referencing an internal trait.
try {
// ...
-} catch (SomeInternalTrait $e) {
+} catch (SomeException $e) {
// ...
}
How to ignore this error #
You can use the identifier catch.internalTrait to ignore this error using a comment:
// @phpstan-ignore catch.internalTrait
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: catch.internalTrait
Rules that report this error #
- PHPStan\Rules\InternalTag\RestrictedInternalClassNameUsageExtension [1]