-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
UseTransformerTest.php
174 lines (153 loc) · 3.44 KB
/
UseTransformerTest.php
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
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\UseTransformer
*/
final class UseTransformerTest extends AbstractTransformerTestCase
{
/**
* @param array<int, int> $expectedTokens index => kind
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
T_USE,
CT::T_USE_LAMBDA,
CT::T_USE_TRAIT,
]
);
}
public function provideProcessCases(): iterable
{
yield [
'<?php use Foo;',
[
1 => T_USE,
],
];
yield [
'<?php $foo = function() use ($bar) {};',
[
9 => CT::T_USE_LAMBDA,
],
];
yield [
'<?php class Foo { use Bar; }',
[
7 => CT::T_USE_TRAIT,
],
];
yield [
'<?php namespace Aaa; use Bbb; class Foo { use Bar; function baz() { $a=1; return function () use ($a) {}; } }',
[
6 => T_USE,
17 => CT::T_USE_TRAIT,
42 => CT::T_USE_LAMBDA,
],
];
yield [
'<?php
namespace A {
class Foo {}
echo Foo::class;
}
namespace B {
use \stdClass;
echo 123;
}',
[
30 => T_USE,
],
];
yield [
'<?php use Foo; $a = Bar::class;',
[
1 => T_USE,
],
];
yield 'nested anonymous classes' => [
'<?php
namespace SomeWhereOverTheRainbow;
trait Foo {
public function test()
{
$a = time();
return function() use ($a) { echo $a; };
}
};
$a = new class(
new class() {
use Foo;
}
) {
public function __construct($bar)
{
$a = $bar->test();
$a();
}
};
',
[
38 => CT::T_USE_LAMBDA,
76 => CT::T_USE_TRAIT,
],
];
yield [
'<?php
use A\{B,};
use function D;
use C\{D,E,};
',
[
1 => T_USE,
11 => T_USE,
18 => T_USE,
],
];
}
/**
* @param array<int, int> $expectedTokens index => kind
*
* @requires PHP 8.1
* @dataProvider provideProcessPhp81Cases
*/
public function testProcessPhp81(string $source, array $expectedTokens = []): void
{
$this->doTest($source, $expectedTokens, [CT::T_USE_TRAIT]);
}
public function provideProcessPhp81Cases(): iterable
{
yield [
'<?php enum Foo: string
{
use Bar;
case Test1 = "a";
}
',
[
10 => CT::T_USE_TRAIT,
],
];
}
}