From fd05efcdd0c7a9d1552f074c16732ea2047f4362 Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Mon, 20 Oct 2025 08:44:05 +0200 Subject: [PATCH 1/2] Better replacement for dynamic uppercase parts in Implements hook_xy --- src/Rector/Convert/HookConvertRector.php | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/Rector/Convert/HookConvertRector.php b/src/Rector/Convert/HookConvertRector.php index 0b8d7705..8c1f18ca 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -320,25 +320,11 @@ protected function getHookAndModuleName(Function_ $node): array { // If the doxygen contains "Implements hook_foo()" then parse the hook // name. A difficulty here is "Implements hook_form_FORM_ID_alter". - // Find these by looking for an optional part starting with an - // uppercase letter. if (preg_match('/^ \* Implements hook_([a-zA-Z0-9_]+)/m', (string) $node->getDocComment()?->getReformattedText(), $matches)) { - $parts = explode('_', $matches[1]); - $isUppercase = false; - foreach ($parts as &$part) { - if (!$part) { - continue; - } - if ($part === strtoupper($part)) { - if (!$isUppercase) { - $isUppercase = true; - $part = '[a-z0-9_]+'; - } - } else { - $isUpperCase = false; - } - } - $hookRegex = implode('_', $parts); + // Replace sections that start and end with an uppercase character + // and contain any number of uppercase characters or underscores + // inbetween. + $hookRegex = preg_replace('/([A-Z][A-Z0-9_]*[A-Z0-9])/', '[a-zA-Z0-9]+', $matches[1]); $hookRegex = "_(?$hookRegex)"; $functionName = $node->name->toString(); // And now find the module and the hook. From 52a350a68fa74f9d6a8c2b8af05ffa70bbb7981a Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher Date: Wed, 22 Oct 2025 01:05:02 +0200 Subject: [PATCH 2/2] Add _ to replacement regex to match multiple parts --- src/Rector/Convert/HookConvertRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rector/Convert/HookConvertRector.php b/src/Rector/Convert/HookConvertRector.php index 8c1f18ca..ae47af6a 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -324,7 +324,7 @@ protected function getHookAndModuleName(Function_ $node): array // Replace sections that start and end with an uppercase character // and contain any number of uppercase characters or underscores // inbetween. - $hookRegex = preg_replace('/([A-Z][A-Z0-9_]*[A-Z0-9])/', '[a-zA-Z0-9]+', $matches[1]); + $hookRegex = preg_replace('/([A-Z][A-Z0-9_]*[A-Z0-9])/', '[a-zA-Z0-9_]+', $matches[1]); $hookRegex = "_(?$hookRegex)"; $functionName = $node->name->toString(); // And now find the module and the hook.