Merge pull request #3805 from rectorphp/naming-fixes

[Nette] dim fetch naming fixes
This commit is contained in:
kodiakhq[bot] 2020-07-27 12:48:56 +00:00 committed by GitHub
commit e09bd79c72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 6 deletions

View File

@ -21,6 +21,6 @@ final class NetteControlNaming
public function createCreateComponentClassMethodName(string $shortName): string
{
return 'createComponent' . ucfirst($this->createVariableName($shortName));
return 'createComponent' . StaticRectorStrings::underscoreToCamelCase($shortName);
}
}

View File

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

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();
}
}