Skip to content

Commit

Permalink
Tokenizer/PHP: add tests for arrow functions with intersection types
Browse files Browse the repository at this point in the history
This was already handled correctly by the Tokenizer, but not safeguarded via tests.
  • Loading branch information
jrfnl committed Apr 22, 2024
1 parent b0d2d61 commit 273959e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ $arrowWithUnionReturn = fn($param) : int|true => $param | 10;
/* testUnionReturnTypeWithFalse */
$arrowWithUnionReturn = fn($param) : string|FALSE => $param | 10;

/* testIntersectionParamType */
$arrowWithUnionParam = fn(Traversable&Countable $param) : int => (new SomeClass($param))->getValue();

/* testIntersectionReturnType */
$arrowWithUnionReturn = fn($param) : \MyFoo&SomeInterface => new SomeClass($param);

/* testTernary */
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';

Expand Down
32 changes: 32 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,38 @@ public function testUnionReturnTypeWithFalse()
}//end testUnionReturnTypeWithFalse()


/**
* Test arrow function with an intersection parameter type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testIntersectionParamType()
{
$token = $this->getTargetToken('/* testIntersectionParamType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 13, 27);

}//end testIntersectionParamType()


/**
* Test arrow function with an intersection return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testIntersectionReturnType()
{
$token = $this->getTargetToken('/* testIntersectionReturnType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 12, 20);

}//end testIntersectionReturnType()


/**
* Test arrow functions used in ternary operators.
*
Expand Down

0 comments on commit 273959e

Please sign in to comment.