mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
[Nette] unite format of FormCallbackRector
This commit is contained in:
parent
36d9c5f5e0
commit
a9354be0e1
25
src/Node/Attribute.php
Normal file
25
src/Node/Attribute.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Node;
|
||||
|
||||
/**
|
||||
* List of attributes by constants, to prevent any typos.
|
||||
*
|
||||
* Because typo can causing return "null" instaed of real value - impossible to spot.
|
||||
*/
|
||||
final class Attribute
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PREVIOUS_NODE = 'prev';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const TYPE = 'type';
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
|
@ -3,29 +3,24 @@
|
|||
namespace Rector\Rector\Contrib\Nette;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Contract\Deprecation\DeprecationInterface;
|
||||
use Rector\Contract\Rector\RectorInterface;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\NodeFactory\NodeFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.
|
||||
*/
|
||||
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
|
||||
final class FormCallbackRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FORM_CLASS = 'Nette\Application\UI\Form';
|
||||
|
||||
/**
|
||||
* @var Node
|
||||
*/
|
||||
private $previousNode;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
|
@ -47,52 +42,47 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
|
|||
}
|
||||
|
||||
/**
|
||||
* @return null|int|Node
|
||||
* Detects "$form->onSuccess[] = $this->someAction;"
|
||||
*/
|
||||
public function enterNode(Node $node)
|
||||
{
|
||||
if ($this->previousNode && $this->isFormEventAssign($this->previousNode)) {
|
||||
if (! $node instanceof PropertyFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->nodeFactory->createArray($node->var, $node->name);
|
||||
}
|
||||
|
||||
$this->previousNode = $node;
|
||||
if ($this->isFormEventAssign($node)) {
|
||||
// do not check children, just go to next token
|
||||
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Assign) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->var instanceof ArrayDimFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->isFormEventHandler($node->var->var)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->expr instanceof PropertyFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Assign $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$node->expr = $this->nodeFactory->createArray($node->expr->var, $node->expr->name);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function isFormEventAssign(Node $node): bool
|
||||
private function isFormEventHandler(PropertyFetch $propertyFetchNode): bool
|
||||
{
|
||||
if (! $node instanceof PropertyFetch) {
|
||||
if ($propertyFetchNode->var->getAttribute(Attribute::TYPE) !== self::FORM_CLASS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->var->getAttribute('type') !== self::FORM_CLASS) {
|
||||
return false;
|
||||
}
|
||||
$propertyName = (string) $propertyFetchNode->name;
|
||||
|
||||
$propertyName = (string) $node->name;
|
||||
if (! in_array($propertyName, ['onSuccess', 'onSubmit'], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return in_array($propertyName, ['onSuccess', 'onSubmit', 'onError', 'onRender'], true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ final class NetteConfiguratorRector extends AbstractClassReplacerRector
|
|||
protected function getOldToNewClasses(): array
|
||||
{
|
||||
return [
|
||||
'Nette\Config\Configurator' => 'Nette\Configurator'
|
||||
'Nette\Config\Configurator' => 'Nette\Configurator',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ services:
|
|||
# PSR-4 autodiscovery
|
||||
Rector\:
|
||||
resource: '../../src'
|
||||
exclude: '../../src/{Node/Attribute.php}'
|
||||
|
||||
# autowire by interface
|
||||
Rector\Contract\Parser\ParserInterface:
|
||||
|
|
Loading…
Reference in New Issue
Block a user