Skip to content

Use this->t instead of t #215

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 PhpParser\Node\Expr\FuncCall;
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(
'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')) {
return null;
}

// not to refactor here
$isVirtual = (bool) $node->name->getAttribute(
AttributeKey::VIRTUAL_NODE

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

View workflow job for this annotation

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

Access to undefined constant Rector\NodeTypeResolver\Node\AttributeKey::VIRTUAL_NODE.

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

View workflow job for this annotation

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

Access to undefined constant Rector\NodeTypeResolver\Node\AttributeKey::VIRTUAL_NODE.
);
if ($isVirtual) {
return null;
}

$parentFunction = $this->betterNodeFinder->findParentType($node, Node\Stmt\ClassMethod::class);

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

View workflow job for this annotation

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

Access to an undefined property DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::$betterNodeFinder.

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

View workflow job for this annotation

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

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 64 in src/Rector/BestPractice/UseThisTInsteadOfTRector.php

View workflow job for this annotation

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

Access to an undefined property DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::$betterNodeFinder.

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

View workflow job for this annotation

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

Access to an undefined property DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::$betterNodeFinder.
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Rector\BestPractive\UseThisTInsteadOfTRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

class UseThisTInsteadOfTRectorTest extends AbstractRectorTestCase
{
/**
* @covers ::refactor
*
* @dataProvider provideData
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<<string>>
*/
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/fixture');
}

public function provideConfigFilePath(): string
{
// must be implemented
return __DIR__.'/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use DrupalRector\Tests\Rector\Deprecation\DeprecationBase;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
DeprecationBase::addClass(DrupalRector\Rector\BestPractice\UseThisTInsteadOfTRector::class, $rectorConfig, false);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
class TestT {
public function t($string) {
return $string;
}
}

class NoT extends TestT {

public function doThings() {
return t('Hello world');
}

}

?>
-----
<?php
class TestT {
public function t($string) {
return $string;
}
}

class NoT extends TestT {

public function doThings() {
return $this->t('Hello world');
}

}

?>
Loading