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
5 changes: 5 additions & 0 deletions src/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
use PHPStan\PhpDocParser\Parser\TokenIterator;
use function array_keys;
use function array_map;
use function array_values;
use function assert;
use function count;
use function get_class;
Expand Down Expand Up @@ -598,6 +599,10 @@ private function printConstExpr(ConstExprNode $node): string
*/
private function printArrayFormatPreserving(array $nodes, array $originalNodes, TokenIterator $originalTokens, int &$tokenIndex, string $parentNodeClass, string $subNodeName): ?string
{
// unset() leaves gaps in the keys, the differ reads the lists by position
$nodes = array_values($nodes);
$originalNodes = array_values($originalNodes);

$diff = $this->differ->diffWithReplacements($originalNodes, $nodes);
$mapKey = $parentNodeClass . '->' . $subNodeName;
$insertStr = $this->listInsertionMap[$mapKey] ?? null;
Expand Down
47 changes: 47 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2899,6 +2899,44 @@ public function enterNode(Node $node)

},
];

$unsetSecond = new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof PhpDocNode) {
unset($node->children[1]);
}

if ($node instanceof UnionTypeNode) {
unset($node->types[1]);
}

return $node;
}

};

yield [
'/** @var A|B|C */',
'/** @var A|C */',
$unsetSecond,
];

yield [
self::nowdoc('
/**
* @param Foo $foo
* @param Bar $bar
* @param Baz $baz
*/'),
self::nowdoc('
/**
* @param Foo $foo
* @param Baz $baz
*/'),
$unsetSecond,
];
}

/**
Expand Down Expand Up @@ -2941,6 +2979,15 @@ public function enterNode(Node $node)
$node->setAttribute(Attribute::ORIGINAL_NODE, null);
$node->setAttribute(Attribute::COMMENTS, null);

// the printed result is compared with a node that can have gaps in its lists
if ($node instanceof PhpDocNode) {
$node->children = array_values($node->children);
}

if ($node instanceof UnionTypeNode) {
$node->types = array_values($node->types);
}

return $node;
}

Expand Down
Loading