diff --git a/src/Reflection/ClassNameHelper.php b/src/Reflection/ClassNameHelper.php index 8fe817e91d..815534f310 100644 --- a/src/Reflection/ClassNameHelper.php +++ b/src/Reflection/ClassNameHelper.php @@ -3,15 +3,23 @@ namespace PHPStan\Reflection; use Nette\Utils\Strings; +use function array_key_exists; use function ltrim; final class ClassNameHelper { + /** @var array */ + private static array $checked = []; + public static function isValidClassName(string $name): bool { - // from https://stackoverflow.com/questions/3195614/validate-class-method-names-with-regex#comment104531582_12011255 - return Strings::match(ltrim($name, '\\'), '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/') !== null; + if (!array_key_exists($name, self::$checked)) { + // from https://stackoverflow.com/questions/3195614/validate-class-method-names-with-regex#comment104531582_12011255 + self::$checked[$name] = Strings::match(ltrim($name, '\\'), '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/') !== null; + + } + return self::$checked[$name]; } }