From fe29b8ea520dbfabbdd57d5e0a287e9b5aa247c1 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Wed, 9 Nov 2022 14:39:38 +0100 Subject: [PATCH 1/4] Use this->t instead of t --- .../BestPractice/UseThisTInsteadOfTRector.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/Rector/BestPractice/UseThisTInsteadOfTRector.php diff --git a/src/Rector/BestPractice/UseThisTInsteadOfTRector.php b/src/Rector/BestPractice/UseThisTInsteadOfTRector.php new file mode 100644 index 00000000..4a6190ad --- /dev/null +++ b/src/Rector/BestPractice/UseThisTInsteadOfTRector.php @@ -0,0 +1,80 @@ +t(). + */ +final class UseThisTInsteadOfTRector extends AbstractRector +{ + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Turns static t calls into $this->t.', + [ + new ConfiguredCodeSample( + 't("Text");', + '$this->t("Text");', + ['t' => '$this->t'] + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [FuncCall::class]; + } + + /** + * @param FuncCall $node + */ + public function refactor(Node $node): ?Node + { + if (!$this->isName($node, 't')) { + return null; + } + + // not to refactor here + $isVirtual = (bool)$node->name->getAttribute( + AttributeKey::VIRTUAL_NODE + ); + if ($isVirtual) { + return null; + } + + $parentFunction = $this->betterNodeFinder->findParentType($node, Node\Stmt\ClassMethod::class); + if (!$parentFunction instanceof Node\Stmt\ClassMethod || $parentFunction->isStatic()) { + return null; + } + + $class = $this->betterNodeFinder->findParentType($node, Class_::class); + if (!$class instanceof Class_) { + return null; + } + + $className = (string) $this->nodeNameResolver->getName($class); + if (method_exists($className, 't')) { + return new Node\Expr\MethodCall( + new Node\Expr\Variable('this'), + 't', + $node->args + ); + } + return null; + } +} From 7e5f1bcf0db8bba7fb0c8944c550948d8313ab2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brala?= Date: Mon, 5 May 2025 10:38:07 +0200 Subject: [PATCH 2/4] Update src/Rector/BestPractice/UseThisTInsteadOfTRector.php Co-authored-by: nLightened Development LLC --- src/Rector/BestPractice/UseThisTInsteadOfTRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rector/BestPractice/UseThisTInsteadOfTRector.php b/src/Rector/BestPractice/UseThisTInsteadOfTRector.php index 4a6190ad..de0bfffc 100644 --- a/src/Rector/BestPractice/UseThisTInsteadOfTRector.php +++ b/src/Rector/BestPractice/UseThisTInsteadOfTRector.php @@ -5,7 +5,7 @@ namespace DrupalRector\Rector\BestPractice; use PhpParser\Node; -use Rector\Core\Rector\AbstractRector; +use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use PhpParser\Node\Expr\FuncCall; From fff1efaa427cf6183c6e7aef241947427840239f Mon Sep 17 00:00:00 2001 From: bjorn Date: Mon, 5 May 2025 10:40:26 +0200 Subject: [PATCH 3/4] build: fix codestyle --- src/Rector/BestPractice/UseThisTInsteadOfTRector.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Rector/BestPractice/UseThisTInsteadOfTRector.php b/src/Rector/BestPractice/UseThisTInsteadOfTRector.php index de0bfffc..bc1fa2be 100644 --- a/src/Rector/BestPractice/UseThisTInsteadOfTRector.php +++ b/src/Rector/BestPractice/UseThisTInsteadOfTRector.php @@ -5,19 +5,18 @@ namespace DrupalRector\Rector\BestPractice; use PhpParser\Node; -use Rector\Rector\AbstractRector; -use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use PhpParser\Node\Expr\FuncCall; -use Rector\NodeTypeResolver\Node\AttributeKey; use PhpParser\Node\Stmt\Class_; +use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\Rector\AbstractRector; +use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * Replaces t() with $this->t(). */ final class UseThisTInsteadOfTRector extends AbstractRector { - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -50,7 +49,7 @@ public function refactor(Node $node): ?Node } // not to refactor here - $isVirtual = (bool)$node->name->getAttribute( + $isVirtual = (bool) $node->name->getAttribute( AttributeKey::VIRTUAL_NODE ); if ($isVirtual) { @@ -75,6 +74,7 @@ public function refactor(Node $node): ?Node $node->args ); } + return null; } } From 25ea8cf9417f8b718f772942acc0da4630b7dda5 Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 6 May 2025 09:18:15 +0200 Subject: [PATCH 4/4] build: add test for t rector. --- .../UseThisTInsteadOfTRectorTest.php | 35 +++++++++++++++++++ .../config/configured_rule.php | 10 ++++++ .../fixture/fixture.php.inc | 33 +++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/UseThisTInsteadOfTRectorTest.php create mode 100644 tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/config/configured_rule.php create mode 100644 tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/fixture/fixture.php.inc diff --git a/tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/UseThisTInsteadOfTRectorTest.php b/tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/UseThisTInsteadOfTRectorTest.php new file mode 100644 index 00000000..4a81344f --- /dev/null +++ b/tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/UseThisTInsteadOfTRectorTest.php @@ -0,0 +1,35 @@ +doTestFile($filePath); + } + + /** + * @return Iterator<> + */ + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__.'/fixture'); + } + + public function provideConfigFilePath(): string + { + // must be implemented + return __DIR__.'/config/configured_rule.php'; + } +} diff --git a/tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/config/configured_rule.php b/tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/config/configured_rule.php new file mode 100644 index 00000000..4dcbaa07 --- /dev/null +++ b/tests/src/Rector/BestPractive/UseThisTInsteadOfTRector/config/configured_rule.php @@ -0,0 +1,10 @@ + +----- +t('Hello world'); + } + +} + +?>