Skip to content

Commit

Permalink
Merge pull request #3280 from rectorphp/tag-value-node
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Apr 30, 2020
2 parents 4d1810f + 170a925 commit 0bbfd8b
Show file tree
Hide file tree
Showing 56 changed files with 120 additions and 606 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpD
use PrintTagValueNodeTrait;

/**
* @var string
* @var mixed[]
*/
protected const PRINT_TYPE_ATTRIBUTE = 'print_attribute';

/**
* @var string
*/
protected const PRINT_TYPE_ANNOTATION = 'print_annotation';
protected $items = [];

/**
* @var bool
Expand Down Expand Up @@ -72,6 +67,16 @@ abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpD
*/
private $keysByQuotedStatus = [];

/**
* Generic fallback
*/
public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);
return $this->printContentItems($items);
}

/**
* @param mixed[] $item
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

namespace Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_;

use Doctrine\ORM\Mapping\Embeddable;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\AbstractDoctrineTagValueNode;

final class EmbeddableTagValueNode extends AbstractDoctrineTagValueNode
{
public function __construct(string $originalContent)
public function __construct(Embeddable $embeddable, string $originalContent)
{
$this->items = get_object_vars($embeddable);
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
return $this->originalContent;
}

public function getShortName(): string
{
return '@ORM\Embeddable';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ final class EntityTagValueNode extends AbstractDoctrineTagValueNode implements P
{
use PhpAttributePhpDocNodePrintTrait;

/**
* @var mixed[]
*/
private $items = [];

public function __construct(?Entity $entity = null, ?string $originalContent = null)
{
if ($entity !== null) {
Expand All @@ -27,14 +22,6 @@ public function __construct(?Entity $entity = null, ?string $originalContent = n
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public function removeRepositoryClass(): void
{
$this->items['repositoryClass'] = null;
Expand All @@ -47,26 +34,24 @@ public function getShortName(): string

public function toAttributeString(): string
{
$items = $this->createItems(self::PRINT_TYPE_ATTRIBUTE);
$items = $this->createAttributeItems();
$items = $this->filterOutMissingItems($items);

$content = $this->printPhpAttributeItems($items);

return $this->printAttributeContent($content);
}

private function createItems(string $printType = self::PRINT_TYPE_ANNOTATION): array
private function createAttributeItems(): array
{
$items = $this->items;

if ($printType === self::PRINT_TYPE_ATTRIBUTE) {
if ($items['repositoryClass'] !== null) {
$items['repositoryClass'] .= '::class';
}
if ($items['repositoryClass'] !== null) {
$items['repositoryClass'] .= '::class';
}

if ($items['readOnly'] !== null) {
$items['readOnly'] = $items['readOnly'] ? 'ORM\Entity::READ_ONLY' : '';
}
if ($items['readOnly'] !== null) {
$items['readOnly'] = $items['readOnly'] ? 'ORM\Entity::READ_ONLY' : '';
}

return $items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ final class IndexTagValueNode extends AbstractDoctrineTagValueNode implements Ta
*/
private $tag;

/**
* @var mixed[]
*/
private $items = [];

public function __construct(Index $index, ?string $originalContent = null, ?string $originalTag = null)
{
$this->items = get_object_vars($index);
Expand All @@ -28,13 +23,6 @@ public function __construct(Index $index, ?string $originalContent = null, ?stri
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);
return $this->printContentItems($items);
}

public function getTag(): ?string
{
return $this->tag ?: $this->getShortName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,13 @@

final class InheritanceTypeTagValueNode extends AbstractDoctrineTagValueNode
{
/**
* @var mixed[]
*/
private $items = [];

public function __construct(InheritanceType $inheritanceType, ?string $originalContent)
{
$this->items = get_object_vars($inheritanceType);

$this->resolveOriginalContentSpacingAndOrder($originalContent, 'value');
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public function getShortName(): string
{
return '@ORM\InheritanceType';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ final class UniqueConstraintTagValueNode extends AbstractDoctrineTagValueNode im
*/
private $tag;

/**
* @var mixed[]
*/
private $items = [];

public function __construct(
UniqueConstraint $uniqueConstraint,
?string $originalContent = null,
Expand All @@ -31,14 +26,6 @@ public function __construct(
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public function getTag(): ?string
{
return $this->tag ?: $this->getShortName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,15 @@ final class ColumnTagValueNode extends AbstractDoctrineTagValueNode implements P
{
use PhpAttributePhpDocNodePrintTrait;

/**
* @var mixed[]
*/
private $items = [];

public function __construct($items, ?string $originalContent = null)
{
$this->items = $items;
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public static function fromColumnAndOriginalContent(Column $column, string $originalContent): self
{
$items = get_object_vars($column);

return new self($items, $originalContent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,12 @@

final class CustomIdGeneratorTagValueNode extends AbstractDoctrineTagValueNode
{
/**
* @var mixed[]
*/
private $items = [];

public function __construct(CustomIdGenerator $customIdGenerator, ?string $originalContent = null)
{
$this->items = get_object_vars($customIdGenerator);
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public function getShortName(): string
{
return '@ORM\CustomIdGenerator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,17 @@ final class GeneratedValueTagValueNode extends AbstractDoctrineTagValueNode impl
{
use PhpAttributePhpDocNodePrintTrait;

/**
* @var mixed[]
*/
private $items = [];

public function __construct(array $items, ?string $annotationContent = null)
{
$this->items = $items;
$this->resolveOriginalContentSpacingAndOrder($annotationContent, 'strategy');
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public static function createFromAnnotationAndAnnotationContent(
public static function createFromAnnotationAndContent(
GeneratedValue $generatedValue,
string $annotationContent
): self {
$items = get_object_vars($generatedValue);

return new self($items, $annotationContent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ public function __construct(?string $annotationContent = null)
$this->resolveOriginalContentSpacingAndOrder($annotationContent);
}

public function __toString(): string
{
$content = '';

if ($this->hasOpeningBracket) {
$content .= '(';
}

if ($this->hasClosingBracket) {
$content .= ')';
}

return $content;
}

public function getShortName(): string
{
return '@ORM\Id';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ final class JoinColumnTagValueNode extends AbstractDoctrineTagValueNode implemen
{
use PhpAttributePhpDocNodePrintTrait;

/**
* @var string|null
*/
private $tag;

/**
* @var string
*/
private $shortName = '@ORM\JoinColumn';

/**
* @var mixed[]
* @var string|null
*/
private $items = [];
private $tag;

public function __construct(array $items, ?string $originalContent = null, ?string $originalTag = null)
{
Expand All @@ -37,14 +32,6 @@ public function __construct(array $items, ?string $originalContent = null, ?stri
$this->tag = $originalTag;
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public static function createFromAnnotationAndOriginalContent(
JoinColumn $joinColumn,
string $originalContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ final class ManyToManyTagValueNode extends AbstractDoctrineTagValueNode implemen
*/
private $fullyQualifiedTargetEntity;

/**
* @var mixed[]
*/
private $items = [];

public function __construct(
array $items,
?string $originalContent = null,
Expand All @@ -36,14 +31,6 @@ public function __construct(
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

public static function createFromAnnotationAndOriginalContent(
ManyToMany $manyToMany,
string $originalContent,
Expand Down Expand Up @@ -96,19 +83,17 @@ public function getShortName(): string

public function toAttributeString(): string
{
$items = $this->createItems(self::PRINT_TYPE_ATTRIBUTE);
$items = $this->createAttributeItems();
$items = $this->filterOutMissingItems($items);

$content = $this->printPhpAttributeItems($items);
return $this->printAttributeContent($content);
}

private function createItems(string $printType = self::PRINT_TYPE_ANNOTATION): array
private function createAttributeItems(): array
{
$items = $this->items;
if ($printType === self::PRINT_TYPE_ATTRIBUTE) {
$items['targetEntity'] .= '::class';
}
$items['targetEntity'] .= '::class';

return $items;
}
Expand Down
Loading

0 comments on commit 0bbfd8b

Please sign in to comment.