Merge pull request #3784 from rectorphp/improve-date-control

[Nette] Improve AddDatePickerToDateControlRector
This commit is contained in:
Tomas Votruba 2020-07-25 16:27:44 +02:00 committed by GitHub
commit d8b829a582
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 11 deletions

View File

@ -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');
}
}
```

View File

@ -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);
}
}

View File

@ -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('...');
}
}

View File

@ -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('...');
}
}
?>

View File

@ -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');
}
}