Skip to content

Commit

Permalink
[BCB] ArrayShapeItemNode and ObjectShapeItemNode are not standalo…
Browse files Browse the repository at this point in the history
…ne TypeNode, just Node
  • Loading branch information
ondrejmirtes committed Sep 15, 2024
1 parent 5504a3b commit 0fe292d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ The line with `some text in the middle` in phpdoc-parser 2.0 is now part of the
* Constructor parameter `$isReference` in `TypelessParamTagValueNode` made required
* Constructor parameter `$templateTypes` in `CallableTypeNode` made required
* Constructor parameters `$expectedTokenValue` and `$currentTokenLine` in `ParserException` made required
* `ArrayShapeItemNode` and `ObjectShapeItemNode` are not standalone TypeNode, just Node
3 changes: 2 additions & 1 deletion src/Ast/Type/ArrayShapeItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use function sprintf;

class ArrayShapeItemNode implements TypeNode
class ArrayShapeItemNode implements Node
{

use NodeAttributes;
Expand Down
3 changes: 2 additions & 1 deletion src/Ast/Type/ObjectShapeItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace PHPStan\PhpDocParser\Ast\Type;

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use function sprintf;

class ObjectShapeItemNode implements TypeNode
class ObjectShapeItemNode implements Node
{

use NodeAttributes;
Expand Down
52 changes: 26 additions & 26 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,30 @@ function (PhpDocChildNode $child): string {
if ($node instanceof DoctrineArrayItem) {
return (string) $node;
}
if ($node instanceof ArrayShapeItemNode) {
if ($node->keyName !== null) {
return sprintf(
'%s%s: %s',
$this->print($node->keyName),
$node->optional ? '?' : '',
$this->printType($node->valueType),
);
}

return $this->printType($node->valueType);
}
if ($node instanceof ObjectShapeItemNode) {
if ($node->keyName !== null) {
return sprintf(
'%s%s: %s',
$this->print($node->keyName),
$node->optional ? '?' : '',
$this->printType($node->valueType),
);
}

return $this->printType($node->valueType);
}

throw new LogicException(sprintf('Unknown node type %s', get_class($node)));
}
Expand Down Expand Up @@ -364,26 +388,14 @@ private function printTagValue(PhpDocTagValueNode $node): string
private function printType(TypeNode $node): string
{
if ($node instanceof ArrayShapeNode) {
$items = array_map(fn (ArrayShapeItemNode $item): string => $this->printType($item), $node->items);
$items = array_map(fn (ArrayShapeItemNode $item): string => $this->print($item), $node->items);

if (! $node->sealed) {
$items[] = '...' . ($node->unsealedType === null ? '' : $this->print($node->unsealedType));
}

return $node->kind . '{' . implode(', ', $items) . '}';
}
if ($node instanceof ArrayShapeItemNode) {
if ($node->keyName !== null) {
return sprintf(
'%s%s: %s',
$this->print($node->keyName),
$node->optional ? '?' : '',
$this->printType($node->valueType),
);
}

return $this->printType($node->valueType);
}
if ($node instanceof ArrayTypeNode) {
return $this->printOffsetAccessType($node->type) . '[]';
}
Expand Down Expand Up @@ -469,22 +481,10 @@ private function printType(TypeNode $node): string
return '?' . $this->printType($node->type);
}
if ($node instanceof ObjectShapeNode) {
$items = array_map(fn (ObjectShapeItemNode $item): string => $this->printType($item), $node->items);
$items = array_map(fn (ObjectShapeItemNode $item): string => $this->print($item), $node->items);

return 'object{' . implode(', ', $items) . '}';
}
if ($node instanceof ObjectShapeItemNode) {
if ($node->keyName !== null) {
return sprintf(
'%s%s: %s',
$this->print($node->keyName),
$node->optional ? '?' : '',
$this->printType($node->valueType),
);
}

return $this->printType($node->valueType);
}
if ($node instanceof OffsetAccessTypeNode) {
return $this->printOffsetAccessType($node->type) . '[' . $this->printType($node->offset) . ']';
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2888,7 +2888,7 @@ public function dataLinesAndIndexes(): iterable
6,
],
[
static fn (ArrayShapeNode $typeNode): TypeNode => $typeNode->items[0],
static fn (ArrayShapeNode $typeNode): Node => $typeNode->items[0],
'foo: int',
1,
1,
Expand Down Expand Up @@ -2924,7 +2924,7 @@ public function dataLinesAndIndexes(): iterable
6,
],
[
static fn (ObjectShapeNode $typeNode): TypeNode => $typeNode->items[0],
static fn (ObjectShapeNode $typeNode): Node => $typeNode->items[0],
'foo: int',
1,
1,
Expand Down

0 comments on commit 0fe292d

Please sign in to comment.