{"record":{"id":"24be96c34c96b97d","repo":"rectorphp/rector","slug":"rector-name-cannot-be-empty","errorCode":null,"errorMessage":"Rector name cannot be empty","messagePattern":"Rector name cannot be empty","errorType":"exception","errorClass":"ShouldNotHappenException","httpStatus":null,"severity":"error","filePath":"src/Console/Command/CustomRuleCommand.php","lineNumber":44,"sourceCode":"     */\n    private ReflectionProvider $reflectionProvider;\n    public function __construct(SymfonyStyle $symfonyStyle, ReflectionProvider $reflectionProvider)\n    {\n        $this->symfonyStyle = $symfonyStyle;\n        $this->reflectionProvider = $reflectionProvider;\n        parent::__construct();\n    }\n    protected function configure(): void\n    {\n        $this->setName('custom-rule');\n        $this->setDescription('Create base of local custom rule with tests');\n    }\n    protected function execute(InputInterface $input, OutputInterface $output): int\n    {\n        // ask for rule name\n        $rectorName = $this->symfonyStyle->ask('What is the rule class name? (e.g. \"LegacyCallToDbalMethodCall\")?', null, static function (?string $answer): string {\n            if ($answer === '' || $answer === null) {\n                throw new ShouldNotHappenException('Rector name cannot be empty');\n            }\n            return $answer;\n        });\n        // suffix with Rector by convention\n        if (substr_compare((string) $rectorName, 'Rector', -strlen('Rector')) !== 0) {\n            $rectorName .= 'Rector';\n        }\n        $rectorName = ucfirst((string) $rectorName);\n        // find all files in templates directory\n        $finder = Finder::create()->files()->in(__DIR__ . '/../../../templates/custom-rule')->notName('__Name__Test.php.phtml');\n        // 0. resolve if local phpunit is at least PHPUnit 10 (which supports #[DataProvider])\n        // to provide annotation if not\n        if ($this->isPHPUnitAttributeSupported()) {\n            $finder->append([new SplFileInfo(__DIR__ . '/../../../templates/custom-rule/utils/rector/tests/Rector/__Name__/__Name__Test.php.phtml', 'utils/rector/tests/Rector/__Name__', 'utils/rector/tests/Rector/__Name__/__Name__Test.php.phtml')]);\n        } else {\n            // use @annotations for PHPUnit 9 and bellow\n            $finder->append([new SplFileInfo(__DIR__ . '/../../../templates/custom-rules-annotations/utils/rector/tests/Rector/__Name__/__Name__Test.php.phtml', 'utils/rector/tests/Rector/__Name__', 'utils/rector/tests/Rector/__Name__/__Name__Test.php.phtml')]);\n        }","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/src/Console/Command/CustomRuleCommand.php#L26-L62","documentation":"The custom-rule console command interactively asks for the rule class name using SymfonyStyle::ask() with a validator closure. The closure throws ShouldNotHappenException('Rector name cannot be empty') when the answer is null (Ctrl+C style abort) or an empty string, so the scaffold command refuses to generate files for an unnamed rule.","triggerScenarios":"Running vendor/bin/rector custom-rule and pressing Enter on the 'What is the rule class name?' prompt without typing anything; or piping empty stdin in a non-interactive script.","commonSituations":"Running the command in CI or a scripted pipeline with no TTY where ask() receives empty input; accidentally hitting Enter twice; automating scaffolds with expect and mis-timing the answer.","solutions":["Re-run vendor/bin/rector custom-rule and type a non-empty class name, e.g. LegacyCallToDbalMethodCall (the 'Rector' suffix is appended automatically if missing)","When scripting, pipe the name in: echo 'MyRule' | vendor/bin/rector custom-rule","Do not abort at the prompt with empty input; use Ctrl+C to cancel intentionally"],"exampleFix":"// before (interactive session)\n$ vendor/bin/rector custom-rule\nWhat is the rule class name? => <Enter>   // throws\n\n// after\n$ vendor/bin/rector custom-rule\nWhat is the rule class name? => LegacyCallToDbalMethodCall","handlingStrategy":"validation","validationCode":"// pre-validate before driving the command non-interactively\n$ruleName = trim((string) $input->getOption('rule-name'));\nif ($ruleName === '') {\n    throw new RuntimeException('Rule name is required when running custom-rule non-interactively.');\n}\nshell_exec(sprintf('echo %s | vendor/bin/rector custom-rule', escapeshellarg($ruleName)));","typeGuard":"function isValidRuleClassName(string $name): bool\n{\n    return preg_match('/^[a-zA-Z_\\x80-\\xff][a-zA-Z0-9_\\x80-\\xff]*$/', $name) === 1;\n}","tryCatchPattern":null,"preventionTips":["Provide the name up front when scripting instead of relying on the interactive prompt","Type the class in StudlyCase; the command appends the Rector suffix automatically","Do not run interactive scaffold commands in CI stages without piped input"],"tags":["php","rector","cli","interactive-prompt","scaffolding"],"backgroundTag":"empty-required-input","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}