6
6
7
7
class ErrorTolerantConversionTest extends \PHPUnit \Framework \TestCase {
8
8
public function testIncompleteVar () {
9
- $ incompleteContents = <<<'EOT'
9
+ $ incomplete_contents = <<<'EOT'
10
10
<?php
11
11
function foo() {
12
12
$a = $
13
13
}
14
14
EOT;
15
- $ validContents = <<<'EOT'
15
+ $ valid_contents = <<<'EOT'
16
16
<?php
17
17
function foo() {
18
18
19
19
}
20
20
EOT;
21
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents );
21
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents );
22
22
}
23
23
24
24
public function testIncompleteVarWithPlaceholder () {
25
- $ incompleteContents = <<<'EOT'
25
+ $ incomplete_contents = <<<'EOT'
26
26
<?php
27
27
function foo() {
28
28
$a = $
29
29
}
30
30
EOT;
31
- $ validContents = <<<'EOT'
31
+ $ valid_contents = <<<'EOT'
32
32
<?php
33
33
function foo() {
34
34
$a = $__INCOMPLETE_VARIABLE__;
35
35
}
36
36
EOT;
37
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents , true );
37
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents , true );
38
38
}
39
39
40
40
public function testIncompleteProperty () {
41
- $ incompleteContents = <<<'EOT'
41
+ $ incomplete_contents = <<<'EOT'
42
42
<?php
43
43
function foo() {
44
44
$c;
45
45
$a = $b->
46
46
}
47
47
EOT;
48
- $ validContents = <<<'EOT'
48
+ $ valid_contents = <<<'EOT'
49
49
<?php
50
50
function foo() {
51
51
$c;
52
52
53
53
}
54
54
EOT;
55
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents );
55
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents );
56
56
}
57
57
58
58
public function testIncompletePropertyWithPlaceholder () {
59
- $ incompleteContents = <<<'EOT'
59
+ $ incomplete_contents = <<<'EOT'
60
60
<?php
61
61
function foo() {
62
62
$c;
63
63
$a = $b->
64
64
}
65
65
EOT;
66
- $ validContents = <<<'EOT'
66
+ $ valid_contents = <<<'EOT'
67
67
<?php
68
68
function foo() {
69
69
$c;
70
70
$a = $b->__INCOMPLETE_PROPERTY__;
71
71
}
72
72
EOT;
73
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents , true );
73
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents , true );
74
74
}
75
75
76
76
public function testIncompleteMethod () {
77
- $ incompleteContents = <<<'EOT'
77
+ $ incomplete_contents = <<<'EOT'
78
78
<?php
79
79
function foo() {
80
80
$b;
81
81
$a = Bar::
82
82
}
83
83
EOT;
84
- $ validContents = <<<'EOT'
84
+ $ valid_contents = <<<'EOT'
85
85
<?php
86
86
function foo() {
87
87
$b;
88
88
89
89
}
90
90
EOT;
91
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents );
91
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents );
92
92
}
93
93
94
94
public function testIncompleteMethodWithPlaceholder () {
95
- $ incompleteContents = <<<'EOT'
95
+ $ incomplete_contents = <<<'EOT'
96
96
<?php
97
97
function foo() {
98
98
$b;
99
99
$a = Bar::
100
100
}
101
101
EOT;
102
- $ validContents = <<<'EOT'
102
+ $ valid_contents = <<<'EOT'
103
103
<?php
104
104
function foo() {
105
105
$b;
106
106
$a = Bar::__INCOMPLETE_CLASS_CONST__;
107
107
}
108
108
EOT;
109
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents , true );
109
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents , true );
110
110
}
111
111
112
112
public function testMiscNoise () {
113
- $ incompleteContents = <<<'EOT'
113
+ $ incomplete_contents = <<<'EOT'
114
114
<?php
115
115
function foo() {
116
116
$b;
117
117
|
118
118
}
119
119
EOT;
120
- $ validContents = <<<'EOT'
120
+ $ valid_contents = <<<'EOT'
121
121
<?php
122
122
function foo() {
123
123
$b;
124
124
125
125
}
126
126
EOT;
127
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents );
127
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents );
128
128
}
129
129
130
130
public function testMiscNoiseWithPlaceholders () {
131
- $ incompleteContents = <<<'EOT'
131
+ $ incomplete_contents = <<<'EOT'
132
132
<?php
133
133
function foo() {
134
134
$b;
135
135
|
136
136
}
137
137
EOT;
138
- $ validContents = <<<'EOT'
138
+ $ valid_contents = <<<'EOT'
139
139
<?php
140
140
function foo() {
141
141
$b;
142
142
143
143
}
144
144
EOT;
145
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents , true );
145
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents , true );
146
146
}
147
147
148
148
public function testIncompleteArithmeticWithPlaceholders () {
149
- $ incompleteContents = <<<'EOT'
149
+ $ incomplete_contents = <<<'EOT'
150
150
<?php
151
151
function foo() {
152
152
($b * $c) +
153
153
}
154
154
EOT;
155
- $ validContents = <<<'EOT'
155
+ $ valid_contents = <<<'EOT'
156
156
<?php
157
157
function foo() {
158
158
$b * $c;
159
159
}
160
160
EOT;
161
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents , true );
161
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents , true );
162
162
}
163
163
164
164
public function testMissingSemicolon () {
165
- $ incompleteContents = <<<'EOT'
165
+ $ incomplete_contents = <<<'EOT'
166
166
<?php
167
167
function foo() {
168
168
$y = 3
169
169
$x = intdiv(3, 2);
170
170
}
171
171
EOT;
172
- $ validContents = <<<'EOT'
172
+ $ valid_contents = <<<'EOT'
173
173
<?php
174
174
function foo() {
175
175
$y = 3;
176
176
$x = intdiv(3, 2);
177
177
}
178
178
EOT;
179
- $ this ->_testFallbackFromParser ($ incompleteContents , $ validContents );
179
+ $ this ->_testFallbackFromParser ($ incomplete_contents , $ valid_contents );
180
180
}
181
181
182
182
// Another test (Won't work with php-parser, might work with tolerant-php-parser
183
183
/**
184
- $incompleteContents = <<<'EOT'
184
+ $incomplete_contents = <<<'EOT'
185
185
<?php
186
186
class C{
187
187
public function foo() {
@@ -192,7 +192,7 @@ public function bar() {
192
192
}
193
193
}
194
194
EOT;
195
- $validContents = <<<'EOT'
195
+ $valid_contents = <<<'EOT'
196
196
<?php
197
197
class C{
198
198
public function foo() {
@@ -205,51 +205,51 @@ public function bar() {
205
205
EOT;
206
206
*/
207
207
208
- private function _testFallbackFromParser (string $ incompleteContents , string $ validContents , bool $ should_add_placeholders = false ) {
208
+ private function _testFallbackFromParser (string $ incomplete_contents , string $ valid_contents , bool $ should_add_placeholders = false ) {
209
209
$ supports40 = ConversionTest::hasNativeASTSupport (40 );
210
210
$ supports50 = ConversionTest::hasNativeASTSupport (50 );
211
211
if (!($ supports40 || $ supports50 )) {
212
212
$ this ->fail ('No supported AST versions to test ' );
213
213
}
214
214
if ($ supports40 ) {
215
- $ this ->_testFallbackFromParserForASTVersion ($ incompleteContents , $ validContents , 40 , $ should_add_placeholders );
215
+ $ this ->_testFallbackFromParserForASTVersion ($ incomplete_contents , $ valid_contents , 40 , $ should_add_placeholders );
216
216
}
217
217
if ($ supports50 ) {
218
- $ this ->_testFallbackFromParserForASTVersion ($ incompleteContents , $ validContents , 50 , $ should_add_placeholders );
218
+ $ this ->_testFallbackFromParserForASTVersion ($ incomplete_contents , $ valid_contents , 50 , $ should_add_placeholders );
219
219
}
220
220
}
221
221
222
- private function _testFallbackFromParserForASTVersion (string $ incompleteContents , string $ validContents , int $ astVersion , bool $ should_add_placeholders ) {
223
- $ ast = \ast \parse_code ($ validContents , $ astVersion );
222
+ private function _testFallbackFromParserForASTVersion (string $ incomplete_contents , string $ valid_contents , int $ ast_version , bool $ should_add_placeholders ) {
223
+ $ ast = \ast \parse_code ($ valid_contents , $ ast_version );
224
224
$ this ->assertInstanceOf ('\ast\Node ' , $ ast , 'Examples(for validContents) must be syntactically valid PHP parseable by php-ast ' );
225
225
$ errors = [];
226
226
$ converter = new ASTConverter ();
227
227
$ converter ->setShouldAddPlaceholders ($ should_add_placeholders );
228
- $ phpParserNode = $ converter ->phpParserParse ($ incompleteContents , true , $ errors );
229
- $ fallback_ast = $ converter ->phpParserToPhpAst ($ phpParserNode , $ astVersion );
228
+ $ php_parser_node = $ converter ->phpParserParse ($ incomplete_contents , true , $ errors );
229
+ $ fallback_ast = $ converter ->phpParserToPhpAst ($ php_parser_node , $ ast_version );
230
230
$ this ->assertInstanceOf ('\ast\Node ' , $ fallback_ast , 'The fallback must also return a tree of php-ast nodes ' );
231
- $ fallbackASTRepr = var_export ($ fallback_ast , true );
232
- $ originalASTRepr = var_export ($ ast , true );
231
+ $ fallbackAST_repr = var_export ($ fallback_ast , true );
232
+ $ originalAST_repr = var_export ($ ast , true );
233
233
234
- if ($ fallbackASTRepr !== $ originalASTRepr ) {
234
+ if ($ fallbackAST_repr !== $ originalAST_repr ) {
235
235
$ dump = 'could not dump ' ;
236
- $ nodeDumper = new \PhpParser \NodeDumper ([
236
+ $ node_dumper = new \PhpParser \NodeDumper ([
237
237
'dumpComments ' => true ,
238
238
'dumpPositions ' => true ,
239
239
]);
240
240
try {
241
- $ dump = $ nodeDumper ->dump ($ phpParserNode );
241
+ $ dump = $ node_dumper ->dump ($ php_parser_node );
242
242
} catch (\PhpParser \Error $ e ) {
243
243
}
244
244
$ original_ast_dump = \ast_dump ($ ast );
245
- // $parser_export = var_export($phpParserNode , true);
246
- $ this ->assertSame ($ originalASTRepr , $ fallbackASTRepr , <<<EOT
245
+ // $parser_export = var_export($php_parser_node , true);
246
+ $ this ->assertSame ($ originalAST_repr , $ fallbackAST_repr , <<<EOT
247
247
The fallback must return the same tree of php-ast nodes
248
248
Code:
249
- $ incompleteContents
249
+ $ incomplete_contents
250
250
251
251
Closest Valid Code:
252
- $ validContents
252
+ $ valid_contents
253
253
254
254
Original AST:
255
255
$ original_ast_dump
0 commit comments