Skip to content

Commit

Permalink
TagValueNode refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 29, 2020
1 parent 130d4cf commit cfc92d0
Show file tree
Hide file tree
Showing 39 changed files with 309 additions and 815 deletions.
2 changes: 2 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ parameters:
- 'src/Testing/PHPUnit/PHPUnitEnvironment.php'
# honesty first
- 'src/*Static*.php'
# static ctor
- 'packages/better-php-doc-parser/src/PhpDocNode/**/*TagValueNode.php'

Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer:
- 'packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface DoctrineRelationTagValueNodeInterface extends PhpDocTagValueNode
{
public function getTargetEntity(): ?string;

public function getFqnTargetEntity(): ?string;
public function getFullyQualifiedTargetEntity(): ?string;

public function changeTargetEntity(string $targetEntity): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function printArrayItem(array $item, ?string $key = null): string

$keyPart = $this->createKeyPart($key);

// should unqote
// should unquote
if ($this->isValueWithoutQuotes($key)) {
// @todo resolve per key item
$json = Strings::replace($json, '#"#');
Expand All @@ -119,6 +119,9 @@ protected function printContentItems(array $items): string
{
$items = $this->filterOutMissingItems($items);

// remove null values
$items = array_filter($items);

if ($items === []) {
if ($this->originalContent !== null && Strings::endsWith($this->originalContent, '()')) {
return '()';
Expand Down Expand Up @@ -247,7 +250,7 @@ protected function filterOutMissingItems(array $contentItems): array

private function createKeyPart(?string $key = null): string
{
if ($key === null || $key === '') {
if (empty($key)) {
return '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_;

use Doctrine\ORM\Mapping\Entity;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\AbstractDoctrineTagValueNode;
use Rector\PhpAttribute\Contract\PhpAttributableTagNodeInterface;
use Rector\PhpAttribute\PhpDocNode\PhpAttributePhpDocNodePrintTrait;
Expand All @@ -13,37 +14,31 @@ final class EntityTagValueNode extends AbstractDoctrineTagValueNode implements P
use PhpAttributePhpDocNodePrintTrait;

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

/**
* @var bool|null
*/
private $readOnly;

public function __construct(
?string $repositoryClass = null,
?bool $readOnly = null,
?string $originalContent = null
) {
$this->repositoryClass = $repositoryClass;
$this->readOnly = $readOnly;
public function __construct(?Entity $entity = null, ?string $originalContent = null)
{
if ($entity !== null) {
$this->items = get_object_vars($entity);
}

$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

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

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

public function removeRepositoryClass(): void
{
$this->repositoryClass = null;
$this->items['repositoryClass'] = null;
}

public function getShortName(): string
Expand All @@ -57,26 +52,21 @@ public function toAttributeString(): string
$items = $this->filterOutMissingItems($items);

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

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

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

if ($this->repositoryClass !== null) {
if ($printType === self::PRINT_TYPE_ATTRIBUTE) {
$items['repositoryClass'] = $this->repositoryClass . '::class';
} else {
$items['repositoryClass'] = $this->printValueWithOptionalQuotes(null, $this->repositoryClass);
if ($printType === self::PRINT_TYPE_ATTRIBUTE) {
if ($items['repositoryClass'] !== null) {
$items['repositoryClass'] .= '::class';
}
}

if ($this->readOnly !== null) {
if ($printType === self::PRINT_TYPE_ATTRIBUTE) {
$items['readOnly'] = $this->readOnly ? 'ORM\Entity::READ_ONLY' : '';
} else {
$items['readOnly'] = $this->readOnly ? 'true' : 'false';
if ($items['readOnly'] !== null) {
$items['readOnly'] = $items['readOnly'] ? 'ORM\Entity::READ_ONLY' : '';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ final class IndexTagValueNode extends AbstractDoctrineTagValueNode implements Ta
public function __construct(Index $index, ?string $originalContent = null, ?string $originalTag = null)
{
$this->items = get_object_vars($index);

if ($originalContent !== null) {
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

$this->tag = $originalTag;

$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

public function __toString(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@

namespace Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_;

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

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

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

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

public function __toString(): string
{
if ($this->value === null) {
return '';
}

$items['value'] = $this->value;
$items = $this->completeItemsQuotes($items);
$items = $this->completeItemsQuotes($this->items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public function __construct(
$this->uniqueConstraints = $uniqueConstraints;
$this->options = $options;

if ($originalContent !== null) {
$this->resolveOriginalContentSpacingAndOrder($originalContent, 'name');
}
$this->resolveOriginalContentSpacingAndOrder($originalContent, 'name');

$this->haveIndexesFinalComma = $haveIndexesFinalComma;
$this->haveUniqueConstraintsFinalComma = $haveUniqueConstraintsFinalComma;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ public function __construct(
?string $originalTag = null
) {
$this->items = get_object_vars($uniqueConstraint);

if ($originalContent !== null) {
$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

$this->tag = $originalTag;

$this->resolveOriginalContentSpacingAndOrder($originalContent);
}

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

$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
Expand Down
Loading

0 comments on commit cfc92d0

Please sign in to comment.