Closed
Description
Symfony version(s) affected
5.4.0,6.0.0
Description
If we have a class which has use
statements which use a namespace instead of a class/interface, \Symfony\Component\PropertyInfo\PhpStan\NameScope::resolveStringName
throws an exception.
How to reproduce
namespace App\Test\SomeNamespace;
class MyNamespacedClass
{
}
namespace App\Entity;
use App\Test\SomeNamespace as TestSomeNamespace;
class MyClass
{
public function __construct()
{
$object = new TestSomeNamespace\MyNamespacedClass();
}
}
run bin/console cache:clear
and you will get an exception like this
In NameScope.php line 50:
[ErrorException]
Warning: Undefined array key "MyNamespacedClass"
Possible Solution
I think there is a mistake in logic in \Symfony\Component\PropertyInfo\PhpStan\NameScope::resolveStringName
, lines 48 and 50. The array_shift
call is correct, but we need it's output because that's the value by which the uses are indexed in $this->uses
Changing those two lines to
public function resolveStringName(string $name): string
{
if (0 === strpos($name, '\\')) {
return ltrim($name, '\\');
}
$nameParts = explode('\\', $name);
if (isset($this->uses[$nameParts[0]])) {
if (1 === \count($nameParts)) {
return $this->uses[$nameParts[0]];
}
array_shift($nameParts);
return sprintf('%s\\%s', $this->uses[$nameParts[0]], implode('\\', $nameParts));
}
if (null !== $this->namespace) {
return sprintf('%s\\%s', $this->namespace, $name);
}
return $name;
}
public function resolveStringName(string $name): string
{
if (0 === strpos($name, '\\')) {
return ltrim($name, '\\');
}
$nameParts = explode('\\', $name);
if (isset($this->uses[$nameParts[0]])) {
if (1 === \count($nameParts)) {
return $this->uses[$nameParts[0]];
}
$use = array_shift($nameParts);
return sprintf('%s\\%s', $use, implode('\\', $nameParts));
}
if (null !== $this->namespace) {
return sprintf('%s\\%s', $this->namespace, $name);
}
return $name;
}
Additional Context
No response