mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Merge pull request #3784 from rectorphp/improve-date-control
[Nette] Improve AddDatePickerToDateControlRector
This commit is contained in:
commit
d8b829a582
@ -5177,7 +5177,7 @@ Nextras/Form upgrade of addDatePicker method call to DateControl assign
|
||||
{
|
||||
$form = new Form();
|
||||
- $form->addDatePicker('key', 'Label');
|
||||
+ $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
+ $keyDateControl = $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -9,12 +9,18 @@ use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* @sponsor Thanks https://amateri.com for sponsoring this rule - visit them on https://www.startupjobs.cz/startup/scrumworks-s-r-o
|
||||
*
|
||||
* @see \Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\AddDatePickerToDateControlRectorTest
|
||||
*/
|
||||
final class AddDatePickerToDateControlRector extends AbstractRector
|
||||
@ -44,7 +50,7 @@ class SomeClass
|
||||
public function run()
|
||||
{
|
||||
$form = new Form();
|
||||
$form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
$keyDateControl = $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
}
|
||||
}
|
||||
PHP
|
||||
@ -66,24 +72,75 @@ PHP
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isName($node->name, 'addDatePicker')) {
|
||||
// 1. chain call
|
||||
if ($node->var instanceof MethodCall) {
|
||||
if (! $this->isOnClassMethodCall($node->var, 'Nette\Application\UI\Form', 'addDatePicker')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$assign = $this->createAssign($node->var);
|
||||
if ($assign === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$controlName = $this->resolveControlName($node->var);
|
||||
|
||||
$node->var = new Variable($controlName);
|
||||
|
||||
// this fixes printing indent
|
||||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->addNodeBeforeNode($assign, $node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
// 2. assign call
|
||||
if (! $this->isOnClassMethodCall($node, 'Nette\Application\UI\Form', 'addDatePicker')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isObjectType($node->var, 'Nette\Application\UI\Form')) {
|
||||
return null;
|
||||
return $this->createAssign($node);
|
||||
}
|
||||
|
||||
private function resolveControlName(MethodCall $methodCall): string
|
||||
{
|
||||
$controlName = $methodCall->args[0]->value;
|
||||
if (! $controlName instanceof String_) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
$key = $node->args[0]->value;
|
||||
$arrayDimFetch = new ArrayDimFetch($node->var, $key);
|
||||
return $controlName->value . 'DateControl';
|
||||
}
|
||||
|
||||
private function createDateTimeControlNew($node): New_
|
||||
{
|
||||
$fullyQualified = new FullyQualified('Nextras\FormComponents\Controls\DateControl');
|
||||
$new = new New_($fullyQualified);
|
||||
|
||||
if (isset($node->args[1])) {
|
||||
$new->args[] = $node->args[1];
|
||||
}
|
||||
return $new;
|
||||
}
|
||||
|
||||
return new Assign($arrayDimFetch, $new);
|
||||
private function createAssign(MethodCall $methodCall): ?Assign
|
||||
{
|
||||
$key = $methodCall->args[0]->value;
|
||||
if (! $key instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$arrayDimFetch = new ArrayDimFetch($methodCall->var, $key);
|
||||
$new = $this->createDateTimeControlNew($methodCall);
|
||||
$formAssign = new Assign($arrayDimFetch, $new);
|
||||
|
||||
$parent = $methodCall->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parent instanceof Assign) {
|
||||
return $formAssign;
|
||||
}
|
||||
|
||||
$controlName = $this->resolveControlName($methodCall);
|
||||
|
||||
return new Assign(new Variable($controlName), $formAssign);
|
||||
}
|
||||
}
|
||||
|
@ -27,8 +27,8 @@ class AddRule
|
||||
public function run()
|
||||
{
|
||||
$form = new Form();
|
||||
($form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label'))
|
||||
->addRule('...');
|
||||
$keyDateControl = $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
$keyDateControl->addRule('...');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
class AddRuleChainCalls
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$form = new Form();
|
||||
$form->addDatePicker('key', 'Label')
|
||||
->addRule('...')
|
||||
->addContitionOn('...');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\AddDatePickerToDateControlRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
class AddRuleChainCalls
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$form = new Form();
|
||||
$keyDateControl = $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
$keyDateControl->addRule('...')
|
||||
->addContitionOn('...');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -26,7 +26,7 @@ class SomeClass
|
||||
public function run()
|
||||
{
|
||||
$form = new Form();
|
||||
$form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
$keyDateControl = $form['key'] = new \Nextras\FormComponents\Controls\DateControl('Label');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user