From c3213f10bc7f9b60b93684d3712bee8d116d5310 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Apr 2026 19:40:33 +0700 Subject: [PATCH 1/7] Allow rector ^2.4.1 and replace file with getFile() if method exists --- composer.json | 2 +- src/Rector/Convert/HookConvertRector.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 996ac1599..d3fd72279 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "ast" ], "require": { - "rector/rector": "^1 || ^2", + "rector/rector": "^1 || ^2.4.1", "webflo/drupal-finder": "^1.2" }, "license": "MIT", diff --git a/src/Rector/Convert/HookConvertRector.php b/src/Rector/Convert/HookConvertRector.php index 00f977cfb..304abce7e 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -127,7 +127,9 @@ public function getNodeTypes(): array public function refactor(Node $node): Node|int|null { - $filePath = $this->file->getFilePath(); + $filePath = method_exists($this, 'getFile') + ? $this->file->getFilePath() + : $this->getFile()->getFilePath(); $ext = pathinfo($filePath, \PATHINFO_EXTENSION); if (!in_array($ext, ['inc', 'module'])) { return null; @@ -181,7 +183,9 @@ public function refactor(Node $node): Node|int|null protected function initializeHookClass(): void { $this->__destruct(); - $this->moduleDir = $this->file->getFilePath(); + $this->moduleDir = method_exists($this, 'getFile') + ? $this->file->getFilePath() + : $this->getFile()->getFilePath(); $this->inputFilename = $this->moduleDir; // Find the relevant info.yml: it's either in the current directory or // one of the parents. @@ -190,7 +194,7 @@ protected function initializeHookClass(): void if (!empty($info)) { $infoFile = reset($info); $this->module = basename($infoFile, '.info.yml'); - $filename = pathinfo($this->file->getFilePath(), \PATHINFO_FILENAME); + $filename = pathinfo(method_exists($this, 'getFile') ? $this->file->getFilePath() : $this->getFile()->getFilePath(), \PATHINFO_FILENAME); $hookClassName = ucfirst(CaseStringHelper::camelCase(str_replace('.', '_', $filename).'_hooks')); $counter = ''; do { From e4dc6ddbd59da01e46315c29975d0bf6857d1c4e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Apr 2026 19:45:52 +0700 Subject: [PATCH 2/7] Fix ternary check --- src/Rector/Convert/HookConvertRector.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Rector/Convert/HookConvertRector.php b/src/Rector/Convert/HookConvertRector.php index 304abce7e..e28901fd0 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -128,8 +128,8 @@ public function getNodeTypes(): array public function refactor(Node $node): Node|int|null { $filePath = method_exists($this, 'getFile') - ? $this->file->getFilePath() - : $this->getFile()->getFilePath(); + ? $this->getFile()->getFilePath() + : $this->file->getFilePath(); $ext = pathinfo($filePath, \PATHINFO_EXTENSION); if (!in_array($ext, ['inc', 'module'])) { return null; @@ -184,8 +184,8 @@ protected function initializeHookClass(): void { $this->__destruct(); $this->moduleDir = method_exists($this, 'getFile') - ? $this->file->getFilePath() - : $this->getFile()->getFilePath(); + ? $this->getFile()->getFilePath() + : $this->file->getFilePath(); $this->inputFilename = $this->moduleDir; // Find the relevant info.yml: it's either in the current directory or // one of the parents. @@ -194,7 +194,7 @@ protected function initializeHookClass(): void if (!empty($info)) { $infoFile = reset($info); $this->module = basename($infoFile, '.info.yml'); - $filename = pathinfo(method_exists($this, 'getFile') ? $this->file->getFilePath() : $this->getFile()->getFilePath(), \PATHINFO_FILENAME); + $filename = pathinfo(method_exists($this, 'getFile') ? $this->getFile()->getFilePath() : $this->file->getFilePath(), \PATHINFO_FILENAME); $hookClassName = ucfirst(CaseStringHelper::camelCase(str_replace('.', '_', $filename).'_hooks')); $counter = ''; do { From 274c9c521d4adad03e5ff230fc219327d845852e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Apr 2026 19:49:01 +0700 Subject: [PATCH 3/7] fix Use item usage --- src/Rector/Convert/HookConvertRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Rector/Convert/HookConvertRector.php b/src/Rector/Convert/HookConvertRector.php index e28901fd0..521ee01f9 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Use_; +use PhpParser\Node\UseItem; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor; @@ -221,7 +222,7 @@ public function __destruct() $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'))]), + new Use_([new UseItem(new Node\Name('Drupal\Core\Hook\Attribute\Hook'))]), $this->hookClass, ]; $this->hookClass->setDocComment(new \PhpParser\Comment\Doc("/**\n * Hook implementations for $this->module.\n */")); From ca3540066f2b3a9c0f720eb87de4277ea9dbb6d7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Apr 2026 19:50:48 +0700 Subject: [PATCH 4/7] fix Use item usage --- src/Rector/Convert/HookConvertRector.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Rector/Convert/HookConvertRector.php b/src/Rector/Convert/HookConvertRector.php index 521ee01f9..5b4dfe412 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -13,7 +13,6 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Use_; -use PhpParser\Node\UseItem; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor; @@ -222,7 +221,11 @@ public function __destruct() $hookClassStmts = [ new Node\Stmt\Namespace_(new Node\Name($namespace)), ...$this->useStmts, - new Use_([new UseItem(new Node\Name('Drupal\Core\Hook\Attribute\Hook'))]), + new Use_([ + class_exists('PhpParser\Node\UseItem') + ? new Node\UseItem(new Node\Name('Drupal\Core\Hook\Attribute\Hook')) + : new Node\Stmt\UseUse(new Node\Name('Drupal\Core\Hook\Attribute\Hook')) + ]), $this->hookClass, ]; $this->hookClass->setDocComment(new \PhpParser\Comment\Doc("/**\n * Hook implementations for $this->module.\n */")); From 0133a215674ae1fdff36e7e7658cac6b6e1f5407 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 30 Apr 2026 11:02:17 +0200 Subject: [PATCH 5/7] build: fix php codestyle issue --- 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 5b4dfe412..786298743 100644 --- a/src/Rector/Convert/HookConvertRector.php +++ b/src/Rector/Convert/HookConvertRector.php @@ -224,7 +224,7 @@ public function __destruct() new Use_([ class_exists('PhpParser\Node\UseItem') ? new Node\UseItem(new Node\Name('Drupal\Core\Hook\Attribute\Hook')) - : new Node\Stmt\UseUse(new Node\Name('Drupal\Core\Hook\Attribute\Hook')) + : new Node\Stmt\UseUse(new Node\Name('Drupal\Core\Hook\Attribute\Hook')), ]), $this->hookClass, ]; From 36962dfb51f349caa6729247d4e657adc13a1f58 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 30 Apr 2026 11:05:48 +0200 Subject: [PATCH 6/7] build: add ignore to baseline for BC code --- phpstan-baseline.neon | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1a1a3f45f..8e5b75159 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -572,3 +572,9 @@ parameters: identifier: property.deprecated count: 3 path: src/Rector/Convert/HookConvertRector.php + + - + message: '#^Call to function method_exists\(\) with \$this\(DrupalRector\\Rector\\Convert\\HookConvertRector\) and ''getFile'' will always evaluate to true\.$#' + identifier: function.alreadyNarrowedType + count: 3 + path: src/Rector/Convert/HookConvertRector.php From 892a9ac5158bd134b39533f8129f5dcf36087148 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 30 Apr 2026 11:15:29 +0200 Subject: [PATCH 7/7] build: don't be more specific on rector version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d3fd72279..996ac1599 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "ast" ], "require": { - "rector/rector": "^1 || ^2.4.1", + "rector/rector": "^1 || ^2", "webflo/drupal-finder": "^1.2" }, "license": "MIT",