Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5941477

Browse files
authored
Fix printing nodes
* Add `toString()` tests for all nodes * Fix printing array shapes * Improve callable type printing * Add missing function import
1 parent 950bddf commit 5941477

File tree

6 files changed

+608
-48
lines changed

6 files changed

+608
-48
lines changed

‎src/Ast/Type/ArrayShapeNode.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __toString(): string
2727
{
2828
$items = $this->items;
2929

30-
if ($this->sealed) {
30+
if (! $this->sealed) {
3131
$items[] = '...';
3232
}
3333

‎src/Ast/Type/CallableTypeParameterNode.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\PhpDocParser\Ast\Node;
66
use PHPStan\PhpDocParser\Ast\NodeAttributes;
7+
use function trim;
78

89
class CallableTypeParameterNode implements Node
910
{
@@ -41,7 +42,7 @@ public function __toString(): string
4142
$isReference = $this->isReference ? '&' : '';
4243
$isVariadic = $this->isVariadic ? '...' : '';
4344
$default = $this->isOptional ? ' = default' : '';
44-
return "{$type}{$isReference}{$isVariadic}{$this->parameterName}{$default}";
45+
return trim("{$type}{$isReference}{$isVariadic}{$this->parameterName}") . $default;
4546
}
4647

4748
}

‎tests/PHPStan/Ast/PhpDoc/NodePrintTest.php‎

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDocParser\Ast\ToString;
4+
5+
use Generator;
6+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayItemNode;
7+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
8+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
9+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFloatNode;
10+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
11+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode;
12+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
13+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
14+
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
15+
use PHPStan\PhpDocParser\Ast\Node;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class ConstExprToStringTest extends TestCase
19+
{
20+
21+
/**
22+
* @dataProvider provideConstExprCases
23+
*/
24+
public function testToString(string $expected, Node $node): void
25+
{
26+
$this->assertSame($expected, (string) $node);
27+
}
28+
29+
public static function provideConstExprCases(): Generator
30+
{
31+
yield from [
32+
['null', new ConstExprNullNode()],
33+
['true', new ConstExprTrueNode()],
34+
['false', new ConstExprFalseNode()],
35+
['8', new ConstExprIntegerNode('8')],
36+
['21.37', new ConstExprFloatNode('21.37')],
37+
['foo', new ConstExprStringNode('foo')],
38+
['FooBar', new ConstFetchNode('', 'FooBar')],
39+
['Foo\\Bar::Baz', new ConstFetchNode('Foo\\Bar', 'Baz')],
40+
['[]', new ConstExprArrayNode([])],
41+
[
42+
'[foo, 4 => foo, bar => baz]',
43+
new ConstExprArrayNode([
44+
new ConstExprArrayItemNode(null, new ConstExprStringNode('foo')),
45+
new ConstExprArrayItemNode(new ConstExprIntegerNode('4'), new ConstExprStringNode('foo')),
46+
new ConstExprArrayItemNode(new ConstExprStringNode('bar'), new ConstExprStringNode('baz')),
47+
]),
48+
],
49+
];
50+
}
51+
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /