Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use this->t instead of t #215

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
80 changes: 80 additions & 0 deletions src/Rector/BestPractice/UseThisTInsteadOfTRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Rector\BestPractice;

use PhpParser\Node;
use Rector\Core\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_;

/**
* Replaces t() with $this->t().
*/
final class UseThisTInsteadOfTRector extends AbstractRector

Check failure on line 18 in src/Rector/BestPractice/UseThisTInsteadOfTRector.php

View workflow job for this annotation

GitHub Actions / Static analysis with PHPStan

Class DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector extends unknown class Rector\Core\Rector\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<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (!$this->isName($node, 't')) {

Check failure on line 48 in src/Rector/BestPractice/UseThisTInsteadOfTRector.php

View workflow job for this annotation

GitHub Actions / Static analysis with PHPStan

Call to an undefined method DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::isName().
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);

Check failure on line 60 in src/Rector/BestPractice/UseThisTInsteadOfTRector.php

View workflow job for this annotation

GitHub Actions / Static analysis with PHPStan

Access to an undefined property DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::$betterNodeFinder.
if (!$parentFunction instanceof Node\Stmt\ClassMethod || $parentFunction->isStatic()) {
return null;
}

$class = $this->betterNodeFinder->findParentType($node, Class_::class);

Check failure on line 65 in src/Rector/BestPractice/UseThisTInsteadOfTRector.php

View workflow job for this annotation

GitHub Actions / Static analysis with PHPStan

Access to an undefined property DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::$betterNodeFinder.
if (!$class instanceof Class_) {
return null;
}

$className = (string) $this->nodeNameResolver->getName($class);

Check failure on line 70 in src/Rector/BestPractice/UseThisTInsteadOfTRector.php

View workflow job for this annotation

GitHub Actions / Static analysis with PHPStan

Access to an undefined property DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::$nodeNameResolver.
if (method_exists($className, 't')) {
return new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
't',
$node->args
);
}
return null;
}
}
Loading