mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 09:42:45 +01:00
[Doctrine] add ClassMetadataFactory typehint change
This commit is contained in:
parent
1abfbfd5a6
commit
aa8833a6c1
@ -4,6 +4,7 @@ namespace Rector\NodeTypeResolver\PerNodeTypeResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\NodeTypeResolver\Contract\PerNodeTypeResolver\PerNodeTypeResolverInterface;
|
||||
|
||||
@ -30,6 +31,13 @@ final class ClassLikeTypeResolver implements PerNodeTypeResolverInterface
|
||||
*/
|
||||
public function resolve(Node $classLikeNode): array
|
||||
{
|
||||
return $this->classAnalyzer->resolveTypeAndParentTypes($classLikeNode);
|
||||
$types = $this->classAnalyzer->resolveTypeAndParentTypes($classLikeNode);
|
||||
|
||||
if (! $types) {
|
||||
return [];
|
||||
}
|
||||
$classLikeNode->setAttribute(Attribute::TYPES, $types);
|
||||
|
||||
return $types;
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ final class MethodArgumentAnalyzer
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var MethodCall $node */
|
||||
if (! $node->args[0]->value instanceof String_) {
|
||||
return false;
|
||||
}
|
||||
|
@ -4,11 +4,12 @@ namespace Rector\Rector\Dynamic;
|
||||
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\BetterReflection\Reflection\TypeAnalyzer;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
@ -37,17 +38,17 @@ final class ParentTypehintedArgumentRector extends AbstractRector
|
||||
private $typehintForArgumentByMethodAndClass = [];
|
||||
|
||||
/**
|
||||
* @var ClassAnalyzer
|
||||
* @var TypeAnalyzer
|
||||
*/
|
||||
private $classAnalyzer;
|
||||
private $typeAnalyzer;
|
||||
|
||||
/**
|
||||
* @param mixed[] $typehintForArgumentByMethodAndClass
|
||||
*/
|
||||
public function __construct(array $typehintForArgumentByMethodAndClass, ClassAnalyzer $classAnalyzer)
|
||||
public function __construct(array $typehintForArgumentByMethodAndClass, TypeAnalyzer $typeAnalyzer)
|
||||
{
|
||||
$this->typehintForArgumentByMethodAndClass = $typehintForArgumentByMethodAndClass;
|
||||
$this->classAnalyzer = $classAnalyzer;
|
||||
$this->typeAnalyzer = $typeAnalyzer;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
@ -59,9 +60,12 @@ final class ParentTypehintedArgumentRector extends AbstractRector
|
||||
/** @var Class_ $classNode */
|
||||
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
|
||||
|
||||
$parentTypes = $this->classAnalyzer->resolveTypeAndParentTypes($classNode);
|
||||
$classNodeTypes = $classNode->getAttribute(Attribute::TYPES);
|
||||
if (! $classNodeTypes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isTypeMatch($parentTypes);
|
||||
return $this->isTypeMatch($classNodeTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,9 +76,9 @@ final class ParentTypehintedArgumentRector extends AbstractRector
|
||||
/** @var Class_ $classMethodNode */
|
||||
$classNode = $classMethodNode->getAttribute(Attribute::CLASS_NODE);
|
||||
|
||||
$classParentTypes = $this->classAnalyzer->resolveTypeAndParentTypes($classNode);
|
||||
$classNodeTypes = $classNode->getAttribute(Attribute::TYPES);
|
||||
|
||||
$matchingTypes = $this->getMatchingTypesForClassNode($classParentTypes);
|
||||
$matchingTypes = $this->getMatchingTypesForClassNode($classNodeTypes);
|
||||
|
||||
$methodName = $classMethodNode->name->toString();
|
||||
|
||||
@ -127,13 +131,18 @@ final class ParentTypehintedArgumentRector extends AbstractRector
|
||||
foreach ($classMethodNode->params as $param) {
|
||||
$parameterName = $param->var->name;
|
||||
|
||||
|
||||
if (! isset($parametersToTypehints[$parameterName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newTypehint = $parametersToTypehints[$parameterName];
|
||||
|
||||
$param->type = BuilderHelpers::normalizeType($newTypehint);
|
||||
if ($this->typeAnalyzer->isBuiltinType($newTypehint)) {
|
||||
$param->type = BuilderHelpers::normalizeType($newTypehint);
|
||||
} else {
|
||||
$param->type = new FullyQualified($newTypehint);
|
||||
}
|
||||
}
|
||||
|
||||
return $classMethodNode;
|
||||
|
@ -9,9 +9,14 @@ final class Test extends AbstractConfigurableRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
// $this->doTestFileMatchesExpectedContent(
|
||||
// __DIR__ . '/wrong/wrong.php.inc',
|
||||
// __DIR__ . '/correct/correct.php.inc'
|
||||
// );
|
||||
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/wrong/wrong.php.inc',
|
||||
__DIR__ . '/correct/correct.php.inc'
|
||||
__DIR__ . '/wrong/wrong2.php.inc',
|
||||
__DIR__ . '/correct/correct2.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -3,3 +3,6 @@ rectors:
|
||||
'PhpParser\Parser':
|
||||
'parse':
|
||||
'code': 'string'
|
||||
'Doctrine\ORM\Mapping\ClassMetadataFactory':
|
||||
'setEntityManager':
|
||||
'em': 'Doctrine\ORM\EntityManagerInterface'
|
||||
|
@ -1,8 +1,13 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
class SomeClass implements \PhpParser\Parser
|
||||
namespace SomeNamespace;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
||||
class MyMetadataFactory extends \Doctrine\ORM\Mapping\ClassMetadataFactory
|
||||
{
|
||||
public function parse(string $code, \PhpParser\ErrorHandler $errorHandler = null)
|
||||
public function setEntityManager(\Doctrine\ORM\EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,13 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
class SomeClass implements \PhpParser\Parser
|
||||
namespace SomeNamespace;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
||||
class MyMetadataFactory extends \Doctrine\ORM\Mapping\ClassMetadataFactory
|
||||
{
|
||||
public function parse($code, \PhpParser\ErrorHandler $errorHandler = null)
|
||||
public function setEntityManager(EntityManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user