Skip to content

Commit

Permalink
Fix printing nodes
Browse files Browse the repository at this point in the history
* Add `toString()` tests for all nodes

* Fix printing array shapes

* Improve callable type printing

* Add missing function import
  • Loading branch information
jrmajor authored Dec 16, 2022
1 parent 950bddf commit 5941477
Show file tree
Hide file tree
Showing 6 changed files with 608 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/Ast/Type/ArrayShapeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __toString(): string
{
$items = $this->items;

if ($this->sealed) {
if (! $this->sealed) {
$items[] = '...';
}

Expand Down
3 changes: 2 additions & 1 deletion src/Ast/Type/CallableTypeParameterNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use function trim;

class CallableTypeParameterNode implements Node
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public function __toString(): string
$isReference = $this->isReference ? '&' : '';
$isVariadic = $this->isVariadic ? '...' : '';
$default = $this->isOptional ? ' = default' : '';
return "{$type}{$isReference}{$isVariadic}{$this->parameterName}{$default}";
return trim("{$type}{$isReference}{$isVariadic}{$this->parameterName}") . $default;
}

}
46 changes: 0 additions & 46 deletions tests/PHPStan/Ast/PhpDoc/NodePrintTest.php

This file was deleted.

52 changes: 52 additions & 0 deletions tests/PHPStan/Ast/ToString/ConstExprToStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Ast\ToString;

use Generator;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFloatNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPUnit\Framework\TestCase;

class ConstExprToStringTest extends TestCase
{

/**
* @dataProvider provideConstExprCases
*/
public function testToString(string $expected, Node $node): void
{
$this->assertSame($expected, (string) $node);
}

public static function provideConstExprCases(): Generator
{
yield from [
['null', new ConstExprNullNode()],
['true', new ConstExprTrueNode()],
['false', new ConstExprFalseNode()],
['8', new ConstExprIntegerNode('8')],
['21.37', new ConstExprFloatNode('21.37')],
['foo', new ConstExprStringNode('foo')],
['FooBar', new ConstFetchNode('', 'FooBar')],
['Foo\\Bar::Baz', new ConstFetchNode('Foo\\Bar', 'Baz')],
['[]', new ConstExprArrayNode([])],
[
'[foo, 4 => foo, bar => baz]',
new ConstExprArrayNode([
new ConstExprArrayItemNode(null, new ConstExprStringNode('foo')),
new ConstExprArrayItemNode(new ConstExprIntegerNode('4'), new ConstExprStringNode('foo')),
new ConstExprArrayItemNode(new ConstExprStringNode('bar'), new ConstExprStringNode('baz')),
]),
],
];
}

}
Loading

0 comments on commit 5941477

Please sign in to comment.