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 @@ -4,11 +4,45 @@

namespace Rector\AttributeAwarePhpDoc\Ast\Type;

use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;

final class AttributeAwareArrayShapeItemNode extends ArrayShapeItemNode implements AttributeAwareNodeInterface
{
use AttributeTrait;

/**
* @var bool
*/
private $hasSpaceAfterDoubleColon = false;

/**
* @param ConstExprIntegerNode|IdentifierTypeNode|null $keyName
*/
public function __construct($keyName, bool $optional, TypeNode $typeNode, string $docComment = '')
{
parent::__construct($keyName, $optional, $typeNode);

$this->hasSpaceAfterDoubleColon = (bool) Strings::match($docComment, '#\:\s+#');
}

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

return sprintf(
'%s%s:%s%s',
(string) $this->keyName,
$this->optional ? '?' : '',
$this->hasSpaceAfterDoubleColon ? ' ' : '',
(string) $this->valueType
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareArrayShapeItemNode;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeAwareNodeFactoryAwareInterface;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeNodeAwareFactoryInterface;
use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;

final class AttributeAwareArrayShapeItemNodeFactory implements AttributeNodeAwareFactoryInterface
final class AttributeAwareArrayShapeItemNodeFactory implements AttributeNodeAwareFactoryInterface, AttributeAwareNodeFactoryAwareInterface
{
/**
* @var AttributeAwareNodeFactory
*/
private $attributeAwareNodeFactory;

public function getOriginalNodeClass(): string
{
return ArrayShapeItemNode::class;
Expand All @@ -27,6 +34,13 @@ public function isMatch(Node $node): bool
*/
public function create(Node $node, string $docContent): AttributeAwareNodeInterface
{
return new AttributeAwareArrayShapeItemNode($node->keyName, $node->optional, $node->valueType);
$node->valueType = $this->attributeAwareNodeFactory->createFromNode($node->valueType, $docContent);

return new AttributeAwareArrayShapeItemNode($node->keyName, $node->optional, $node->valueType, $docContent);
}

public function setAttributeAwareNodeFactory(AttributeAwareNodeFactory $attributeAwareNodeFactory): void
{
$this->attributeAwareNodeFactory = $attributeAwareNodeFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public function create(Node $node, string $docContent): AttributeAwareNodeInterf
{
$items = [];
foreach ($node->items as $item) {
$item->valueType = $this->attributeAwareNodeFactory->createFromNode($item->valueType, $docContent);
$items[] = $item;
$items[] = $this->attributeAwareNodeFactory->createFromNode($item, $docContent);
}

return new AttributeAwareArrayShapeNode($items);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* normal view template will be rendered.
*
* @var array{serialize:string|bool|null}
*/