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
22 changes: 4 additions & 18 deletions src/Rector/Convert/HookConvertRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
$hookClassStmts = [
new Node\Stmt\Namespace_(new Node\Name($namespace)),
...$this->useStmts,
new Use_([new Node\Stmt\UseUse(new Node\Name('Drupal\Core\Hook\Attribute\Hook'))]),

Check failure on line 221 in src/Rector/Convert/HookConvertRector.php

View workflow job for this annotation

GitHub Actions / Static analysis with PHPStan (8.2, ^1, phpstan-1.neon)

Instantiation of deprecated class PhpParser\Node\Stmt\UseUse: use \PhpParser\Node\UseItem instead.
$this->hookClass,
];
$this->hookClass->setDocComment(new \PhpParser\Comment\Doc("/**\n * Hook implementations for $this->module.\n */"));
Expand Down Expand Up @@ -322,25 +322,11 @@
{
// 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 = "_(?<hook>$hookRegex)";
$functionName = $node->name->toString();
// And now find the module and the hook.
Expand Down
Loading