mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
move NodeVisitor to Rector, add AbstractRector
This commit is contained in:
parent
ef97f993be
commit
bcc02f678c
15
src/Contract/Rector/RectorInterface.php
Normal file
15
src/Contract/Rector/RectorInterface.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Contract\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
|
||||
interface RectorInterface
|
||||
{
|
||||
public function isCandidate(Node $node): bool;
|
||||
|
||||
/**
|
||||
* @param Node $node
|
||||
*/
|
||||
public function refactor($node): void;
|
||||
}
|
22
src/Rector/AbstractRector.php
Normal file
22
src/Rector/AbstractRector.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Contract\Deprecation\DeprecationInterface;
|
||||
use Rector\Contract\Rector\RectorInterface;
|
||||
|
||||
abstract class AbstractRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
|
||||
{
|
||||
public function enterNode(Node $node): ?int
|
||||
{
|
||||
if ($this->isCandidate($node)) {
|
||||
$this->refactor($node);
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
42
src/Rector/Contrib/Nette/FormCallbackRector.php
Normal file
42
src/Rector/Contrib/Nette/FormCallbackRector.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Nette;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Contract\Deprecation\DeprecationInterface;
|
||||
use Rector\Deprecation\SetNames;
|
||||
|
||||
/**
|
||||
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject
|
||||
*/
|
||||
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::NETTE;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 2.4;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): ?int
|
||||
{
|
||||
if ($this->isCandidate($node)) {
|
||||
dump($node); // get next node!
|
||||
die;
|
||||
|
||||
$this->refactor($node);
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isCandidate(Node $node): bool
|
||||
{
|
||||
return $node instanceof Node\Expr\PropertyFetch;
|
||||
}
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeVisitor\UpgradeDeprecation;
|
||||
namespace Rector\Rector\Contrib\Nette;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\TraitUse;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Builder\StatementGlue;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Reflects @link https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject
|
||||
*/
|
||||
final class DeprecatedParentClassToTraitNodeVisitor extends NodeVisitorAbstract
|
||||
final class NetteObjectToSmartTraitRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var StatementGlue
|
||||
|
@ -25,27 +25,17 @@ final class DeprecatedParentClassToTraitNodeVisitor extends NodeVisitorAbstract
|
|||
$this->statementGlue = $statementGlue;
|
||||
}
|
||||
|
||||
public function getParentClassName(): string
|
||||
public function getSetName(): string
|
||||
{
|
||||
return 'Nette\Object';
|
||||
return SetNames::NETTE;
|
||||
}
|
||||
|
||||
public function getTraitName(): string
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 'Nette\SmartObject';
|
||||
return 2.2;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): ?int
|
||||
{
|
||||
if ($this->isCandidate($node)) {
|
||||
$this->refactor($node);
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function isCandidate(Node $node): bool
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if ($node instanceof Class_) {
|
||||
if (! $node->extends) {
|
||||
|
@ -53,7 +43,7 @@ final class DeprecatedParentClassToTraitNodeVisitor extends NodeVisitorAbstract
|
|||
}
|
||||
|
||||
$parentClassName = (string) $node->extends;
|
||||
if ($parentClassName !== $this->getParentClassName()) {
|
||||
if ($parentClassName !== 'Nette\Object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -63,21 +53,22 @@ final class DeprecatedParentClassToTraitNodeVisitor extends NodeVisitorAbstract
|
|||
return false;
|
||||
}
|
||||
|
||||
private function refactor(Class_ $classNode): void
|
||||
/**
|
||||
* @param Class_ $classNode
|
||||
*/
|
||||
public function refactor($classNode): void
|
||||
{
|
||||
// remove parent class
|
||||
$classNode->extends = null;
|
||||
|
||||
$traitUseNode = $this->createTraitUse($this->getTraitName());
|
||||
$traitUseNode = $this->createTraitUse('Nette\SmartObject');
|
||||
$this->statementGlue->addAsFirstTrait($classNode, $traitUseNode);
|
||||
}
|
||||
|
||||
private function createTraitUse(string $traitName): TraitUse
|
||||
{
|
||||
$nameParts = explode('\\', $traitName);
|
||||
|
||||
return new TraitUse([
|
||||
new FullyQualified($nameParts)
|
||||
new FullyQualified($traitName)
|
||||
]);
|
||||
}
|
||||
}
|
16
tests/Rector/Contrib/Nette/FormCallbackRector/Test.php
Normal file
16
tests/Rector/Contrib/Nette/FormCallbackRector/Test.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Nette\FormCallbackRector;
|
||||
|
||||
use Rector\Testing\PHPUnit\AbstractReconstructorTestCase;
|
||||
|
||||
final class Test extends AbstractReconstructorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong.php.inc',
|
||||
__DIR__ . '/correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
class SomePresenter
|
||||
{
|
||||
public function createForm()
|
||||
{
|
||||
$form = new Form;
|
||||
|
||||
$form->onSuccess[] = function (Form $form) {
|
||||
$this->someMethod($form);
|
||||
};
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
class SomePresenter
|
||||
{
|
||||
public function createForm()
|
||||
{
|
||||
$form = new Form;
|
||||
|
||||
$form->onSuccess[] = [$this, 'someMethod'];
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
class SomePresenter
|
||||
{
|
||||
public function createForm()
|
||||
{
|
||||
$form = new Form;
|
||||
|
||||
$form->onSuccess[] = $this->someMethod;
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\NodeVisitor\UpgradeDeprecation\DeprecatedParentClassToTraitNodeVisitor;
|
||||
namespace Rector\Tests\Rector\Contrib\NetteObjectToSmartTraitRector;
|
||||
|
||||
use Rector\Testing\PHPUnit\AbstractReconstructorTestCase;
|
||||
|
Loading…
Reference in New Issue
Block a user