rectorphp/rector · error · InvalidArgumentException

"%s" is not a valid %s 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.

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)

Solutions

  1. 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
  2. Sanitize/validate external input before passing it to the assert (preg_match the same pattern)
  3. 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

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


AI-assisted analysis of rectorphp/rector@408fcb0ff1 (2026-08-21). Data as JSON: /api/errors/000061921674e4ab. Report an issue: GitHub.