From eff0d8cd0ae3964c7deafb53b23381dfc3603808 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 24 Oct 2024 23:18:46 +0200 Subject: [PATCH] Tokenizer/PHP: add extra tests for DNF type tokenization Tests taken from https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/630#issuecomment-2418664762 Props michalbundyra --- tests/Core/Tokenizers/PHP/DNFTypesTest.inc | 44 ++++++++- tests/Core/Tokenizers/PHP/DNFTypesTest.php | 106 +++++++++++++++------ 2 files changed, 121 insertions(+), 29 deletions(-) diff --git a/tests/Core/Tokenizers/PHP/DNFTypesTest.inc b/tests/Core/Tokenizers/PHP/DNFTypesTest.inc index 5043ab7db5..cd27474eb2 100644 --- a/tests/Core/Tokenizers/PHP/DNFTypesTest.inc +++ b/tests/Core/Tokenizers/PHP/DNFTypesTest.inc @@ -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) ) {} @@ -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 */ @@ -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. diff --git a/tests/Core/Tokenizers/PHP/DNFTypesTest.php b/tests/Core/Tokenizers/PHP/DNFTypesTest.php index 75e1b1dbdc..e72dd41596 100644 --- a/tests/Core/Tokenizers/PHP/DNFTypesTest.php +++ b/tests/Core/Tokenizers/PHP/DNFTypesTest.php @@ -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 */', ], @@ -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 */', @@ -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() @@ -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 */', ],