Skip to content

Commit e70fa6e

Browse files
committed
Tests/BCFile::findStartOfStatement(): sync in new test from upstream [1]
See PR PHPCSStandards/PHP_CodeSniffer 502 / PHPCSStandards/PHP_CodeSniffer@467d284
1 parent 8b680ac commit e70fa6e

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Tests/BackCompat/BCFile/FindStartOfStatementTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,13 @@ switch ($foo) {
162162
/* testInsideDefaultContinueStatement */
163163
continue $var;
164164
}
165+
166+
match ($var) {
167+
true =>
168+
/* test437ClosureDeclaration */
169+
function ($var) {
170+
/* test437EchoNestedWithinClosureWithinMatch */
171+
echo $var, 'text', PHP_EOL;
172+
},
173+
default => false
174+
};

Tests/BackCompat/BCFile/FindStartOfStatementTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,82 @@ public static function dataFindStartInsideSwitchCaseDefaultStatements()
582582
],
583583
];
584584
}
585+
586+
/**
587+
* Test finding the start of a statement inside a closed scope nested within a match expressions.
588+
*
589+
* @param string $testMarker The comment which prefaces the target token in the test file.
590+
* @param int|string $target The token to search for after the test marker.
591+
* @param int|string $expectedTarget Token code of the expected start of statement stack pointer.
592+
*
593+
* @link https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/437
594+
*
595+
* @dataProvider dataFindStartInsideClosedScopeNestedWithinMatch
596+
*
597+
* @return void
598+
*/
599+
public function testFindStartInsideClosedScopeNestedWithinMatch($testMarker, $target, $expectedTarget)
600+
{
601+
$testToken = $this->getTargetToken($testMarker, $target);
602+
$expected = $this->getTargetToken($testMarker, $expectedTarget);
603+
604+
$found = BCFile::findStartOfStatement(self::$phpcsFile, $testToken);
605+
606+
$this->assertSame($expected, $found);
607+
}
608+
609+
/**
610+
* Data provider.
611+
*
612+
* @return array<string, array<string, int|string>>
613+
*/
614+
public static function dataFindStartInsideClosedScopeNestedWithinMatch()
615+
{
616+
return [
617+
// These were already working correctly.
618+
'Closure function keyword should be start of closure - closure keyword' => [
619+
'testMarker' => '/* test437ClosureDeclaration */',
620+
'target' => T_CLOSURE,
621+
'expectedTarget' => T_CLOSURE,
622+
],
623+
'Open curly is a statement/expression opener - open curly' => [
624+
'testMarker' => '/* test437ClosureDeclaration */',
625+
'target' => T_OPEN_CURLY_BRACKET,
626+
'expectedTarget' => T_OPEN_CURLY_BRACKET,
627+
],
628+
629+
'Echo should be start for expression - echo keyword' => [
630+
'testMarker' => '/* test437EchoNestedWithinClosureWithinMatch */',
631+
'target' => T_ECHO,
632+
'expectedTarget' => T_ECHO,
633+
],
634+
'Echo should be start for expression - variable' => [
635+
'testMarker' => '/* test437EchoNestedWithinClosureWithinMatch */',
636+
'target' => T_VARIABLE,
637+
'expectedTarget' => T_ECHO,
638+
],
639+
'Echo should be start for expression - comma' => [
640+
'testMarker' => '/* test437EchoNestedWithinClosureWithinMatch */',
641+
'target' => T_COMMA,
642+
'expectedTarget' => T_ECHO,
643+
],
644+
645+
// These were not working correctly and would previously return the close curly of the match expression.
646+
'First token after comma in echo expression should be start for expression - text string' => [
647+
'testMarker' => '/* test437EchoNestedWithinClosureWithinMatch */',
648+
'target' => T_CONSTANT_ENCAPSED_STRING,
649+
'expectedTarget' => T_CONSTANT_ENCAPSED_STRING,
650+
],
651+
'First token after comma in echo expression - PHP_EOL constant' => [
652+
'testMarker' => '/* test437EchoNestedWithinClosureWithinMatch */',
653+
'target' => T_STRING,
654+
'expectedTarget' => T_STRING,
655+
],
656+
'First token after comma in echo expression - semicolon' => [
657+
'testMarker' => '/* test437EchoNestedWithinClosureWithinMatch */',
658+
'target' => T_SEMICOLON,
659+
'expectedTarget' => T_STRING,
660+
],
661+
];
662+
}
585663
}

0 commit comments

Comments
 (0)