From 955f7a7e76902f72b9cd88e2ed6505cda715675f Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Mon, 27 Jul 2020 13:16:53 +0200 Subject: [PATCH] refactor arra dim fetch to exclusive rules --- .../ArrayDimFetchControlTypeResolver.php | 6 +++- .../NodeAnalyzer/ControlDimFetchAnalyzer.php | 35 ++++++++++++++++--- ...AccessToAnnotatedControlVariableRector.php | 2 +- ...AccessToAnnotatedControlVariableRector.php | 2 +- .../Fixture/skip_control.php.inc | 19 ++++++++++ 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/skip_control.php.inc diff --git a/rules/nette-code-quality/src/FormControlTypeResolver/ArrayDimFetchControlTypeResolver.php b/rules/nette-code-quality/src/FormControlTypeResolver/ArrayDimFetchControlTypeResolver.php index 06856d4efaf..078573c24ff 100644 --- a/rules/nette-code-quality/src/FormControlTypeResolver/ArrayDimFetchControlTypeResolver.php +++ b/rules/nette-code-quality/src/FormControlTypeResolver/ArrayDimFetchControlTypeResolver.php @@ -65,7 +65,11 @@ final class ArrayDimFetchControlTypeResolver implements FormControlTypeResolverI return []; } - $controlShortName = $this->controlDimFetchAnalyzer->matchName($node); + $controlShortName = $this->controlDimFetchAnalyzer->matchNameOnVariableTypes($node, [ + 'Nette\Application\UI\Form', + 'Nette\Application\UI\Control', + ]); + if ($controlShortName === null) { return []; } diff --git a/rules/nette-code-quality/src/NodeAnalyzer/ControlDimFetchAnalyzer.php b/rules/nette-code-quality/src/NodeAnalyzer/ControlDimFetchAnalyzer.php index 967d5ff0929..bdae5b5580e 100644 --- a/rules/nette-code-quality/src/NodeAnalyzer/ControlDimFetchAnalyzer.php +++ b/rules/nette-code-quality/src/NodeAnalyzer/ControlDimFetchAnalyzer.php @@ -22,13 +22,31 @@ final class ControlDimFetchAnalyzer $this->nodeTypeResolver = $nodeTypeResolver; } - public function matchName(Node $node): ?string + public function matchNameOnFormOrControlVariable(Node $node): ?string + { + return $this->matchNameOnVariableTypes($node, ['Nette\Application\UI\Form']); + } + + public function matchNameOnControlVariable(Node $node): ?string + { + $variableName = $this->matchNameOnVariableTypes($node, ['Nette\Application\UI\Control']); + if ($variableName === null) { + return null; + } + + return $variableName; + } + + /** + * @param string[] $types + */ + public function matchNameOnVariableTypes(Node $node, array $types): ?string { if (! $node instanceof ArrayDimFetch) { return null; } - if (! $this->isContainerVariable($node->var)) { + if (! $this->isVariableTypes($node->var, $types)) { return null; } @@ -39,12 +57,21 @@ final class ControlDimFetchAnalyzer return $node->dim->value; } - private function isContainerVariable(Node $node): bool + /** + * @param string[] $types + */ + private function isVariableTypes(Node $node, array $types): bool { if (! $node instanceof Variable) { return false; } - return $this->nodeTypeResolver->isObjectTypeOrNullableObjectType($node, 'Nette\ComponentModel\IContainer'); + foreach ($types as $type) { + if ($this->nodeTypeResolver->isObjectTypeOrNullableObjectType($node, $type)) { + return true; + } + } + + return false; } } diff --git a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php index 0b9a6c41a63..967271a4e58 100644 --- a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php +++ b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeControlArrayAccessToAnnotatedControlVariableRector.php @@ -96,7 +96,7 @@ PHP return null; } - $controlName = $this->controlDimFetchAnalyzer->matchName($node); + $controlName = $this->controlDimFetchAnalyzer->matchNameOnControlVariable($node); if ($controlName === null) { return null; } diff --git a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector.php b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector.php index 09d021bde79..cf6207d53a8 100644 --- a/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector.php +++ b/rules/nette-code-quality/src/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector.php @@ -87,7 +87,7 @@ PHP return null; } - $inputName = $this->controlDimFetchAnalyzer->matchName($node); + $inputName = $this->controlDimFetchAnalyzer->matchNameOnFormOrControlVariable($node); if ($inputName === null) { return null; } diff --git a/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/skip_control.php.inc b/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/skip_control.php.inc new file mode 100644 index 00000000000..d02f7fcdba1 --- /dev/null +++ b/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/skip_control.php.inc @@ -0,0 +1,19 @@ +values(); + } + + public function createComponentSomeForm(): Form + { + return new Form(); + } +}