Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function refactor(Node $node): ?Node

$phpDocInfo = $this->docBlockManipulator->createPhpDocInfoFromNode($node);

$doctrineEntityTag = $phpDocInfo->getDoctrineEntityTag();
$doctrineEntityTag = $phpDocInfo->getDoctrineEntity();
if ($doctrineEntityTag === null) {
return null;
}
Expand Down
44 changes: 22 additions & 22 deletions packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,49 +177,49 @@ public function getVarTypes(): array
return $this->getResolvedTypesAttribute($varTagValue);
}

public function getDoctrineIdTagValueNode(): ?IdTagValueNode
public function getDoctrineId(): ?IdTagValueNode
{
return $this->matchChildValueNodeOfType(IdTagValueNode::class);
return $this->getByType(IdTagValueNode::class);
}

public function getDoctrineTableTagValueNode(): ?TableTagValueNode
public function getDoctrineTable(): ?TableTagValueNode
{
return $this->matchChildValueNodeOfType(TableTagValueNode::class);
return $this->getByType(TableTagValueNode::class);
}

public function getDoctrineManyToManyTagValueNode(): ?ManyToManyTagValueNode
public function getDoctrineManyToMany(): ?ManyToManyTagValueNode
{
return $this->matchChildValueNodeOfType(ManyToManyTagValueNode::class);
return $this->getByType(ManyToManyTagValueNode::class);
}

public function getDoctrineManyToOneTagValueNode(): ?ManyToOneTagValueNode
public function getDoctrineManyToOne(): ?ManyToOneTagValueNode
{
return $this->matchChildValueNodeOfType(ManyToOneTagValueNode::class);
return $this->getByType(ManyToOneTagValueNode::class);
}

public function getDoctrineOneToOneTagValueNode(): ?OneToOneTagValueNode
public function getDoctrineOneToOne(): ?OneToOneTagValueNode
{
return $this->matchChildValueNodeOfType(OneToOneTagValueNode::class);
return $this->getByType(OneToOneTagValueNode::class);
}

public function getDoctrineOneToManyTagValueNode(): ?OneToManyTagValueNode
public function getDoctrineOneToMany(): ?OneToManyTagValueNode
{
return $this->matchChildValueNodeOfType(OneToManyTagValueNode::class);
return $this->getByType(OneToManyTagValueNode::class);
}

public function getDoctrineEntityTag(): ?EntityTagValueNode
public function getDoctrineEntity(): ?EntityTagValueNode
{
return $this->matchChildValueNodeOfType(EntityTagValueNode::class);
return $this->getByType(EntityTagValueNode::class);
}

public function getDoctrineColumnTagValueNode(): ?ColumnTagValueNode
public function getDoctrineColumn(): ?ColumnTagValueNode
{
return $this->matchChildValueNodeOfType(ColumnTagValueNode::class);
return $this->getByType(ColumnTagValueNode::class);
}

public function getDoctrineJoinColumnTagValueNode(): ?JoinColumnTagValueNode
{
return $this->matchChildValueNodeOfType(JoinColumnTagValueNode::class);
return $this->getByType(JoinColumnTagValueNode::class);
}

/**
Expand Down Expand Up @@ -263,10 +263,10 @@ public function getReturnTypes(): array

public function getDoctrineRelationTagValueNode(): ?DoctrineRelationTagValueNodeInterface
{
return $this->getDoctrineManyToManyTagValueNode() ??
$this->getDoctrineOneToManyTagValueNode() ??
$this->getDoctrineOneToOneTagValueNode() ??
$this->getDoctrineManyToOneTagValueNode() ?? null;
return $this->getDoctrineManyToMany() ??
$this->getDoctrineOneToMany() ??
$this->getDoctrineOneToOne() ??
$this->getDoctrineManyToOne() ?? null;
}

public function removeTagValueNodeFromNode(PhpDocTagValueNode $phpDocTagValueNode): void
Expand All @@ -285,7 +285,7 @@ public function removeTagValueNodeFromNode(PhpDocTagValueNode $phpDocTagValueNod
/**
* @param string $type
*/
public function matchChildValueNodeOfType(string $type): ?PhpDocTagValueNode
public function getByType(string $type): ?PhpDocTagValueNode
{
foreach ($this->phpDocNode->children as $phpDocChildNode) {
if ($phpDocChildNode instanceof PhpDocTagNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Attributes\Contract\Ast\AttributeAwareNodeInterface;
use Rector\DoctrinePhpDocParser\Array_\ArrayItemStaticHelper;

abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpDocTagValueNode
{
use AttributeTrait;

/**
* @var string[]|null
*/
protected $orderedVisibleItems;

/**
* @param mixed[] $item
*/
Expand All @@ -21,6 +27,51 @@ protected function printArrayItem(array $item, string $key): string
$json = Strings::replace($json, '#,#', ', ');
$json = Strings::replace($json, '#\[(.*?)\]#', '{$1}');

// cleanup json encoded extra slashes
$json = Strings::replace($json, '#\\\\\\\\#', '\\');

return sprintf('%s=%s', $key, $json);
}

/**
* @param string[] $contentItems
*/
protected function printContentItems(array $contentItems): string
{
if ($this->orderedVisibleItems !== null) {
$contentItems = ArrayItemStaticHelper::filterAndSortVisibleItems($contentItems, $this->orderedVisibleItems);
}

if ($contentItems === []) {
return '';
}

return '(' . implode(', ', $contentItems) . ')';
}

/**
* @param PhpDocTagValueNode[] $tagValueNodes
*/
protected function printTagValueNodesSeparatedByComma(array $tagValueNodes, string $prefix = ''): string
{
if ($tagValueNodes === []) {
return '';
}

$itemsAsStrings = [];
foreach ($tagValueNodes as $tagValueNode) {
$itemsAsStrings[] = $prefix . (string) $tagValueNode;
}

return implode(', ', $itemsAsStrings);
}

protected function resolveItemsOrderFromAnnotationContent(?string $annotationContent): void
{
if ($annotationContent === null) {
return;
}

$this->orderedVisibleItems = ArrayItemStaticHelper::resolveAnnotationItemsOrder($annotationContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function parseTag(TokenIterator $tokenIterator): PhpDocTagNode
$tokenIterator->next();

// @todo somehow decouple to tag pre-processor
if ($tag === '@ORM') {
if (Strings::match($tag, '#@(ORM|Assert|Serializer)$#')) {
$tag .= $tokenIterator->currentTokenValue();
$tokenIterator->next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function isDoctrineEntityClass(Class_ $class): bool
return false;
}

return (bool) $classPhpDocInfo->getDoctrineEntityTag();
return (bool) $classPhpDocInfo->getDoctrineEntity();
}

public function getTargetEntity(Property $property): ?string
Expand All @@ -49,7 +49,7 @@ public function hasPropertyDoctrineIdTag(Property $property): bool
return false;
}

return (bool) $propertyPhpDocInfo->getDoctrineIdTagValueNode();
return (bool) $propertyPhpDocInfo->getDoctrineId();
}

public function getDoctrineRelationTagValueNode(Property $property): ?DoctrineRelationTagValueNodeInterface
Expand All @@ -69,7 +69,7 @@ public function getDoctrineTableTagValueNode(Class_ $class): ?TableTagValueNode
return null;
}

return $classPhpDocInfo->getDoctrineTableTagValueNode();
return $classPhpDocInfo->getDoctrineTable();
}

public function isDoctrineProperty(Property $property): bool
Expand All @@ -79,7 +79,7 @@ public function isDoctrineProperty(Property $property): bool
return false;
}

if ($propertyPhpDocInfo->getDoctrineColumnTagValueNode()) {
if ($propertyPhpDocInfo->getDoctrineColumn()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ private function hasClassIdPropertyWithUuidType(Class_ $class): bool
return false;
}

$idTagValueNode = $propertyPhpDocInfo->getDoctrineIdTagValueNode();
$idTagValueNode = $propertyPhpDocInfo->getDoctrineId();
if ($idTagValueNode === null) {
return false;
}

// get column!
$columnTagValueNode = $propertyPhpDocInfo->getDoctrineColumnTagValueNode();
$columnTagValueNode = $propertyPhpDocInfo->getDoctrineColumn();
if ($columnTagValueNode === null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ReflectionMethod;
use ReflectionProperty;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Validator\Constraint;

final class NodeAnnotationReader
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public function readMethodAnnotation(ClassMethod $classMethod, string $annotatio
return $this->annotationReader->getMethodAnnotation($reflectionMethod, $annotationClassName);
}

public function readDoctrineClassAnnotation(Class_ $class, string $annotationClassName): Annotation
public function readClassAnnotation(Class_ $class, string $annotationClassName): Annotation
{
$classReflection = $this->createClassReflectionFromNode($class);

Expand All @@ -59,7 +60,10 @@ public function readDoctrineClassAnnotation(Class_ $class, string $annotationCla
return $classAnnotation;
}

public function readDoctrinePropertyAnnotation(Property $property, string $annotationClassName): Annotation
/**
* @return Annotation|Constraint|null
*/
public function readPropertyAnnotation(Property $property, string $annotationClassName)
{
$propertyReflection = $this->createPropertyReflectionFromPropertyNode($property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,9 @@

namespace Rector\DoctrinePhpDocParser\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use Rector\BetterPhpDocParser\PhpDocParser\Ast\PhpDoc\AbstractTagValueNode;
use Rector\DoctrinePhpDocParser\Array_\ArrayItemStaticHelper;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineTagNodeInterface;

abstract class AbstractDoctrineTagValueNode extends AbstractTagValueNode implements DoctrineTagNodeInterface
{
/**
* @var string[]|null
*/
protected $orderedVisibleItems;

/**
* @param string[] $contentItems
*/
protected function printContentItems(array $contentItems): string
{
if ($this->orderedVisibleItems !== null) {
$contentItems = ArrayItemStaticHelper::filterAndSortVisibleItems($contentItems, $this->orderedVisibleItems);
}

if ($contentItems === []) {
return '';
}

return '(' . implode(', ', $contentItems) . ')';
}

/**
* @param PhpDocTagValueNode[] $tagValueNodes
*/
protected function printTagValueNodesSeparatedByComma(array $tagValueNodes, string $prefix = ''): string
{
if ($tagValueNodes === []) {
return '';
}

$itemsAsStrings = [];
foreach ($tagValueNodes as $tagValueNode) {
$itemsAsStrings[] = $prefix . (string) $tagValueNode;
}

return implode(', ', $itemsAsStrings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ public function removeInversedBy(): void
{
$this->inversedBy = null;
}

public function changeTargetEntity(string $targetEntity): void
{
$this->targetEntity = $targetEntity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ public function removeInversedBy(): void
{
$this->inversedBy = null;
}

public function changeTargetEntity(string $targetEntity): void
{
$this->targetEntity = $targetEntity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ public function removeMappedBy(): void
{
$this->mappedBy = null;
}

public function changeTargetEntity(string $targetEntity): void
{
$this->targetEntity = $targetEntity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,9 @@ public function removeMappedBy(): void
{
$this->mappedBy = null;
}

public function changeTargetEntity(string $targetEntity): void
{
$this->targetEntity = $targetEntity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ interface DoctrineRelationTagValueNodeInterface
public function getTargetEntity(): ?string;

public function getFqnTargetEntity(): ?string;

public function changeTargetEntity(string $targetEntity): void;
}
Loading