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
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 @@ -11,7 +11,6 @@
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\TagAwareNodeInterface;
use Rector\BetterPhpDocParser\Utils\ArrayItemStaticHelper;
use Rector\Core\Exception\ShouldNotHappenException;

abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpDocTagValueNode
{
Expand Down Expand Up @@ -93,7 +92,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 +118,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 @@ -202,40 +204,6 @@ protected function resolveOriginalContentSpacingAndOrder(?string $originalConten
$this->isSilentKeyExplicit = (bool) Strings::contains($originalContent, sprintf('%s=', $silentKey));
}

protected function printValueWithOptionalQuotes(?string $key = null, ...$values): string
{
// pick first non-null value
foreach ($values as $value) {
if ($value === null) {
continue;
}

break;
}

if (! isset($value)) {
throw new ShouldNotHappenException();
}

if (is_array($value)) {
return $this->printArrayItem($value, $key);
}

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

// quote by default
if (! isset($this->keysByQuotedStatus[$key]) || (isset($this->keysByQuotedStatus[$key]) && $this->keysByQuotedStatus[$key])) {
// probably constant - no quote
if (Strings::match($value, '#\w+::\w+#')) {
return $keyPart . $value;
}

return sprintf('%s"%s"', $keyPart, $value);
}

return $keyPart . $value;
}

protected function filterOutMissingItems(array $contentItems): array
{
if ($this->orderedVisibleItems === null) {
Expand All @@ -247,7 +215,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,30 @@ 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($this->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 +51,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