mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-05 19:00:49 +00:00
add FormCallbackRector
This commit is contained in:
parent
ae5f24d677
commit
604d4cb521
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "rector/rector",
|
"name": "rector/rector",
|
||||||
"description": "Tool that reconstructs your legacy code to modern codebase.",
|
"description": "Refactor legacy code to modern frameworks.",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{ "name": "Tomas Votruba", "email": "tomas.vot@gmail.com", "homepage": "https://tomasvotruba.com" },
|
{ "name": "Tomas Votruba", "email": "tomas.vot@gmail.com", "homepage": "https://tomasvotruba.com" },
|
||||||
|
@ -11,7 +11,6 @@
|
||||||
"symfony/console": "^3.3",
|
"symfony/console": "^3.3",
|
||||||
"symfony/dependency-injection": "^3.3",
|
"symfony/dependency-injection": "^3.3",
|
||||||
"nikic/php-parser": "4.0.x-dev as 3.0.2",
|
"nikic/php-parser": "4.0.x-dev as 3.0.2",
|
||||||
"ocramius/code-generator-utils": "^0.4",
|
|
||||||
"nette/utils": "^2.4"
|
"nette/utils": "^2.4"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|
|
@ -50,14 +50,13 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
|
||||||
public function enterNode(Node $node)
|
public function enterNode(Node $node)
|
||||||
{
|
{
|
||||||
if ($node instanceof Class_) {
|
if ($node instanceof Class_) {
|
||||||
$this->reconstruct($node);
|
return $this->reconstruct($node);
|
||||||
return $node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function reconstruct(Class_ $classNode): void
|
private function reconstruct(Class_ $classNode): Node
|
||||||
{
|
{
|
||||||
foreach ($classNode->stmts as $classElementStatement) {
|
foreach ($classNode->stmts as $classElementStatement) {
|
||||||
if (! $classElementStatement instanceof Property) {
|
if (! $classElementStatement instanceof Property) {
|
||||||
|
@ -81,6 +80,8 @@ final class InjectAnnotationToConstructorNodeVisitor extends NodeVisitorAbstract
|
||||||
|
|
||||||
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $classNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createDocBlockFromProperty(Property $propertyNode): DocBlock
|
private function createDocBlockFromProperty(Property $propertyNode): DocBlock
|
||||||
|
|
|
@ -3,6 +3,11 @@
|
||||||
namespace Rector\Rector\Contrib\Nette;
|
namespace Rector\Rector\Contrib\Nette;
|
||||||
|
|
||||||
use PhpParser\Node;
|
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 PhpParser\NodeVisitorAbstract;
|
||||||
use Rector\Contract\Deprecation\DeprecationInterface;
|
use Rector\Contract\Deprecation\DeprecationInterface;
|
||||||
use Rector\Deprecation\SetNames;
|
use Rector\Deprecation\SetNames;
|
||||||
|
@ -12,6 +17,11 @@ use Rector\Deprecation\SetNames;
|
||||||
*/
|
*/
|
||||||
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface
|
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Node
|
||||||
|
*/
|
||||||
|
private $previousNode;
|
||||||
|
|
||||||
public function getSetName(): string
|
public function getSetName(): string
|
||||||
{
|
{
|
||||||
return SetNames::NETTE;
|
return SetNames::NETTE;
|
||||||
|
@ -22,22 +32,52 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
|
||||||
return 2.4;
|
return 2.4;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function enterNode(Node $node): ?int
|
/**
|
||||||
|
* @return int|null|Node
|
||||||
|
*/
|
||||||
|
public function enterNode(Node $node)
|
||||||
{
|
{
|
||||||
if ($this->isCandidate($node)) {
|
if ($this->previousNode && $this->isFormEventAssign($this->previousNode)) {
|
||||||
return false;
|
if (! $node instanceof PropertyFetch) {
|
||||||
dump($node); // get next node!
|
return null;
|
||||||
die;
|
}
|
||||||
|
|
||||||
$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 NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user