mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 14:27:14 +01:00
cleanup & fixes
This commit is contained in:
parent
f581f7dabe
commit
a9533b7ecb
@ -48,24 +48,12 @@ final class NodeTypeResolver
|
||||
/** @var Scope $nodeScope */
|
||||
$nodeScope = $node->getAttribute(Attribute::SCOPE);
|
||||
|
||||
if ($nodeScope === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($node instanceof Variable) {
|
||||
$variableName = (string) $node->name;
|
||||
|
||||
if ($nodeScope->hasVariableType($variableName) === TrinaryLogic::createYes()) {
|
||||
$type = $nodeScope->getVariableType($variableName);
|
||||
|
||||
// this
|
||||
if ($type instanceof ThisType) {
|
||||
return $this->classReflectionTypesResolver->resolve($nodeScope->getClassReflection());
|
||||
}
|
||||
|
||||
return $this->resolveObjectTypesToStrings($type);
|
||||
}
|
||||
|
||||
// $nodeTypes = $this->perNodeTypeResolvers[Variable::class]->resolve($node);
|
||||
// if (count($nodeTypes)) {
|
||||
// return $nodeTypes;
|
||||
// }
|
||||
return $this->resolveVariableNode($node, $nodeScope);
|
||||
}
|
||||
|
||||
if ($node instanceof Expr) {
|
||||
@ -73,13 +61,11 @@ final class NodeTypeResolver
|
||||
}
|
||||
|
||||
$nodeClass = get_class($node);
|
||||
if (! isset($this->perNodeTypeResolvers[$nodeClass])) {
|
||||
return [];
|
||||
if (isset($this->perNodeTypeResolvers[$nodeClass])) {
|
||||
return $this->perNodeTypeResolvers[$nodeClass]->resolve($node);
|
||||
}
|
||||
|
||||
$nodeTypes = $this->perNodeTypeResolvers[$nodeClass]->resolve($node);
|
||||
|
||||
return $this->cleanPreSlashes($nodeTypes);
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,42 +76,11 @@ final class NodeTypeResolver
|
||||
/** @var Scope $nodeScope */
|
||||
$nodeScope = $exprNode->getAttribute(Attribute::SCOPE);
|
||||
|
||||
$types = [];
|
||||
// @todo decouple - resolve $this
|
||||
// if ($exprNode instanceof Variable && $exprNode->name === 'this') {
|
||||
// $types[] = $nodeScope->getClassReflection()->getName();
|
||||
// $types = array_merge($types, $nodeScope->getClassReflection()->getParentClassesNames());
|
||||
// foreach ($nodeScope->getClassReflection()->getInterfaces() as $classReflection) {
|
||||
// $types[] = $classReflection->getName();
|
||||
// }
|
||||
//
|
||||
// return $types;
|
||||
// }
|
||||
|
||||
$type = $nodeScope->getType($exprNode);
|
||||
|
||||
return $this->resolveObjectTypesToStrings($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* "\FqnType" => "FqnType"
|
||||
*
|
||||
* @param string[] $nodeTypes
|
||||
* @return string[]
|
||||
*/
|
||||
private function cleanPreSlashes(array $nodeTypes): array
|
||||
{
|
||||
foreach ($nodeTypes as $key => $nodeType) {
|
||||
// filter out non-type values
|
||||
if (! is_string($nodeType)) {
|
||||
continue;
|
||||
}
|
||||
$nodeTypes[$key] = ltrim($nodeType, '\\');
|
||||
}
|
||||
|
||||
return $nodeTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
@ -157,4 +112,25 @@ final class NodeTypeResolver
|
||||
|
||||
return $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveVariableNode(Variable $variableNode, Scope $nodeScope): array
|
||||
{
|
||||
$variableName = (string) $variableNode->name;
|
||||
|
||||
if ($nodeScope->hasVariableType($variableName) === TrinaryLogic::createYes()) {
|
||||
$type = $nodeScope->getVariableType($variableName);
|
||||
|
||||
// this
|
||||
if ($type instanceof ThisType) {
|
||||
return $this->classReflectionTypesResolver->resolve($nodeScope->getClassReflection());
|
||||
}
|
||||
|
||||
return $this->resolveObjectTypesToStrings($type);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -23,10 +23,26 @@ final class FormTypeGetParentRector extends AbstractRector
|
||||
*/
|
||||
private $formTypeStringToTypeProvider;
|
||||
|
||||
public function __construct(NodeFactory $nodeFactory, FormTypeStringToTypeProvider $formTypeStringToTypeProvider)
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $abstractTypeClass;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $abstractTypeExtensionClass;
|
||||
|
||||
public function __construct(
|
||||
NodeFactory $nodeFactory,
|
||||
FormTypeStringToTypeProvider $formTypeStringToTypeProvider,
|
||||
string $abstractTypeClass = 'Symfony\Component\Form\AbstractType',
|
||||
string $abstractTypeExtensionClass = 'Symfony\Component\Form\AbstractTypeExtension'
|
||||
) {
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->formTypeStringToTypeProvider = $formTypeStringToTypeProvider;
|
||||
$this->abstractTypeClass = $abstractTypeClass;
|
||||
$this->abstractTypeExtensionClass = $abstractTypeExtensionClass;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
@ -56,11 +72,11 @@ final class FormTypeGetParentRector extends AbstractRector
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isParentTypeAndMethod($node, 'Symfony\Component\Form\AbstractType', 'getParent')) {
|
||||
if ($this->isParentTypeAndMethod($node, $this->abstractTypeClass, 'getParent')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->isParentTypeAndMethod($node, 'Symfony\Component\Form\AbstractTypeExtension', 'getExtendedType');
|
||||
return $this->isParentTypeAndMethod($node, $this->abstractTypeExtensionClass, 'getExtendedType');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -36,8 +36,15 @@ final class OptionNameRector extends AbstractRector
|
||||
{
|
||||
return new RectorDefinition('Turns old option names to new ones in FormTypes in Form in Symfony', [
|
||||
new CodeSample(
|
||||
'$builder->add("...", ["precision" => "...", "virtual" => "..."];',
|
||||
'$builder->add("...", ["scale" => "...", "inherit_data" => "..."];'
|
||||
<<<'CODE_SAMPLE'
|
||||
$builder = new FormBuilder;
|
||||
$builder->add("...", ["precision" => "...", "virtual" => "..."];
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$builder = new FormBuilder;
|
||||
$builder->add("...", ["scale" => "...", "inherit_data" => "..."];
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractType;
|
||||
|
||||
class PermissionCollectionType extends AbstractType
|
||||
{
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
use Symfony\Component\Form\AbstractTypeExtension;
|
||||
use Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractTypeExtension;
|
||||
|
||||
class TypeExtension extends AbstractTypeExtension
|
||||
{
|
||||
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source;
|
||||
|
||||
abstract class AbstractType
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source;
|
||||
|
||||
abstract class AbstractTypeExtension
|
||||
{
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractType;
|
||||
|
||||
class PermissionCollectionType extends AbstractType
|
||||
{
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php declare (strict_types=1);
|
||||
|
||||
use Symfony\Component\Form\AbstractTypeExtension;
|
||||
use Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractTypeExtension;
|
||||
|
||||
class TypeExtension extends AbstractTypeExtension
|
||||
{
|
||||
|
@ -1,2 +1,4 @@
|
||||
services:
|
||||
Rector\Symfony\Rector\Form\FormTypeGetParentRector: ~
|
||||
Rector\Symfony\Rector\Form\FormTypeGetParentRector:
|
||||
$abstractTypeClass: 'Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractType'
|
||||
$abstractTypeExtensionClass: 'Rector\Symfony\Tests\Rector\Form\FormTypeGetParentRector\Source\AbstractTypeExtension'
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Rector\Symfony\Tests\Rector\Form\OptionNameRector\Source\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class RegistrationFormType extends AbstractType
|
||||
|
@ -0,0 +1,8 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Symfony\Tests\Rector\Form\OptionNameRector\Source;
|
||||
|
||||
abstract class AbstractType
|
||||
{
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Rector\Symfony\Tests\Rector\Form\OptionNameRector\Source\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class RegistrationFormType extends AbstractType
|
||||
|
@ -62,6 +62,7 @@ final class PropertyFetchAnalyzer
|
||||
}
|
||||
|
||||
$varNodeTypes = $this->nodeTypeResolver->resolve($node->var);
|
||||
|
||||
if (! array_intersect($types, $varNodeTypes)) {
|
||||
return false;
|
||||
}
|
||||
@ -81,6 +82,7 @@ final class PropertyFetchAnalyzer
|
||||
}
|
||||
|
||||
$varNodeTypes = $this->nodeTypeResolver->resolve($node->var);
|
||||
|
||||
if (! in_array($type, $varNodeTypes, true)) {
|
||||
return false;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user