refactor arra dim fetch to exclusive rules

This commit is contained in:
TomasVotruba 2020-07-27 13:16:53 +02:00
parent e647b8f003
commit 955f7a7e76
5 changed files with 57 additions and 7 deletions

View File

@ -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 [];
}

View File

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

View File

@ -96,7 +96,7 @@ PHP
return null;
}
$controlName = $this->controlDimFetchAnalyzer->matchName($node);
$controlName = $this->controlDimFetchAnalyzer->matchNameOnControlVariable($node);
if ($controlName === null) {
return null;
}

View File

@ -87,7 +87,7 @@ PHP
return null;
}
$inputName = $this->controlDimFetchAnalyzer->matchName($node);
$inputName = $this->controlDimFetchAnalyzer->matchNameOnFormOrControlVariable($node);
if ($inputName === null) {
return null;
}

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ChangeFormArrayAccessToAnnotatedControlVariableRector\Fixture;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
class SkipControl extends Control
{
public function run()
{
$this['some_form']->values();
}
public function createComponentSomeForm(): Form
{
return new Form();
}
}