Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Reflection/ClassNameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
namespace PHPStan\Reflection;

use Nette\Utils\Strings;
use function array_key_exists;
use function ltrim;

final class ClassNameHelper
{

/** @var array<string, bool> */
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;
Copy link
Contributor

@mvorisek mvorisek May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much faster https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.75.0/src/DocBlock/TypeExpression.php#L177-L179 this regex is using native preg_match directly?

Regexes are usually fast.

(...*)* in theory can be very slow compared to (...*+)*.

Also the trim can be easily integrated into the regex (and as the linked PHP CS Fixer already does).

And the main slowdown seems to be comming from Nette. As long as the regex is valid, no need to check for anything else than the return value. This is what PHPStan already checks and check well.


}
return self::$checked[$name];
}

}
Loading