Skip to content

Commit

Permalink
Merge pull request #470 from PHPCSStandards/php-8.2/file-isreference-…
Browse files Browse the repository at this point in the history
…add-tests-with-dnf-types

IsReferenceTest: add extra tests with intersection/DNF types
  • Loading branch information
jrfnl authored Apr 28, 2024
2 parents f41285a + e8dea90 commit 6119828
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions tests/Core/File/IsReferenceTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ $closure = function &($param) use ($value) {};
/* testBitwiseAndArrowFunctionInDefault */
$fn = fn( $one = E_NOTICE & E_STRICT) => 1;

/* testIntersectionIsNotReference */
function intersect(Foo&Bar $param) {}

/* testDNFTypeIsNotReference */
$fn = fn((Foo&\Bar)|null /* testParamPassByReference */ &$param) => $param;

/* testTokenizerIssue1284PHPCSlt280A */
if ($foo) {}
[&$a, /* testTokenizerIssue1284PHPCSlt280B */ &$b] = $c;
Expand Down
42 changes: 40 additions & 2 deletions tests/Core/File/IsReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,50 @@ final class IsReferenceTest extends AbstractMethodUnitTest
/**
* Test that false is returned when a non-"bitwise and" token is passed.
*
* @param string $testMarker Comment which precedes the test case.
* @param array<int|string> $targetTokens Type of tokens to look for.
*
* @dataProvider dataNotBitwiseAndToken
*
* @return void
*/
public function testNotBitwiseAndToken()
public function testNotBitwiseAndToken($testMarker, $targetTokens)
{
$target = $this->getTargetToken('/* testBitwiseAndA */', T_STRING);
$targetTokens[] = T_BITWISE_AND;

$target = $this->getTargetToken($testMarker, $targetTokens);
$this->assertFalse(self::$phpcsFile->isReference($target));

}//end testNotBitwiseAndToken()


/**
* Data provider.
*
* @see testNotBitwiseAndToken()
*
* @return array<string, array<string, string|array<int|string>>>
*/
public static function dataNotBitwiseAndToken()
{
return [
'Not ampersand token at all' => [
'testMarker' => '/* testBitwiseAndA */',
'targetTokens' => [T_STRING],
],
'ampersand in intersection type' => [
'testMarker' => '/* testIntersectionIsNotReference */',
'targetTokens' => [T_TYPE_INTERSECTION],
],
'ampersand in DNF type' => [
'testMarker' => '/* testDNFTypeIsNotReference */',
'targetTokens' => [T_TYPE_INTERSECTION],
],
];

}//end dataNotBitwiseAndToken()


/**
* Test correctly identifying whether a "bitwise and" token is a reference or not.
*
Expand Down Expand Up @@ -338,6 +372,10 @@ public static function dataIsReference()
'testMarker' => '/* testBitwiseAndArrowFunctionInDefault */',
'expected' => false,
],
'reference: param pass by ref in arrow function' => [
'testMarker' => '/* testParamPassByReference */',
'expected' => true,
],
'issue-1284-short-list-directly-after-close-curly-control-structure' => [
'testMarker' => '/* testTokenizerIssue1284PHPCSlt280A */',
'expected' => true,
Expand Down

0 comments on commit 6119828

Please sign in to comment.