rector/packages/BetterPhpDocParser/ValueObject/PhpDoc/DoctrineAnnotation/CurlyListNode.php
Tomas Votruba 7d0f151a40 Updated Rector to commit a2cd7283fbf2d6b2904016c51e3f4a545caa0256
a2cd7283fb Typo fix comment php 7.3 compat on rector workflow (#3432)
2023-03-01 13:00:30 +00:00

51 lines
1.4 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Stringable;
use RectorPrefix202303\Webmozart\Assert\Assert;
final class CurlyListNode extends \Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\AbstractValuesAwareNode
{
/**
* @var ArrayItemNode[]
* @readonly
*/
private $arrayItemNodes = [];
/**
* @param ArrayItemNode[] $arrayItemNodes
*/
public function __construct(array $arrayItemNodes = [])
{
$this->arrayItemNodes = $arrayItemNodes;
Assert::allIsInstanceOf($this->arrayItemNodes, ArrayItemNode::class);
parent::__construct($this->arrayItemNodes);
}
public function __toString() : string
{
// possibly list items
return $this->implode($this->values);
}
/**
* @param mixed[] $array
*/
private function implode(array $array) : string
{
$itemContents = '';
\end($array);
$lastItemKey = \key($array);
foreach ($array as $key => $value) {
if (\is_int($key)) {
$itemContents .= (string) $value;
} else {
$itemContents .= $key . '=' . $value;
}
if ($lastItemKey !== $key) {
$itemContents .= ', ';
}
}
return '{' . $itemContents . '}';
}
}