Error Identifier: instanceof.internalClass
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);
namespace App;
use Some\Internal\InternalService;
function checkService(object $obj): void
{
if ($obj instanceof InternalService) { // ERROR: Instanceof references internal class InternalService.
// ...
}
}
Why is it reported? #
The class used in the instanceof expression has been marked as @internal. Internal classes are not part of the public API of the package that defines them. Relying on internal types in instanceof checks creates a dependency on implementation details that may change without notice in future versions of the package.
How to fix it #
Use a public interface or class provided by the package instead:
<?php declare(strict_types = 1);
namespace App;
-use Some\Internal\InternalService;
+use Some\PublicServiceInterface;
function checkService(object $obj): void
{
- if ($obj instanceof InternalService) {
+ if ($obj instanceof PublicServiceInterface) {
// ...
}
}
If no public alternative exists, contact the package maintainer to request a public API.
How to ignore this error #
You can use the identifier instanceof.internalClass to ignore this error using a comment:
// @phpstan-ignore instanceof.internalClass
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.internalClass
Rules that report this error #
- PHPStan\Rules\InternalTag\RestrictedInternalClassNameUsageExtension [1]