Skip to content

Commit d6bbff9

Browse files
committed
Tokenizer/PHP: add extra tests for DNF type tokenization
Tests taken from #630 (comment) Props michalbundyra
1 parent d7ecf7e commit d6bbff9

File tree

2 files changed

+121
-29
lines changed

2 files changed

+121
-29
lines changed

tests/Core/Tokenizers/PHP/DNFTypesTest.inc

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ $a = $var ? $something : CONST_C | ( CONST_A & CONST_B );
1313
/* testParensNoOwnerInShortTernary */
1414
$a = $var ?: ( CONST_A & CONST_B );
1515

16+
/* testFnCallParensNoOwnerInTernaryA */
17+
$var1 ? \X\call8() : /* testFnCallParensNoOwnerInTernaryB */ \Y\call9();
18+
19+
/* testPFnCallarensNoOwnerInShortTernary */
20+
$var2 ?: \Z\call10();
21+
1622
/* testParensOwnerFunctionAmpersandInDefaultValue */
1723
function defaultValueLooksLikeDNF( mixed $param = (CONST_A&CONST_B) ) {}
1824

@@ -53,6 +59,20 @@ callMe(label: CONST_A | CONST_B);
5359
/* testParensNoOwnerFunctionCallWithDNFLookALikeNamedParamIntersect */
5460
callMe(label: CONST_A & CONST_B);
5561

62+
\Z1\call11(
63+
/* testParensNoOwnerFunctionCallInNamedParam */
64+
param1: \Z2\call12(),
65+
/* testParensOwnerArrowFunctionInNamedParam */
66+
param2: fn (): /* testDNFTypeArrowFnReturnInNamedParam */ int|(\Countable&\Iterable)
67+
/* testParensNoOwnerFunctionCallInArrowFnReturn */
68+
=> \Z3\call13(),
69+
/* testParensOwnerClosureInNamedParam */
70+
param3: function (): /* testDNFTypeClosureReturnInNamedParam */ int|(\DateTime&\ArrayObject) {
71+
/* testParensNoOwnerFunctionCallInClosureReturn */
72+
return \Z4\call14();
73+
},
74+
);
75+
5676
/* testSwitchControlStructureCondition */
5777
switch (CONST_A | CONST_B) {
5878
/* testFunctionCallInSwitchCaseCondition */
@@ -70,17 +90,37 @@ switch (CONST_A | CONST_B) {
7090
/* testIfAlternativeSyntaxCondition */
7191
if (true):
7292
/* testFunctionCallInIfBody */
73-
\B\call();
93+
\B\call();
7494
/* testElseIfAlternativeSyntaxCondition */
7595
elseif (10):
7696
/* testFunctionCallInElseIfBody */
77-
C\call();
97+
C\call();
98+
else:
99+
/* testFunctionCallInElseBody */
100+
\C\call3();
78101
endif;
79102

80103
gotolabel:
81104
/* testFunctionCallInGotoBody */
82105
\doSomething();
83106

107+
/* testWhileAlternativeSyntaxCondition */
108+
while ($c3):
109+
/* testFunctionCallInWhileBody */
110+
\D\call4();
111+
endwhile;
112+
113+
/* testForAlternativeSyntaxCondition */
114+
for ($i = 0; $i < 10; $i++):
115+
/* testFunctionCallInForBody */
116+
\F\call5();
117+
endfor;
118+
119+
/* testForEachAlternativeSyntaxCondition */
120+
foreach ($array as $key => $value):
121+
/* testFunctionCallInForeachBody */
122+
\G\call6();
123+
endforeach;
84124

85125
/*
86126
* DNF parentheses.

tests/Core/Tokenizers/PHP/DNFTypesTest.php

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ public static function dataNormalParentheses()
113113
'parens without owner in short ternary' => [
114114
'testMarker' => '/* testParensNoOwnerInShortTernary */',
115115
],
116+
'parens without owner in ternary then (fn call in inline then)' => [
117+
'testMarker' => '/* testFnCallParensNoOwnerInTernaryA */',
118+
],
119+
'parens without owner in ternary then (fn call in inline else)' => [
120+
'testMarker' => '/* testFnCallParensNoOwnerInTernaryB */',
121+
],
122+
'parens without owner in short ternary (fn call)' => [
123+
'testMarker' => '/* testPFnCallarensNoOwnerInShortTernary */',
124+
],
116125
'parens with owner: function; & in default value' => [
117126
'testMarker' => '/* testParensOwnerFunctionAmpersandInDefaultValue */',
118127
],
@@ -158,6 +167,69 @@ public static function dataNormalParentheses()
158167
'parens without owner, function call, named param + bitwise and' => [
159168
'testMarker' => '/* testParensNoOwnerFunctionCallWithDNFLookALikeNamedParamIntersect */',
160169
],
170+
'parens without owner, function call in named param' => [
171+
'testMarker' => '/* testParensNoOwnerFunctionCallInNamedParam */',
172+
],
173+
'parens with owner: fn; in named param' => [
174+
'testMarker' => '/* testParensOwnerArrowFunctionInNamedParam */',
175+
],
176+
'parens without owner, function call in named param arrow return' => [
177+
'testMarker' => '/* testParensNoOwnerFunctionCallInArrowFnReturn */',
178+
],
179+
'parens with owner: closure; in named param' => [
180+
'testMarker' => '/* testParensOwnerClosureInNamedParam */',
181+
],
182+
'parens without owner, function call, named param closure return' => [
183+
'testMarker' => '/* testParensNoOwnerFunctionCallInClosureReturn */',
184+
],
185+
'parens with owner: switch condition' => [
186+
'testMarker' => '/* testSwitchControlStructureCondition */',
187+
],
188+
'parens without owner in switch-case condition' => [
189+
'testMarker' => '/* testFunctionCallInSwitchCaseCondition */',
190+
],
191+
'parens without owner in switch-case body' => [
192+
'testMarker' => '/* testFunctionCallInSwitchCaseBody */',
193+
],
194+
'parens without owner in switch-default body' => [
195+
'testMarker' => '/* testFunctionCallInSwitchDefaultBody */',
196+
],
197+
'parens with owner: if condition, alternative syntax' => [
198+
'testMarker' => '/* testIfAlternativeSyntaxCondition */',
199+
],
200+
'parens without owner in if body, alternative syntax' => [
201+
'testMarker' => '/* testFunctionCallInIfBody */',
202+
],
203+
'parens with owner: elseif condition, alternative syntax' => [
204+
'testMarker' => '/* testElseIfAlternativeSyntaxCondition */',
205+
],
206+
'parens without owner in elseif body, alternative syntax' => [
207+
'testMarker' => '/* testFunctionCallInElseIfBody */',
208+
],
209+
'parens without owner in else body, alternative syntax' => [
210+
'testMarker' => '/* testFunctionCallInElseBody */',
211+
],
212+
'parens without owner in goto body' => [
213+
'testMarker' => '/* testFunctionCallInGotoBody */',
214+
],
215+
'parens with owner: while condition, alternative syntax' => [
216+
'testMarker' => '/* testWhileAlternativeSyntaxCondition */',
217+
],
218+
'parens without owner in while body, alternative syntax' => [
219+
'testMarker' => '/* testFunctionCallInWhileBody */',
220+
],
221+
'parens with owner: for condition, alternative syntax' => [
222+
'testMarker' => '/* testForAlternativeSyntaxCondition */',
223+
],
224+
'parens without owner in for body, alternative syntax' => [
225+
'testMarker' => '/* testFunctionCallInForBody */',
226+
],
227+
'parens with owner: foreach condition, alternative syntax' => [
228+
'testMarker' => '/* testForEachAlternativeSyntaxCondition */',
229+
],
230+
'parens without owner in foreach body, alternative syntax' => [
231+
'testMarker' => '/* testFunctionCallInForeachBody */',
232+
],
161233

162234
'parens without owner in OO const default value' => [
163235
'testMarker' => '/* testParensNoOwnerOOConstDefaultValue */',
@@ -193,33 +265,6 @@ public static function dataNormalParentheses()
193265
'parens without owner in arrow function return expression' => [
194266
'testMarker' => '/* testParensNoOwnerInArrowReturnExpression */',
195267
],
196-
'parens with owner: switch condition' => [
197-
'testMarker' => '/* testSwitchControlStructureCondition */',
198-
],
199-
'parens without owner in switch-case condition' => [
200-
'testMarker' => '/* testFunctionCallInSwitchCaseCondition */',
201-
],
202-
'parens without owner in switch-case body' => [
203-
'testMarker' => '/* testFunctionCallInSwitchCaseBody */',
204-
],
205-
'parens without owner in switch-default body' => [
206-
'testMarker' => '/* testFunctionCallInSwitchDefaultBody */',
207-
],
208-
'parens with owner: if condition, alternative syntax' => [
209-
'testMarker' => '/* testIfAlternativeSyntaxCondition */',
210-
],
211-
'parens without owner in if body, alternative syntax' => [
212-
'testMarker' => '/* testFunctionCallInIfBody */',
213-
],
214-
'parens with owner: elseif condition, alternative syntax' => [
215-
'testMarker' => '/* testElseIfAlternativeSyntaxCondition */',
216-
],
217-
'parens without owner in elseif body, alternative syntax' => [
218-
'testMarker' => '/* testFunctionCallInElseIfBody */',
219-
],
220-
'parens without owner in goto body' => [
221-
'testMarker' => '/* testFunctionCallInGotoBody */',
222-
],
223268
];
224269

225270
}//end dataNormalParentheses()
@@ -340,6 +385,13 @@ public function testDNFTypeParentheses($testMarker)
340385
public static function dataDNFTypeParentheses()
341386
{
342387
return [
388+
'arrow function return type: in named parameter' => [
389+
'testMarker' => '/* testDNFTypeArrowFnReturnInNamedParam */',
390+
],
391+
'closure return type: in named parameter' => [
392+
'testMarker' => '/* testDNFTypeClosureReturnInNamedParam */',
393+
],
394+
343395
'OO const type: unqualified classes' => [
344396
'testMarker' => '/* testDNFTypeOOConstUnqualifiedClasses */',
345397
],

0 commit comments

Comments
 (0)