1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
<?php declare(strict_types = 1);
namespace PHPStan\PhpDocParser\Ast\ToString;
use Generator;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class TypeToStringTest extends TestCase
{
/**
* @dataProvider provideSimpleCases
* @dataProvider provideArrayCases
* @dataProvider provideCallableCases
* @dataProvider provideGenericCases
* @dataProvider provideConditionalCases
* @dataProvider provideCombinedCases
*/
#[DataProvider('provideSimpleCases')]
#[DataProvider('provideArrayCases')]
#[DataProvider('provideCallableCases')]
#[DataProvider('provideGenericCases')]
#[DataProvider('provideConditionalCases')]
#[DataProvider('provideCombinedCases')]
public function testToString(string $expected, Node $node): void
{
$this->assertSame($expected, (string) $node);
}
public static function provideSimpleCases(): Generator
{
yield from [
['string', new IdentifierTypeNode('string')],
['Foo\\Bar', new IdentifierTypeNode('Foo\\Bar')],
['null', new ConstTypeNode(new ConstExprNullNode())],
['$this', new ThisTypeNode()],
];
}
public static function provideArrayCases(): Generator
{
yield from [
['$this[]', new ArrayTypeNode(new ThisTypeNode())],
['array[int]', new OffsetAccessTypeNode(new IdentifierTypeNode('array'), new IdentifierTypeNode('int'))],
];
yield from [
['array{}', ArrayShapeNode::createSealed([])],
['array{...}', ArrayShapeNode::createUnsealed([], null)],
[
'array{string, int, ...}',
ArrayShapeNode::createUnsealed([
new ArrayShapeItemNode(null, false, new IdentifierTypeNode('string')),
new ArrayShapeItemNode(null, false, new IdentifierTypeNode('int')),
], null),
],
[
'array{\'foo\': Foo, \'bar\'?: Bar, 1: Baz}',
ArrayShapeNode::createSealed([
new ArrayShapeItemNode(new ConstExprStringNode('foo', ConstExprStringNode::SINGLE_QUOTED), false, new IdentifierTypeNode('Foo')),
new ArrayShapeItemNode(new ConstExprStringNode('bar', ConstExprStringNode::SINGLE_QUOTED), true, new IdentifierTypeNode('Bar')),
new ArrayShapeItemNode(new ConstExprIntegerNode('1'), false, new IdentifierTypeNode('Baz')),
]),
],
['list{}', ArrayShapeNode::createSealed([], 'list')],
['list{...}', ArrayShapeNode::createUnsealed([], null, 'list')],
[
'list{string, int, ...}',
ArrayShapeNode::createUnsealed([
new ArrayShapeItemNode(null, false, new IdentifierTypeNode('string')),
new ArrayShapeItemNode(null, false, new IdentifierTypeNode('int')),
], null, 'list'),
],
];
}
public static function provideCallableCases(): Generator
{
yield from [
[
'\\Closure(): string',
new CallableTypeNode(new IdentifierTypeNode('\Closure'), [], new IdentifierTypeNode('string'), []),
],
[
'callable(int, int $foo): void',
new CallableTypeNode(new IdentifierTypeNode('callable'), [
new CallableTypeParameterNode(new IdentifierTypeNode('int'), false, false, '', false),
new CallableTypeParameterNode(new IdentifierTypeNode('int'), false, false, '$foo', false),
], new IdentifierTypeNode('void'), []),
],
[
'callable(int=, int $foo=): void',
new CallableTypeNode(new IdentifierTypeNode('callable'), [
new CallableTypeParameterNode(new IdentifierTypeNode('int'), false, false, '', true),
new CallableTypeParameterNode(new IdentifierTypeNode('int'), false, false, '$foo', true),
], new IdentifierTypeNode('void'), []),
],
[
'callable(int &, int &$foo): void',
new CallableTypeNode(new IdentifierTypeNode('callable'), [
new CallableTypeParameterNode(new IdentifierTypeNode('int'), true, false, '', false),
new CallableTypeParameterNode(new IdentifierTypeNode('int'), true, false, '$foo', false),
], new IdentifierTypeNode('void'), []),
],
[
'callable(string ...$foo): void',
new CallableTypeNode(new IdentifierTypeNode('callable'), [
new CallableTypeParameterNode(new IdentifierTypeNode('string'), false, true, '$foo', false),
], new IdentifierTypeNode('void'), []),
],
];
}
public static function provideGenericCases(): Generator
{
yield from [
[
'array<string>',
new GenericTypeNode(new IdentifierTypeNode('array'), [new IdentifierTypeNode('string')]),
],
[
'array<string, *>',
new GenericTypeNode(
new IdentifierTypeNode('array'),
[new IdentifierTypeNode('string'), new IdentifierTypeNode('int')],
[GenericTypeNode::VARIANCE_INVARIANT, GenericTypeNode::VARIANCE_BIVARIANT],
),
],
[
'Foo\Bar<covariant string, contravariant int>',
new GenericTypeNode(
new IdentifierTypeNode('Foo\\Bar'),
[new IdentifierTypeNode('string'), new IdentifierTypeNode('int')],
[GenericTypeNode::VARIANCE_COVARIANT, GenericTypeNode::VARIANCE_CONTRAVARIANT],
),
],
];
}
public static function provideConditionalCases(): Generator
{
yield from [
[
'(TKey is int ? list<int> : list<string>)',
new ConditionalTypeNode(
new IdentifierTypeNode('TKey'),
new IdentifierTypeNode('int'),
new GenericTypeNode(new IdentifierTypeNode('list'), [new IdentifierTypeNode('int')]),
new GenericTypeNode(new IdentifierTypeNode('list'), [new IdentifierTypeNode('string')]),
false,
),
],
[
'(TValue is not array ? int : int[])',
new ConditionalTypeNode(
new IdentifierTypeNode('TValue'),
new IdentifierTypeNode('array'),
new IdentifierTypeNode('int'),
new ArrayTypeNode(new IdentifierTypeNode('int')),
true,
),
],
[
'($foo is Exception ? never : string)',
new ConditionalTypeForParameterNode(
'$foo',
new IdentifierTypeNode('Exception'),
new IdentifierTypeNode('never'),
new IdentifierTypeNode('string'),
false,
),
],
[
'($foo is not Exception ? string : never)',
new ConditionalTypeForParameterNode(
'$foo',
new IdentifierTypeNode('Exception'),
new IdentifierTypeNode('string'),
new IdentifierTypeNode('never'),
true,
),
],
];
}
public static function provideCombinedCases(): Generator
{
yield from [
['?string', new NullableTypeNode(new IdentifierTypeNode('string'))],
[
'(Foo & Bar)',
new IntersectionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
],
[
'(Foo | Bar)',
new UnionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
],
[
'((Foo & Bar) | Baz)',
new UnionTypeNode([
new IntersectionTypeNode([
new IdentifierTypeNode('Foo'),
new IdentifierTypeNode('Bar'),
]),
new IdentifierTypeNode('Baz'),
]),
],
];
}
}
|