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 Vendor {
/** @internal */
class InternalConfig {
public const VERSION = '1.0';
}
}
namespace App {
function test(): string {
return \Vendor\InternalConfig::VERSION; // error: Access to constant VERSION of internal class Vendor\InternalConfig from outside its root namespace Vendor.
}
}
Why is it reported? #
A constant is being accessed on a class that is marked as @internal. Internal classes are not meant to be used outside the package or namespace where they are defined. Accessing constants on internal classes creates a dependency on implementation details that may change without notice.
How to fix it #
Use a public (non-internal) API to access the information:
namespace App {
function test(): string {
- return \Vendor\InternalConfig::VERSION;
+ return \Vendor\Application::getVersion();
}
}
If no public API exists, consider requesting one from the library maintainers. Relying on internal classes is fragile and may break on library updates.
How to ignore this error #
You can use the identifier classConstant.internalClass to ignore this error using a comment:
// @phpstan-ignore classConstant.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: classConstant.internalClass