Error Identifier: magicConstant.outOfNamespace
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);
echo __NAMESPACE__;
Why is it reported? #
The magic constant __NAMESPACE__ is used outside of any namespace declaration, where its value is always an empty string. This usually indicates a logic error, as the code likely expects the constant to contain a meaningful namespace value.
How to fix it #
Place the code inside a namespace declaration:
-<?php declare(strict_types = 1);
-+<?php declare(strict_types = 1);
+
+namespace App;
+
echo __NAMESPACE__;
Or use the expected namespace string directly if the namespace is known.
How to ignore this error #
You can use the identifier magicConstant.outOfNamespace to ignore this error using a comment:
// @phpstan-ignore magicConstant.outOfNamespace
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: magicConstant.outOfNamespace
Rules that report this error #
- PHPStan\Rules\Constants\MagicConstantContextRule [1]