Merge pull request #8745 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class

fix: [FileLocator] Cannot declare class XXX, because the name is already in use
This commit is contained in:
kenjis 2024-04-13 19:47:48 +09:00 committed by GitHub
commit 666c8fc4ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,6 +28,13 @@ class FileLocator implements FileLocatorInterface
*/ */
protected $autoloader; protected $autoloader;
/**
* List of classnames that did not exist.
*
* @var list<class-string>
*/
private array $invalidClassnames = [];
public function __construct(Autoloader $autoloader) public function __construct(Autoloader $autoloader)
{ {
$this->autoloader = $autoloader; $this->autoloader = $autoloader;
@ -288,14 +295,20 @@ class FileLocator implements FileLocatorInterface
), ),
'\\' '\\'
); );
// Remove the file extension (.php) // Remove the file extension (.php)
$className = mb_substr($className, 0, -4); $className = mb_substr($className, 0, -4);
if (in_array($className, $this->invalidClassnames, true)) {
continue;
}
// Check if this exists // Check if this exists
if (class_exists($className)) { if (class_exists($className)) {
return $className; return $className;
} }
// If the class does not exist, it is an invalid classname.
$this->invalidClassnames[] = $className;
} }
} }