Error Identifier: instanceof.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 package vendor/some-library:
/** @internal */
trait InternalTrait
{
}
// In your code:
function doFoo(object $obj): void
{
if ($obj instanceof InternalTrait) {
// ...
}
}
Why is it reported? #
The instanceof check references a trait that is marked as @internal. Internal types are implementation details of their package and may change or be removed without notice. Code outside the package should not depend on them, even in type checks.
How to fix it #
Use a public interface or class instead of the internal trait in the instanceof check:
function doFoo(object $obj): void
{
- if ($obj instanceof InternalTrait) {
+ if ($obj instanceof PublicInterface) {
// ...
}
}
If the library does not expose a suitable public type, consider requesting one from the library maintainers.
How to ignore this error #
You can use the identifier instanceof.internalTrait to ignore this error using a comment:
// @phpstan-ignore instanceof.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: instanceof.internalTrait
Rules that report this error #
- PHPStan\Rules\InternalTag\RestrictedInternalClassNameUsageExtension [1]