add FormCallbackRector

This commit is contained in:
TomasVotruba 2017-08-08 01:15:01 +02:00
parent ae5f24d677
commit 604d4cb521
3 changed files with 53 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name": "rector/rector",
"description": "Tool that reconstructs your legacy code to modern codebase.",
"description": "Refactor legacy code to modern frameworks.",
"license": "MIT",
"authors": [
{ "name": "Tomas Votruba", "email": "tomas.vot@gmail.com", "homepage": "https://tomasvotruba.com" },
@ -11,7 +11,6 @@
"symfony/console": "^3.3",
"symfony/dependency-injection": "^3.3",
"nikic/php-parser": "4.0.x-dev as 3.0.2",
"ocramius/code-generator-utils": "^0.4",
"nette/utils": "^2.4"
},
"require-dev": {

View File

@ -50,14 +50,13 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
public function enterNode(Node $node)
{
if ($node instanceof Class_) {
$this->reconstruct($node);
return $node;
return $this->reconstruct($node);
}
return null;
}
private function reconstruct(Class_ $classNode): void
private function reconstruct(Class_ $classNode): Node
{
foreach ($classNode->stmts as $classElementStatement) {
if (! $classElementStatement instanceof Property) {
@ -81,6 +80,8 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
}
return $classNode;
}
private function createDocBlockFromProperty(Property $propertyNode): DocBlock

View File

@ -3,6 +3,11 @@
namespace Rector\Rector\Contrib\Nette;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\Deprecation\DeprecationInterface;
use Rector\Deprecation\SetNames;
@ -12,6 +17,11 @@ use Rector\Deprecation\SetNames;
*/
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface
{
/**
* @var Node
*/
private $previousNode;
public function getSetName(): string
{
return SetNames::NETTE;
@ -22,22 +32,52 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
return 2.4;
}
public function enterNode(Node $node): ?int
/**
* @return int|null|Node
*/
public function enterNode(Node $node)
{
if ($this->isCandidate($node)) {
return false;
dump($node); // get next node!
die;
if ($this->previousNode && $this->isFormEventAssign($this->previousNode)) {
if (! $node instanceof PropertyFetch) {
return null;
}
$this->refactor($node);
return new Array_([
new ArrayItem($node->var),
new ArrayItem(
new String_(
(string) $node->name
)
)
], [
'kind' => Array_::KIND_SHORT
]);
}
$this->previousNode = $node;
if ($this->isFormEventAssign($node)) {
// do not check children, just go to next token
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
return null;
}
private function isCandidate(Node $node): bool
private function isFormEventAssign(Node $node): bool
{
return $node instanceof Node\Expr\PropertyFetch;
if (! $node instanceof PropertyFetch) {
return false;
}
if ($node->var->name !== 'form') {
return false;
}
$propertyName = (string) $node->name;
if (! in_array($propertyName, ['onSuccess', 'onSubmit'], true)) {
return false;
}
return true;
}
}