From b99a3cbdb2652ae2f35f6067ae4187e5eef26edd Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 2 Aug 2020 14:58:14 +0200 Subject: [PATCH] add case with type form (#3874) --- ...ableConstructorFormControlTypeResolver.php | 87 +++++++++++++++++++ .../Fixture/parent_var_construct.php.inc | 59 +++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 rules/nette-code-quality/src/FormControlTypeResolver/VariableConstructorFormControlTypeResolver.php create mode 100644 rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/parent_var_construct.php.inc diff --git a/rules/nette-code-quality/src/FormControlTypeResolver/VariableConstructorFormControlTypeResolver.php b/rules/nette-code-quality/src/FormControlTypeResolver/VariableConstructorFormControlTypeResolver.php new file mode 100644 index 00000000000..367f5176622 --- /dev/null +++ b/rules/nette-code-quality/src/FormControlTypeResolver/VariableConstructorFormControlTypeResolver.php @@ -0,0 +1,87 @@ +nodeTypeResolver = $nodeTypeResolver; + $this->functionLikeParsedNodesFinder = $functionLikeParsedNodesFinder; + $this->nodeNameResolver = $nodeNameResolver; + } + + /** + * @return array + */ + public function resolve(Node $node): array + { + if (! $node instanceof Variable) { + return []; + } + + // handled else-where + if ($this->nodeNameResolver->isName($node, 'this')) { + return []; + } + + $formType = $this->nodeTypeResolver->getStaticType($node); + if (! $formType instanceof TypeWithClassName) { + return []; + } + + if (! is_a($formType->getClassName(), 'Nette\Application\UI\Form', true)) { + return []; + } + + $constructorClassMethod = $this->functionLikeParsedNodesFinder->findClassMethod( + '__construct', + $formType->getClassName() + ); + if ($constructorClassMethod === null) { + return []; + } + + return $this->methodNamesByInputNamesResolver->resolveExpr($constructorClassMethod); + } + + public function setResolver(MethodNamesByInputNamesResolver $methodNamesByInputNamesResolver): void + { + $this->methodNamesByInputNamesResolver = $methodNamesByInputNamesResolver; + } +} diff --git a/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/parent_var_construct.php.inc b/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/parent_var_construct.php.inc new file mode 100644 index 00000000000..476592da4d6 --- /dev/null +++ b/rules/nette-code-quality/tests/Rector/ArrayDimFetch/ChangeFormArrayAccessToAnnotatedControlVariableRector/Fixture/parent_var_construct.php.inc @@ -0,0 +1,59 @@ +setItems([1, 2, 3]); + } +} + +final class ContestForm extends Form +{ + public function __construct(?IContainer $parent = null, $name = null) + { + parent::__construct($parent, $name); + + $this->addSelect('not_found'); + } +} + +?> +----- +setItems([1, 2, 3]); + } +} + +final class ContestForm extends Form +{ + public function __construct(?IContainer $parent = null, $name = null) + { + parent::__construct($parent, $name); + + $this->addSelect('not_found'); + } +} + +?>