mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 22:08:00 +01:00
Merge pull request #3796 from rectorphp/control-add-switch
[Nette] Add control assign unique per variable
This commit is contained in:
commit
03de26f5da
@ -66,7 +66,9 @@ class SomeEventSubscriber implements EventSubscriberInterface
|
||||
PHP
|
||||
,
|
||||
[
|
||||
'EventSubscriberInterface' => ['getSubscribedEvents'],
|
||||
'$methodsByType' => [
|
||||
'EventSubscriberInterface' => ['getSubscribedEvents'],
|
||||
],
|
||||
]
|
||||
),
|
||||
]);
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\NetteCodeQuality\Rector\ArrayDimFetch;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
@ -127,6 +128,10 @@ PHP
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($this->isBeingAssigned($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$controlName = $this->controlDimFetchAnalyzer->matchName($node);
|
||||
if ($controlName === null) {
|
||||
return null;
|
||||
@ -143,8 +148,8 @@ PHP
|
||||
private function createAssignExpression(string $variableName, ArrayDimFetch $arrayDimFetch): Expression
|
||||
{
|
||||
$variable = new Variable($variableName);
|
||||
$assignedDimFetch = clone $arrayDimFetch;
|
||||
$assign = new Assign($variable, $assignedDimFetch);
|
||||
$assignedArrayDimFetch = clone $arrayDimFetch;
|
||||
$assign = new Assign($variable, $assignedArrayDimFetch);
|
||||
|
||||
return new Expression($assign);
|
||||
}
|
||||
@ -157,7 +162,7 @@ PHP
|
||||
/** @var ClassMethod|null $classMethod */
|
||||
$classMethod = $arrayDimFetch->getAttribute(AttributeKey::METHOD_NODE);
|
||||
if ($classMethod !== null) {
|
||||
$classMethodObjectHash = spl_object_hash($classMethod);
|
||||
$classMethodObjectHash = spl_object_hash($classMethod) . $variableName;
|
||||
if (in_array($classMethodObjectHash, $this->alreadyInitializedAssignsClassMethodObjectHashes, true)) {
|
||||
return;
|
||||
}
|
||||
@ -192,4 +197,14 @@ PHP
|
||||
|
||||
return new ObjectType($controlType);
|
||||
}
|
||||
|
||||
private function isBeingAssigned(Expr $expr): bool
|
||||
{
|
||||
$parent = $expr->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parent instanceof Assign) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $parent->expr === $expr;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ChangeControlArrayAccessToAnnotatedControlVariableRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Presenter;
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
final class InSwitchPresenter extends Presenter
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
switch ($value) {
|
||||
case 'form':
|
||||
return $this['some_form']->getValues();
|
||||
case 'another_form':
|
||||
return $this['another_form']->getValues();
|
||||
}
|
||||
}
|
||||
|
||||
protected function createComponentSomeForm()
|
||||
{
|
||||
return new Form();
|
||||
}
|
||||
|
||||
protected function createComponentAnotherForm()
|
||||
{
|
||||
return new Form();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ChangeControlArrayAccessToAnnotatedControlVariableRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Presenter;
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
final class InSwitchPresenter extends Presenter
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
switch ($value) {
|
||||
case 'form':
|
||||
/** @var \Nette\Application\UI\Form $someForm */
|
||||
$someForm = $this['some_form'];
|
||||
return $someForm->getValues();
|
||||
case 'another_form':
|
||||
/** @var \Nette\Application\UI\Form $anotherForm */
|
||||
$anotherForm = $this['another_form'];
|
||||
return $anotherForm->getValues();
|
||||
}
|
||||
}
|
||||
|
||||
protected function createComponentSomeForm()
|
||||
{
|
||||
return new Form();
|
||||
}
|
||||
|
||||
protected function createComponentAnotherForm()
|
||||
{
|
||||
return new Form();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ChangeControlArrayAccessToAnnotatedControlVariableRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Presenter;
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
final class SkipAlreadyPresenter extends Presenter
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
/** @var \Nette\Application\UI\Form $someForm */
|
||||
$someForm = $this['some_form'];
|
||||
if ($someForm->isSubmitted()) {
|
||||
}
|
||||
}
|
||||
|
||||
protected function createComponentSomeForm()
|
||||
{
|
||||
return new Form();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user