From 0c64e0a3ab5e4127fb42aa54bc5766ba4ba34dd7 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 26 Jul 2020 22:37:47 +0200 Subject: [PATCH] add skip assign expr --- ...eldClassMethodToArrayClassMethodRector.php | 4 +++- ...AccessToAnnotatedControlVariableRector.php | 15 +++++++++++++ .../Fixture/skip_already.php.inc | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector/Fixture/skip_already.php.inc diff --git a/rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php b/rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php index 87949d0bbac..afc138bad34 100644 --- a/rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php +++ b/rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php @@ -66,7 +66,9 @@ class SomeEventSubscriber implements EventSubscriberInterface PHP , [ - 'EventSubscriberInterface' => ['getSubscribedEvents'], + '$methodsByType' => [ + 'EventSubscriberInterface' => ['getSubscribedEvents'], + ], ] ), ]); diff --git a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php index b9987e2881b..2a3ae25e80a 100644 --- a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php +++ b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php @@ -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; @@ -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; + } } diff --git a/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector/Fixture/skip_already.php.inc b/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector/Fixture/skip_already.php.inc new file mode 100644 index 00000000000..6be93222eca --- /dev/null +++ b/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector/Fixture/skip_already.php.inc @@ -0,0 +1,22 @@ +isSubmitted()) { + } + } + + protected function createComponentSomeForm() + { + return new Form(); + } +}