rectorphp/rector · error · InvalidArgumentException
" " is not a valid name
Error message
"%s" is not a valid %s name
What it means
RectorAssert::elementName() is the shared validator behind className/propertyName/methodName/constantName/functionName. It matches the name against a PHP identifier regex (class names may contain namespaces; function names may be namespaced; properties/methods/constants must be bare identifiers) and throws webmozart InvalidArgumentException('"%s" is not a valid %s name') on the first mismatch.
Solutions
- Use valid PHP identifier characters: start [a-zA-Z_\x80-\xff], continue [a-zA-Z0-9_\x80-\xff], backslash-separated for namespaced class/function names
- Sanitize/validate external input before passing it to the assert (preg_match the same pattern)
- Fix separators: convert 'App.Class' to 'App\\Class'
Example fix
// before
RectorAssert::functionName('my-function');
// after
RectorAssert::functionName('my_function'); Defensive patterns
Strategy: validation
Validate before calling
// same shape Rector uses for bare identifiers
function isValidPhpIdentifier(string $name): bool
{
return preg_match('#^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$#', $name) === 1;
}
// namespaced class/function FQCNs
function isValidPhpFqcn(string $name): bool
{
return preg_match('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$#', $name) === 1;
} Try / catch
try {
RectorAssert::functionName($name);
} catch (\RectorPrefix202608\Webmozart\Assert\InvalidArgumentException $e) {
// reject config input with a field-scoped message
$errors[] = "config.functions: {$e->getMessage()}";
} Prevention
- Validate names at the config boundary (CLI/env/user input), not deep in rule execution
- Build FQCNs with implode('\\', $segments) rather than string concat of dotted input
- Keep a shared identifier-regex helper so wrappers and RectorAssert never drift
When it happens
Trigger: Calling RectorAssert::functionName('my-function') (hyphen), className('App.Class') (dot), or methodName('9to5') (leading digit); passing names assembled from user input that never got sanitized.
Common situations: Custom rules validating configured identifiers (e.g. a rule driven by a function-name list from rector.php); config parsing where an FQCN arrives with wrong separators; generated names from templates containing dashes.
Related errors
- Parameter " :: " must be int, " " given.%sUse constant from…
- a literal null byte in the regex
- an invalid bound
- an invalid character range
- an invalid escape sequence at the end
AI-assisted analysis of rectorphp/rector@408fcb0ff1 (2026-08-21).
Data as JSON: /api/errors/000061921674e4ab.
Report an issue: GitHub.
Appendix: source
Thrown at src/Validation/RectorAssert.php:73
}
public static function methodName(string $name): void
{
self::elementName($name, self::METHOD_OR_CONSTANT_NAME_REGEX, 'method');
}
public static function functionName(string $name): void
{
self::elementName($name, self::FUNCTION_NAME_REGEX, 'function');
}
/**
* @api
*/
public static function elementName(string $name, string $regex, string $elementType): void
{
if (StringUtils::isMatch($name, $regex)) {
return;
}
$errorMessage = sprintf('"%s" is not a valid %s name', $name, $elementType);
throw new InvalidArgumentException($errorMessage);
}
}
View on GitHub (pinned to 408fcb0ff1)