mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 22:08:00 +01:00
refactor arra dim fetch to exclusive rules
This commit is contained in:
parent
e647b8f003
commit
955f7a7e76
@ -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 [];
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ PHP
|
||||
return null;
|
||||
}
|
||||
|
||||
$controlName = $this->controlDimFetchAnalyzer->matchName($node);
|
||||
$controlName = $this->controlDimFetchAnalyzer->matchNameOnControlVariable($node);
|
||||
if ($controlName === null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ PHP
|
||||
return null;
|
||||
}
|
||||
|
||||
$inputName = $this->controlDimFetchAnalyzer->matchName($node);
|
||||
$inputName = $this->controlDimFetchAnalyzer->matchNameOnFormOrControlVariable($node);
|
||||
if ($inputName === null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user