Error Identifier: class.nameInUse
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 SomeNamespace;
use SomeOtherNamespace\Foo;
class Foo
{
}
Why is it reported? #
A class is being declared with a name that is already in use within the same namespace. This happens when a use statement imports a class with the same name as a class being declared in the current file. PHP cannot resolve which Foo is intended, resulting in a fatal error at runtime.
In the example above, Foo is imported via use SomeOtherNamespace\Foo and then a class named Foo is declared in the SomeNamespace namespace. Both refer to the short name Foo, creating a conflict.
How to fix it #
Rename the local class to avoid the conflict:
<?php declare(strict_types = 1);
namespace SomeNamespace;
use SomeOtherNamespace\Foo;
-class Foo
+class MyFoo
{
}
Or use an alias for the imported class:
<?php declare(strict_types = 1);
namespace SomeNamespace;
-use SomeOtherNamespace\Foo;
+use SomeOtherNamespace\Foo as OtherFoo;
class Foo
{
}
Or remove the use statement if the imported class is not needed:
<?php declare(strict_types = 1);
namespace SomeNamespace;
-use SomeOtherNamespace\Foo;
-
class Foo
{
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Names\UsedNamesRule [1]