Skip to content

Commit

Permalink
add AbstractChangeMethodNameRector [closes #9]
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 20, 2017
1 parent 9cf112a commit 34dce56
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
<?php declare(strict_types=1);

namespace Rector\Rector\Contrib\Nette;
namespace Rector\Rector;

use Nette\Utils\Html;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\Deprecation\SetNames;
use Rector\Rector\AbstractRector;

final class HtmlAddMethodRector extends AbstractRector
abstract class AbstractChangeMethodNameRector extends AbstractRector
{
/**
* @var string
*/
private const CLASS_NAME = Html::class;
abstract protected function getClassName(): string;

public function getSetName(): string
{
return SetNames::NETTE;
}
abstract protected function getOldMethodName(): string;

public function sinceVersion(): float
{
return 2.4;
}
abstract protected function getNewMethodName(): string;

public function isCandidate(Node $node): bool
{
if ($this->isOnTypeCall($node, self::CLASS_NAME)) {
if ($this->isOnTypeCall($node, $this->getClassName())) {
return true;
}

Expand All @@ -46,7 +34,7 @@ public function isCandidate(Node $node): bool
*/
public function refactor(Node $node): ?Node
{
$node->name->name = 'addHtml';
$node->name->name = $this->getNewMethodName();

return $node;
}
Expand All @@ -61,15 +49,11 @@ private function isStaticCall(Node $node): bool
return false;
}

if ($node->class->toString() !== self::CLASS_NAME) {
return false;
}

if ((string) $node->name !== 'add') {
if ($node->class->toString() !== $this->getClassName()) {
return false;
}

return true;
return (string) $node->name === $this->getOldMethodName();
}

private function isOnTypeCall(Node $node, string $class): bool
Expand Down
34 changes: 34 additions & 0 deletions src/Rector/Contrib/Nette/HtmlAddMethodRectorRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Rector\Rector\Contrib\Nette;

use Rector\Deprecation\SetNames;
use Rector\Rector\AbstractChangeMethodNameRector;

final class HtmlAddMethodRectorRector extends AbstractChangeMethodNameRector
{
public function getSetName(): string
{
return SetNames::NETTE;
}

public function sinceVersion(): float
{
return 2.4;
}

protected function getClassName(): string
{
return 'Nette\Utils\Html';
}

protected function getOldMethodName(): string
{
return 'add';
}

protected function getNewMethodName(): string
{
return 'addHtml';
}
}
4 changes: 2 additions & 2 deletions tests/Rector/Contrib/Nette/HtmlAddMethodRector/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\Rector\Contrib\Nette\HtmlAddMethodRector;

use Rector\Rector\Contrib\Nette\HtmlAddMethodRector;
use Rector\Rector\Contrib\Nette\HtmlAddMethodRectorRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class Test extends AbstractRectorTestCase
Expand Down Expand Up @@ -36,6 +36,6 @@ public function test(): void
*/
protected function getRectorClasses(): array
{
return [HtmlAddMethodRector::class];
return [HtmlAddMethodRectorRector::class];
}
}

0 comments on commit 34dce56

Please sign in to comment.