Skip to content

Commit

Permalink
Merge pull request #10 from TomasVotruba/abstract-change-method-name-…
Browse files Browse the repository at this point in the history
…rector

add AbstractChangeMethodNameRector [closes #9]
  • Loading branch information
Tomáš Votruba authored Aug 20, 2017
2 parents 23c6c4d + 3ddc078 commit 5a5c276
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 62 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ This tool will *reconstruct* (change) your code - **run it only in a new clean g

- `FormCallbackRector`
- `InjectPropertyRector`
- `HtmlAddMethodRector`
- `NetteObjectToSmartTraitRector`
- `RemoveConfiguratorConstantsRector`

### [Symfony](https://github.com/symfony/)

- `NamedServicesToConstructorNodeTraverser`

### Abstract to use

- `AbstractChangeMethodNameRector`


## Install

Expand Down
3 changes: 2 additions & 1 deletion easy-coding-standard.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ parameters:
# class might not exist
- src/Rector/Contrib/Nette/RemoveConfiguratorConstantsRector.php
- src/Rector/Contrib/Nette/NetteObjectToSmartTraitRector.php
- src/Rector/Contrib/Nette/HtmlAddMethodRector.php
Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff:
# examples of code to be found
- src/Rector/Contrib/Symfony/GetterToPropertyRector.php
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
# will be used soon
- packages/NodeTypeResolver/src/TypeContext.php
- packages/NodeTypeResolver/src/TypeContext.php
71 changes: 71 additions & 0 deletions src/Rector/AbstractChangeMethodNameRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

namespace Rector\Rector;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;

abstract class AbstractChangeMethodNameRector extends AbstractRector
{
public function isCandidate(Node $node): bool
{
if ($this->isOnTypeCall($node, $this->getClassName())) {
return true;
}

if ($this->isStaticCall($node)) {
return true;
}

return false;
}

/**
* @param StaticCall|MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$node->name->name = $this->getNewMethodName();

return $node;
}

abstract protected function getClassName(): string;

abstract protected function getOldMethodName(): string;

abstract protected function getNewMethodName(): string;

private function isStaticCall(Node $node): bool
{
if (! $node instanceof StaticCall) {
return false;
}

if (! $node->name instanceof Identifier) {
return false;
}

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

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

private function isOnTypeCall(Node $node, string $class): bool
{
if (! $node instanceof MethodCall) {
return false;
}

if (! $node->var instanceof Variable) {
return false;
}

return $node->var->getAttribute('type') === $class;
}
}
69 changes: 8 additions & 61 deletions src/Rector/Contrib/Nette/HtmlAddMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@

namespace Rector\Rector\Contrib\Nette;

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;
use Rector\Rector\AbstractChangeMethodNameRector;

final class HtmlAddMethodRector extends AbstractRector
final class HtmlAddMethodRector extends AbstractChangeMethodNameRector
{
/**
* @var string
*/
private const CLASS_NAME = Html::class;

public function getSetName(): string
{
return SetNames::NETTE;
Expand All @@ -28,60 +17,18 @@ public function sinceVersion(): float
return 2.4;
}

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

if ($this->isStaticCall($node)) {
return true;
}

return false;
return 'Nette\Utils\Html';
}

/**
* @param StaticCall|MethodCall $node
*/
public function refactor(Node $node): ?Node
protected function getOldMethodName(): string
{
$node->name->name = 'addHtml';

return $node;
return 'add';
}

private function isStaticCall(Node $node): bool
protected function getNewMethodName(): string
{
if (! $node instanceof StaticCall) {
return false;
}

if (! $node->name instanceof Identifier) {
return false;
}

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

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

return true;
}

private function isOnTypeCall(Node $node, string $class): bool
{
if (! $node instanceof MethodCall) {
return false;
}

if (! $node->var instanceof Variable) {
return false;
}

return $node->var->getAttribute('type') === $class;
return 'addHtml';
}
}

0 comments on commit 5a5c276

Please sign in to comment.