forked from phpactor/phpactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionLikeWalkerTest.php
243 lines (205 loc) · 7.59 KB
/
FunctionLikeWalkerTest.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
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
238
239
240
241
242
243
<?php
namespace Phpactor\WorseReflection\Tests\Integration\Core\Inference\FrameWalker;
use Phpactor\WorseReflection\Core\Type\IterableType;
use Phpactor\WorseReflection\Tests\Integration\Core\Inference\FrameWalkerTestCase;
use Generator;
use Phpactor\WorseReflection\Core\Inference\Frame;
class FunctionLikeWalkerTest extends FrameWalkerTestCase
{
public function provideWalk(): Generator
{
yield 'It returns this' => [
<<<'EOT'
<?php
namespace Foobar\Barfoo;
use Acme\Factory;
class Foobar
{
public function hello()
{
<>
}
}
EOT
, function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('this'));
$this->assertEquals('Foobar\Barfoo\Foobar', $frame->locals()->byName('this')->first()->type()->__toString());
$this->assertEquals(false, $frame->locals()->byName('this')->first()->isProperty());
}];
yield 'It returns this with correct type in an anonymous function' => [
<<<'EOT'
<?php
namespace Foobar\Barfoo;
use Acme\Factory;
class Foobar
{
public function hello()
{
new \ReflectionFunction(function() { <> });
}
}
EOT
, function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('this'));
$this->assertEquals('Foobar\Barfoo\Foobar', $frame->locals()->byName('this')->first()->type()->__toString());
$this->assertEquals(false, $frame->locals()->byName('this')->first()->isProperty());
}];
yield 'It returns method arguments' => [
<<<'EOT'
<?php
namespace Foobar\Barfoo;
use Acme\Factory;
use Phpactor\WorseReflection\Core\Logger\ArrayLogger;
class Foobar
{
public function hello(World $world)
{
<>
}
}
EOT
, function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('this'));
$this->assertEquals(
'Foobar\Barfoo\Foobar',
$frame->locals()->byName('this')->first()->type()->__toString()
);
}];
yield 'It injects method argument with inferred types' => [
<<<'EOT'
<?php
namespace Foobar\Barfoo;
use Acme\Factory;
use Phpactor\WorseReflection\Core\Logger\ArrayLogger;
class Foobar
{
/**
* @param World[] $worlds
* @param string $many
*/
public function hello(array $worlds, $many)
{
<>
}
}
EOT
, function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('many'));
$this->assertEquals('string', (string) $frame->locals()->byName('many')->first()->type());
$this->assertCount(1, $frame->locals()->byName('worlds'));
$this->assertEquals('Foobar\Barfoo\World[]', (string) $frame->locals()->byName('worlds')->first()->type());
$type = $frame->locals()->byName('worlds')->first()->type();
assert($type instanceof IterableType);
$this->assertEquals('Foobar\Barfoo\World', (string) $type->iterableValueType());
}];
yield 'Variadic argument' => [
<<<'EOT'
<?php
namespace Foobar\Barfoo;
use Acme\Factory;
use Phpactor\WorseReflection\Core\Logger\ArrayLogger;
class Foobar
{
public function hello(string ...$hellos)
{
<>
}
}
EOT
, function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('hellos'));
$variable = $frame->locals()->byName('hellos')->first();
$type = $variable->type();
assert($type instanceof IterableType);
$this->assertEquals('string', (string)$type->iterableValueType());
}];
yield 'Respects closure scope' => [
<<<'EOT'
<?php
$foo = 'bar';
$hello = function () {
$bar = 'foo';
<>
};
EOT
,
function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('$bar'), 'Scoped variable exists');
$this->assertCount(0, $frame->locals()->byName('$foo'), 'Parent scoped variable doesnt exist');
}
];
yield 'Static anonymous function has no $this or self::' => [
<<<'EOT'
<?php
$foo = 'bar';
class Foo {
public function baz() {
$hello = static function () {
$bar = 'foo';
<>
};
}
}
EOT
,
function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('$bar'));
$this->assertCount(0, $frame->locals()->byName('$this'));
}
];
yield 'Injects closure parameters' => [
<<<'EOT'
<?php
$foo = 'bar';
$hello = function (Foobar $foo) {
<>
};
EOT
,
function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('$foo'));
$variable = $frame->locals()->byName('$foo')->first();
$this->assertEquals('Foobar', $variable->type()->__toString());
}
];
yield 'Injects imported closure parent scope variables' => [
<<<'EOT'
<?php
$zed = 'zed';
$art = 'art';
$hello = function () use ($zed) {
<>
};
EOT
,
function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('$zed'));
$zed = $frame->locals()->byName('$zed')->first();
$this->assertEquals('"zed"', (string) $zed->type());
}
];
yield 'Incomplete use name' => [
<<<'EOT'
<?php
function () use ($<>
EOT
,
function (Frame $frame): void {
$this->assertCount(0, $frame->locals());
}
];
yield 'Injects variables with @var (non-standard)' => [
<<<'EOT'
<?php
/** @var string $zed */
$zed;
<>
EOT
,
function (Frame $frame): void {
$this->assertCount(1, $frame->locals()->byName('$zed'));
$this->assertEquals('string', (string) $frame->locals()->byName('$zed')->last()->type());
}
];
}
}