remove getDoctrine*() methods from PhpDocInfo, use getByType()… (#1935)

remove getDoctrine*() methods from PhpDocInfo, use getByType() instead
This commit is contained in:
Tomáš Votruba 2019-08-31 14:00:48 +02:00 committed by GitHub
commit 4379b4a04b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 78 additions and 66 deletions

View File

@ -6,6 +6,9 @@ namespace PHPSTORM_META;
// $container->get(Type::class) → instance of "Type"
override(\Psr\Container\ContainerInterface::get(0), type(0));
// $propertyPhpDocInfo->getByType(Type::class) → instance of "Type"|null - @todo how to make this nullable?
override(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo::getByType(0), type(0));
// PhpStorm 2019.1 - add argument autocomplete
// https://blog.jetbrains.com/phpstorm/2019/02/new-phpstorm-meta-php-features/
expectedArguments(

View File

@ -5,6 +5,7 @@ namespace Rector\Architecture\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Architecture\Tests\Rector\Class_\RemoveRepositoryFromEntityAnnotationRector\RemoveRepositoryFromEntityAnnotationRectorTest;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Class_\EntityTagValueNode;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
@ -73,7 +74,7 @@ CODE_SAMPLE
$phpDocInfo = $this->docBlockManipulator->createPhpDocInfoFromNode($node);
$doctrineEntityTag = $phpDocInfo->getDoctrineEntity();
$doctrineEntityTag = $phpDocInfo->getByType(EntityTagValueNode::class);
if ($doctrineEntityTag === null) {
return null;
}

View File

@ -13,14 +13,6 @@ use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareReturnTagValue
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareVarTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\Attribute;
use Rector\BetterPhpDocParser\Attributes\Contract\Ast\AttributeAwareNodeInterface;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Class_\EntityTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\IdTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\JoinColumnTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ManyToManyTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ManyToOneTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\OneToManyTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\OneToOneTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\TableTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
final class PhpDocInfo
@ -176,46 +168,6 @@ final class PhpDocInfo
return $this->getResolvedTypesAttribute($varTagValue);
}
public function getDoctrineId(): ?IdTagValueNode
{
return $this->getByType(IdTagValueNode::class);
}
public function getDoctrineTable(): ?TableTagValueNode
{
return $this->getByType(TableTagValueNode::class);
}
public function getDoctrineManyToMany(): ?ManyToManyTagValueNode
{
return $this->getByType(ManyToManyTagValueNode::class);
}
public function getDoctrineManyToOne(): ?ManyToOneTagValueNode
{
return $this->getByType(ManyToOneTagValueNode::class);
}
public function getDoctrineOneToOne(): ?OneToOneTagValueNode
{
return $this->getByType(OneToOneTagValueNode::class);
}
public function getDoctrineOneToMany(): ?OneToManyTagValueNode
{
return $this->getByType(OneToManyTagValueNode::class);
}
public function getDoctrineEntity(): ?EntityTagValueNode
{
return $this->getByType(EntityTagValueNode::class);
}
public function getDoctrineJoinColumnTagValueNode(): ?JoinColumnTagValueNode
{
return $this->getByType(JoinColumnTagValueNode::class);
}
/**
* @return string[]
*/
@ -257,10 +209,7 @@ final class PhpDocInfo
public function getDoctrineRelationTagValueNode(): ?DoctrineRelationTagValueNodeInterface
{
return $this->getDoctrineManyToMany() ??
$this->getDoctrineOneToMany() ??
$this->getDoctrineOneToOne() ??
$this->getDoctrineManyToOne() ?? null;
return $this->getByType(DoctrineRelationTagValueNodeInterface::class);
}
public function removeTagValueNodeFromNode(PhpDocTagValueNode $phpDocTagValueNode): void

View File

@ -6,7 +6,9 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Class_\EntityTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ColumnTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\IdTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\TableTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
@ -30,7 +32,7 @@ final class DoctrineDocBlockResolver
return false;
}
return (bool) $classPhpDocInfo->getDoctrineEntity();
return (bool) $classPhpDocInfo->getByType(EntityTagValueNode::class);
}
public function isDoctrineEntityClassWithIdProperty(Class_ $class): bool
@ -69,7 +71,7 @@ final class DoctrineDocBlockResolver
return false;
}
return (bool) $propertyPhpDocInfo->getDoctrineId();
return (bool) $propertyPhpDocInfo->getByType(IdTagValueNode::class);
}
public function getDoctrineRelationTagValueNode(Property $property): ?DoctrineRelationTagValueNodeInterface
@ -89,7 +91,7 @@ final class DoctrineDocBlockResolver
return null;
}
return $classPhpDocInfo->getDoctrineTable();
return $classPhpDocInfo->getByType(TableTagValueNode::class);
}
public function isDoctrineProperty(Property $property): bool

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Doctrine\PhpDocParser\DoctrineDocBlockResolver;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ColumnTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\IdTagValueNode;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\PhpParser\Node\Resolver\NameResolver;
@ -111,12 +112,11 @@ final class EntityWithMissingUuidProvider
{
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
$idTagValueNode = $propertyPhpDocInfo->getDoctrineId();
$idTagValueNode = $propertyPhpDocInfo->getByType(IdTagValueNode::class);
if ($idTagValueNode === null) {
return false;
}
/** @var ColumnTagValueNode|null $columnTagValueNode */
$columnTagValueNode = $propertyPhpDocInfo->getByType(ColumnTagValueNode::class);
if ($columnTagValueNode === null) {
return false;

View File

@ -14,6 +14,7 @@ use Rector\Doctrine\Collector\UuidMigrationDataCollector;
use Rector\Doctrine\PhpDocParser\Ast\PhpDoc\PhpDocTagNodeFactory;
use Rector\Doctrine\Provider\EntityWithMissingUuidProvider;
use Rector\Doctrine\Uuid\JoinTableNameResolver;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\JoinColumnTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\ToManyTagNodeInterface;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\ToOneTagNodeInterface;
@ -148,8 +149,7 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
private function refactorToManyPropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo, Property $property): void
{
$doctrineJoinColumnTagValueNode = $propertyPhpDocInfo->getDoctrineJoinColumnTagValueNode();
$doctrineJoinColumnTagValueNode = $propertyPhpDocInfo->getByType(JoinColumnTagValueNode::class);
if ($doctrineJoinColumnTagValueNode) {
// replace @ORM\JoinColumn with @ORM\JoinTable
$propertyPhpDocInfo->removeTagValueNodeFromNode($doctrineJoinColumnTagValueNode);
@ -161,7 +161,7 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
private function refactorToOnePropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo): void
{
$joinColumnTagValueNode = $propertyPhpDocInfo->getDoctrineJoinColumnTagValueNode();
$joinColumnTagValueNode = $propertyPhpDocInfo->getByType(JoinColumnTagValueNode::class);
if ($joinColumnTagValueNode) {
$joinColumnTagValueNode->changeNullable(true);

View File

@ -134,7 +134,6 @@ CODE_SAMPLE
/** @var PhpDocInfo $classMethodPhpDocInfo */
$classMethodPhpDocInfo = $this->getPhpDocInfo($classMethod);
/** @var TemplateTagValueNode|null $templateTagValueNode */
$templateTagValueNode = $classMethodPhpDocInfo->getByType(TemplateTagValueNode::class);
if ($templateTagValueNode === null) {
throw new ShouldNotHappenException(__METHOD__);

View File

@ -70,13 +70,17 @@ final class DoctrineColumnPropertyTypeInferer implements PropertyTypeInfererInte
$phpDocInfo = $this->docBlockManipulator->createPhpDocInfoFromNode($property);
/** @var ColumnTagValueNode|null $doctrineColumnTagValueNode */
$doctrineColumnTagValueNode = $phpDocInfo->getByType(ColumnTagValueNode::class);
if ($doctrineColumnTagValueNode === null) {
return [];
}
$scalarType = $this->doctrineTypeToScalarType[$doctrineColumnTagValueNode->getType()] ?? null;
$type = $doctrineColumnTagValueNode->getType();
if ($type === null) {
return [];
}
$scalarType = $this->doctrineTypeToScalarType[$type] ?? null;
if ($scalarType === null) {
return [];
}

View File

@ -44,7 +44,7 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
if ($relationTagValueNode instanceof ToManyTagNodeInterface) {
return $this->processToManyRelation($relationTagValueNode);
} elseif ($relationTagValueNode instanceof ToOneTagNodeInterface) {
$joinColumnTagValueNode = $phpDocInfo->getDoctrineJoinColumnTagValueNode();
$joinColumnTagValueNode = $phpDocInfo->getByType(JoinColumnTagValueNode::class);
return $this->processToOneRelation($relationTagValueNode, $joinColumnTagValueNode);
}

View File

@ -1,7 +1,7 @@
services:
- Rector\PHPStanExtensions\Utils\PHPStanValueResolver
# $node->geAttribute($1) => Type|null by $1
# $node->getAttribute($1) => Type|null by $1
- { class: Rector\PHPStanExtensions\Rector\Type\GetAttributeReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
# $nameResolver->getName() => in some cases always string
@ -11,3 +11,6 @@ services:
# $betterNodeFinder->findByInstance(..., $1) => $1[]
- { class: Rector\PHPStanExtensions\Rector\Type\BetterNodeFinderReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }
# $phpDocInfo->getByType($1) => Type|null by $1
- { class: Rector\PHPStanExtensions\Rector\Type\PhpDocInfoGetByTypeReturnTypeExtension, tags: [phpstan.broker.dynamicMethodReturnTypeExtension] }

View File

@ -0,0 +1,51 @@
<?php declare(strict_types=1);
namespace Rector\PHPStanExtensions\Rector\Type;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
final class PhpDocInfoGetByTypeReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return PhpDocInfo::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getByType';
}
public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type {
$returnType = $this->resolveArgumentValue($methodCall->args[0]->value);
return new UnionType([new ObjectType($returnType), new NullType()]);
}
private function resolveArgumentValue(Expr $expr): ?string
{
if ($expr instanceof ClassConstFetch) {
if ((string) $expr->name !== 'class') {
return null;
}
return $expr->class->toString();
}
return null;
}
}