Skip to content

Commit fe0fad3

Browse files
samsonasikbbrala
andauthored
Replace deprecated ->file with ->getFile() if method exists (#326)
* Allow rector ^2.4.1 and replace file with getFile() if method exists * Fix ternary check * fix Use item usage * fix Use item usage * build: fix php codestyle issue * build: add ignore to baseline for BC code * build: don't be more specific on rector version --------- Co-authored-by: Björn Brala <bjorn@swis.nl>
1 parent dc0375a commit fe0fad3

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,9 @@ parameters:
572572
identifier: property.deprecated
573573
count: 3
574574
path: src/Rector/Convert/HookConvertRector.php
575+
576+
-
577+
message: '#^Call to function method_exists\(\) with \$this\(DrupalRector\\Rector\\Convert\\HookConvertRector\) and ''getFile'' will always evaluate to true\.$#'
578+
identifier: function.alreadyNarrowedType
579+
count: 3
580+
path: src/Rector/Convert/HookConvertRector.php

src/Rector/Convert/HookConvertRector.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public function getNodeTypes(): array
127127

128128
public function refactor(Node $node): Node|int|null
129129
{
130-
$filePath = $this->file->getFilePath();
130+
$filePath = method_exists($this, 'getFile')
131+
? $this->getFile()->getFilePath()
132+
: $this->file->getFilePath();
131133
$ext = pathinfo($filePath, \PATHINFO_EXTENSION);
132134
if (!in_array($ext, ['inc', 'module'])) {
133135
return null;
@@ -181,7 +183,9 @@ public function refactor(Node $node): Node|int|null
181183
protected function initializeHookClass(): void
182184
{
183185
$this->__destruct();
184-
$this->moduleDir = $this->file->getFilePath();
186+
$this->moduleDir = method_exists($this, 'getFile')
187+
? $this->getFile()->getFilePath()
188+
: $this->file->getFilePath();
185189
$this->inputFilename = $this->moduleDir;
186190
// Find the relevant info.yml: it's either in the current directory or
187191
// one of the parents.
@@ -190,7 +194,7 @@ protected function initializeHookClass(): void
190194
if (!empty($info)) {
191195
$infoFile = reset($info);
192196
$this->module = basename($infoFile, '.info.yml');
193-
$filename = pathinfo($this->file->getFilePath(), \PATHINFO_FILENAME);
197+
$filename = pathinfo(method_exists($this, 'getFile') ? $this->getFile()->getFilePath() : $this->file->getFilePath(), \PATHINFO_FILENAME);
194198
$hookClassName = ucfirst(CaseStringHelper::camelCase(str_replace('.', '_', $filename).'_hooks'));
195199
$counter = '';
196200
do {
@@ -217,7 +221,11 @@ public function __destruct()
217221
$hookClassStmts = [
218222
new Node\Stmt\Namespace_(new Node\Name($namespace)),
219223
...$this->useStmts,
220-
new Use_([new Node\Stmt\UseUse(new Node\Name('Drupal\Core\Hook\Attribute\Hook'))]),
224+
new Use_([
225+
class_exists('PhpParser\Node\UseItem')
226+
? new Node\UseItem(new Node\Name('Drupal\Core\Hook\Attribute\Hook'))
227+
: new Node\Stmt\UseUse(new Node\Name('Drupal\Core\Hook\Attribute\Hook')),
228+
]),
221229
$this->hookClass,
222230
];
223231
$this->hookClass->setDocComment(new \PhpParser\Comment\Doc("/**\n * Hook implementations for $this->module.\n */"));

0 commit comments

Comments
 (0)