Skip to content

Commit

Permalink
Tokenizer/PHP: add extra tests for DNF type tokenization
Browse files Browse the repository at this point in the history
Tests taken from #630 (comment)

Props michalbundyra
  • Loading branch information
jrfnl committed Dec 19, 2024
1 parent a804206 commit eff0d8c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 29 deletions.
44 changes: 42 additions & 2 deletions tests/Core/Tokenizers/PHP/DNFTypesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ $a = $var ? $something : CONST_C | ( CONST_A & CONST_B );
/* testParensNoOwnerInShortTernary */
$a = $var ?: ( CONST_A & CONST_B );

/* testFnCallParensNoOwnerInTernaryA */
$var1 ? \X\call8() : /* testFnCallParensNoOwnerInTernaryB */ \Y\call9();

/* testPFnCallarensNoOwnerInShortTernary */
$var2 ?: \Z\call10();

/* testParensOwnerFunctionAmpersandInDefaultValue */
function defaultValueLooksLikeDNF( mixed $param = (CONST_A&CONST_B) ) {}

Expand Down Expand Up @@ -53,6 +59,20 @@ callMe(label: CONST_A | CONST_B);
/* testParensNoOwnerFunctionCallWithDNFLookALikeNamedParamIntersect */
callMe(label: CONST_A & CONST_B);

\Z1\call11(
/* testParensNoOwnerFunctionCallInNamedParam */
param1: \Z2\call12(),
/* testParensOwnerArrowFunctionInNamedParam */
param2: fn (): /* testDNFTypeArrowFnReturnInNamedParam */ int|(\Countable&\Iterable)
/* testParensNoOwnerFunctionCallInArrowFnReturn */
=> \Z3\call13(),
/* testParensOwnerClosureInNamedParam */
param3: function (): /* testDNFTypeClosureReturnInNamedParam */ int|(\DateTime&\ArrayObject) {
/* testParensNoOwnerFunctionCallInClosureReturn */
return \Z4\call14();
},
);

/* testSwitchControlStructureCondition */
switch (CONST_A | CONST_B) {
/* testFunctionCallInSwitchCaseCondition */
Expand All @@ -70,17 +90,37 @@ switch (CONST_A | CONST_B) {
/* testIfAlternativeSyntaxCondition */
if (true):
/* testFunctionCallInIfBody */
\B\call();
\B\call();
/* testElseIfAlternativeSyntaxCondition */
elseif (10):
/* testFunctionCallInElseIfBody */
C\call();
C\call();
else:
/* testFunctionCallInElseBody */
\C\call3();
endif;

gotolabel:
/* testFunctionCallInGotoBody */
\doSomething();

/* testWhileAlternativeSyntaxCondition */
while ($c3):
/* testFunctionCallInWhileBody */
\D\call4();
endwhile;

/* testForAlternativeSyntaxCondition */
for ($i = 0; $i < 10; $i++):
/* testFunctionCallInForBody */
\F\call5();
endfor;

/* testForEachAlternativeSyntaxCondition */
foreach ($array as $key => $value):
/* testFunctionCallInForeachBody */
\G\call6();
endforeach;

/*
* DNF parentheses.
Expand Down
106 changes: 79 additions & 27 deletions tests/Core/Tokenizers/PHP/DNFTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public static function dataNormalParentheses()
'parens without owner in short ternary' => [
'testMarker' => '/* testParensNoOwnerInShortTernary */',
],
'parens without owner in ternary then (fn call in inline then)' => [
'testMarker' => '/* testFnCallParensNoOwnerInTernaryA */',
],
'parens without owner in ternary then (fn call in inline else)' => [
'testMarker' => '/* testFnCallParensNoOwnerInTernaryB */',
],
'parens without owner in short ternary (fn call)' => [
'testMarker' => '/* testPFnCallarensNoOwnerInShortTernary */',
],
'parens with owner: function; & in default value' => [
'testMarker' => '/* testParensOwnerFunctionAmpersandInDefaultValue */',
],
Expand Down Expand Up @@ -158,6 +167,69 @@ public static function dataNormalParentheses()
'parens without owner, function call, named param + bitwise and' => [
'testMarker' => '/* testParensNoOwnerFunctionCallWithDNFLookALikeNamedParamIntersect */',
],
'parens without owner, function call in named param' => [
'testMarker' => '/* testParensNoOwnerFunctionCallInNamedParam */',
],
'parens with owner: fn; in named param' => [
'testMarker' => '/* testParensOwnerArrowFunctionInNamedParam */',
],
'parens without owner, function call in named param arrow return' => [
'testMarker' => '/* testParensNoOwnerFunctionCallInArrowFnReturn */',
],
'parens with owner: closure; in named param' => [
'testMarker' => '/* testParensOwnerClosureInNamedParam */',
],
'parens without owner, function call, named param closure return' => [
'testMarker' => '/* testParensNoOwnerFunctionCallInClosureReturn */',
],
'parens with owner: switch condition' => [
'testMarker' => '/* testSwitchControlStructureCondition */',
],
'parens without owner in switch-case condition' => [
'testMarker' => '/* testFunctionCallInSwitchCaseCondition */',
],
'parens without owner in switch-case body' => [
'testMarker' => '/* testFunctionCallInSwitchCaseBody */',
],
'parens without owner in switch-default body' => [
'testMarker' => '/* testFunctionCallInSwitchDefaultBody */',
],
'parens with owner: if condition, alternative syntax' => [
'testMarker' => '/* testIfAlternativeSyntaxCondition */',
],
'parens without owner in if body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInIfBody */',
],
'parens with owner: elseif condition, alternative syntax' => [
'testMarker' => '/* testElseIfAlternativeSyntaxCondition */',
],
'parens without owner in elseif body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInElseIfBody */',
],
'parens without owner in else body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInElseBody */',
],
'parens without owner in goto body' => [
'testMarker' => '/* testFunctionCallInGotoBody */',
],
'parens with owner: while condition, alternative syntax' => [
'testMarker' => '/* testWhileAlternativeSyntaxCondition */',
],
'parens without owner in while body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInWhileBody */',
],
'parens with owner: for condition, alternative syntax' => [
'testMarker' => '/* testForAlternativeSyntaxCondition */',
],
'parens without owner in for body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInForBody */',
],
'parens with owner: foreach condition, alternative syntax' => [
'testMarker' => '/* testForEachAlternativeSyntaxCondition */',
],
'parens without owner in foreach body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInForeachBody */',
],

'parens without owner in OO const default value' => [
'testMarker' => '/* testParensNoOwnerOOConstDefaultValue */',
Expand Down Expand Up @@ -193,33 +265,6 @@ public static function dataNormalParentheses()
'parens without owner in arrow function return expression' => [
'testMarker' => '/* testParensNoOwnerInArrowReturnExpression */',
],
'parens with owner: switch condition' => [
'testMarker' => '/* testSwitchControlStructureCondition */',
],
'parens without owner in switch-case condition' => [
'testMarker' => '/* testFunctionCallInSwitchCaseCondition */',
],
'parens without owner in switch-case body' => [
'testMarker' => '/* testFunctionCallInSwitchCaseBody */',
],
'parens without owner in switch-default body' => [
'testMarker' => '/* testFunctionCallInSwitchDefaultBody */',
],
'parens with owner: if condition, alternative syntax' => [
'testMarker' => '/* testIfAlternativeSyntaxCondition */',
],
'parens without owner in if body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInIfBody */',
],
'parens with owner: elseif condition, alternative syntax' => [
'testMarker' => '/* testElseIfAlternativeSyntaxCondition */',
],
'parens without owner in elseif body, alternative syntax' => [
'testMarker' => '/* testFunctionCallInElseIfBody */',
],
'parens without owner in goto body' => [
'testMarker' => '/* testFunctionCallInGotoBody */',
],
];

}//end dataNormalParentheses()
Expand Down Expand Up @@ -340,6 +385,13 @@ public function testDNFTypeParentheses($testMarker)
public static function dataDNFTypeParentheses()
{
return [
'arrow function return type: in named parameter' => [
'testMarker' => '/* testDNFTypeArrowFnReturnInNamedParam */',
],
'closure return type: in named parameter' => [
'testMarker' => '/* testDNFTypeClosureReturnInNamedParam */',
],

'OO const type: unqualified classes' => [
'testMarker' => '/* testDNFTypeOOConstUnqualifiedClasses */',
],
Expand Down

0 comments on commit eff0d8c

Please sign in to comment.