add case with type form (#3874)

This commit is contained in:
Tomas Votruba 2020-08-02 14:58:14 +02:00 committed by GitHub
parent bb6a53fef0
commit b99a3cbdb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Rector\NetteCodeQuality\FormControlTypeResolver;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\TypeWithClassName;
use Rector\NetteCodeQuality\Contract\FormControlTypeResolverInterface;
use Rector\NetteCodeQuality\Contract\MethodNamesByInputNamesResolverAwareInterface;
use Rector\NetteCodeQuality\NodeResolver\MethodNamesByInputNamesResolver;
use Rector\NodeCollector\NodeFinder\FunctionLikeParsedNodesFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
final class VariableConstructorFormControlTypeResolver implements FormControlTypeResolverInterface, MethodNamesByInputNamesResolverAwareInterface
{
/**
* @var MethodNamesByInputNamesResolver
*/
private $methodNamesByInputNamesResolver;
/**
* @var NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @var FunctionLikeParsedNodesFinder
*/
private $functionLikeParsedNodesFinder;
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;
public function __construct(
NodeTypeResolver $nodeTypeResolver,
NodeNameResolver $nodeNameResolver,
FunctionLikeParsedNodesFinder $functionLikeParsedNodesFinder
) {
$this->nodeTypeResolver = $nodeTypeResolver;
$this->functionLikeParsedNodesFinder = $functionLikeParsedNodesFinder;
$this->nodeNameResolver = $nodeNameResolver;
}
/**
* @return array<string, string>
*/
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;
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ChangeFormArrayAccessToAnnotatedControlVariableRector\Fixture;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
final class ParentVarConstruct extends Control
{
public function run()
{
/** @var ContestForm $contestForm */
$contestForm = $this['whatever'];
$contestForm['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');
}
}
?>
-----
<?php
namespace Rector\NetteCodeQuality\Tests\Rector\ArrayDimFetch\ChangeFormArrayAccessToAnnotatedControlVariableRector\Fixture;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
final class ParentVarConstruct extends Control
{
public function run()
{
/** @var ContestForm $contestForm */
$contestForm = $this['whatever'];
/** @var \Nette\Forms\Controls\SelectBox $notFoundControl */
$notFoundControl = $contestForm['not_found'];
$notFoundControl->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');
}
}
?>