diff --git a/PHPCSUtils/Utils/Arrays.php b/PHPCSUtils/Utils/Arrays.php index d1fe32b0..af8234b7 100644 --- a/PHPCSUtils/Utils/Arrays.php +++ b/PHPCSUtils/Utils/Arrays.php @@ -105,7 +105,9 @@ public static function isShortArray(File $phpcsFile, $stackPtr) * * @link https://github.com/squizlabs/PHP_CodeSniffer/issues/1971 */ - if ($prevNonEmpty !== 0 || $tokens[$prevNonEmpty]['code'] !== \T_OPEN_TAG) { + if ($prevNonEmpty !== 0 + || isset(Collections::phpOpenTags()[$tokens[$prevNonEmpty]['code']]) === false + ) { return false; } } diff --git a/PHPCSUtils/Utils/Lists.php b/PHPCSUtils/Utils/Lists.php index 12b76309..4c9d229c 100644 --- a/PHPCSUtils/Utils/Lists.php +++ b/PHPCSUtils/Utils/Lists.php @@ -106,7 +106,8 @@ public static function isShortList(File $phpcsFile, $stackPtr) } $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true); - if ((($prevNonEmpty === 0 && $tokens[$prevNonEmpty]['code'] === \T_OPEN_TAG) // Bug #1971. + if ((($prevNonEmpty === 0 + && isset(Collections::phpOpenTags()[$tokens[$prevNonEmpty]['code']]) === true) // Bug #1971. || ($tokens[$prevNonEmpty]['code'] === \T_CLOSE_CURLY_BRACKET && isset($tokens[$prevNonEmpty]['scope_condition']))) // Bug #1284. ) { diff --git a/Tests/Utils/Lists/IsShortArrayOrListTokenizerBC3Test.inc b/Tests/Utils/Lists/IsShortArrayOrListTokenizerBC3Test.inc new file mode 100644 index 00000000..0145bfe5 --- /dev/null +++ b/Tests/Utils/Lists/IsShortArrayOrListTokenizerBC3Test.inc @@ -0,0 +1,5 @@ + true, + 'array' => true, + ]; + + $this->assertNotSame($forbidden, $data); + } + + /** + * Test correctly determining whether a short array open token is a short array, + * even when the token is incorrectly tokenized. + * + * @dataProvider dataIsShortArrayOrList + * @covers \PHPCSUtils\Utils\Arrays::isShortArray + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param bool[] $expected The expected boolean return value for list and array. + * + * @return void + */ + public function testIsShortArray($testMarker, $expected) + { + $stackPtr = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]); + $result = Arrays::isShortArray(self::$phpcsFile, $stackPtr); + + $this->assertSame($expected['array'], $result); + } + + /** + * Test correctly determining whether a short array open token is a short array or a short list, + * even when the token is incorrectly tokenized. + * + * @dataProvider dataIsShortArrayOrList + * @covers \PHPCSUtils\Utils\Lists::isShortList + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param bool[] $expected The expected boolean return value for list and array. + * + * @return void + */ + public function testIsShortList($testMarker, $expected) + { + $stackPtr = $this->getTargetToken($testMarker, [\T_OPEN_SHORT_ARRAY, \T_OPEN_SQUARE_BRACKET]); + $result = Lists::isShortList(self::$phpcsFile, $stackPtr); + + $this->assertSame($expected['list'], $result); + } + + /** + * Data provider. + * + * @see testIsShortArray() For the array format. + * @see testIsShortList() For the array format. + * + * @return array + */ + public function dataIsShortArrayOrList() + { + return [ + 'issue-1971-short-array-first-in-file' => [ + '/* testTokenizerIssue1971PHPCSlt330gt271E */', + [ + 'array' => true, + 'list' => false, + ], + ], + 'issue-1971-short-array-first-in-file-nested' => [ + '/* testTokenizerIssue1971PHPCSlt330gt271F */', + [ + 'array' => true, + 'list' => false, + ], + ], + ]; + } +}