mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-01 07:03:32 +00:00
Merge pull request #3805 from rectorphp/naming-fixes
[Nette] dim fetch naming fixes
This commit is contained in:
commit
e09bd79c72
|
@ -21,6 +21,6 @@ final class NetteControlNaming
|
|||
|
||||
public function createCreateComponentClassMethodName(string $shortName): string
|
||||
{
|
||||
return 'createComponent' . ucfirst($this->createVariableName($shortName));
|
||||
return 'createComponent' . StaticRectorStrings::underscoreToCamelCase($shortName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\NetteCodeQuality\NodeAnalyzer;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
|
@ -22,13 +23,23 @@ final class ControlDimFetchAnalyzer
|
|||
$this->nodeTypeResolver = $nodeTypeResolver;
|
||||
}
|
||||
|
||||
public function matchNameOnFormOrControlVariable(Node $node): ?string
|
||||
{
|
||||
return $this->matchNameOnVariableTypes($node, ['Nette\Application\UI\Form']);
|
||||
}
|
||||
|
||||
public function matchNameOnControlVariable(Node $node): ?string
|
||||
{
|
||||
return $this->matchNameOnVariableTypes($node, ['Nette\Application\UI\Control']);
|
||||
}
|
||||
|
||||
public function matchName(Node $node): ?string
|
||||
{
|
||||
if (! $node instanceof ArrayDimFetch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isContainerVariable($node->var)) {
|
||||
if (! $this->isVariableTypes($node->var, ['Nette\ComponentModel\IContainer'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -39,12 +50,39 @@ final class ControlDimFetchAnalyzer
|
|||
return $node->dim->value;
|
||||
}
|
||||
|
||||
private function isContainerVariable(Node $node): bool
|
||||
/**
|
||||
* @param string[] $types
|
||||
*/
|
||||
private function matchNameOnVariableTypes(Node $node, array $types): ?string
|
||||
{
|
||||
$matchedName = $this->matchName($node);
|
||||
if ($matchedName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Assign $node */
|
||||
if (! $this->isVariableTypes($node->var, $types)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $matchedName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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…
Reference in New Issue
Block a user