-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from PHPCSStandards/arrays-lists/extra-fix-fo…
…r-tokenizer-bug Arrays::isShortArray()/Lists::isShortList(): bugfix for tokenizer issue icw short open tag
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?= | ||
|
||
/* testTokenizerIssue1971PHPCSlt330gt271E */ | ||
// Valid PHP, though not useful as the implied echo will break on the array. | ||
[1, 2, /* testTokenizerIssue1971PHPCSlt330gt271F */ [3], 4]; |
119 changes: 119 additions & 0 deletions
119
Tests/Utils/Lists/IsShortArrayOrListTokenizerBC3Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. | ||
* | ||
* @package PHPCSUtils | ||
* @copyright 2019-2020 PHPCSUtils Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCSStandards/PHPCSUtils | ||
*/ | ||
|
||
namespace PHPCSUtils\Tests\Utils\Lists; | ||
|
||
use PHPCSUtils\TestUtils\UtilityMethodTestCase; | ||
use PHPCSUtils\Utils\Arrays; | ||
use PHPCSUtils\Utils\Lists; | ||
|
||
/** | ||
* Tests for specific PHPCS tokenizer issues which can affect the \PHPCSUtils\Utils\Arrays::isShortArray() | ||
* and the \PHPCSUtils\Utils\Lists::isShortList() methods. | ||
* | ||
* @group arrays | ||
* @group lists | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class IsShortArrayOrListTokenizerBC3Test extends UtilityMethodTestCase | ||
{ | ||
|
||
/** | ||
* Data integrity test. Verify that the data provider is consistent. | ||
* | ||
* Something is either a short array or a short list or neither. It can never be both at the same time. | ||
* | ||
* @dataProvider dataIsShortArrayOrList | ||
* @coversNothing | ||
* | ||
* @param string $ignore Unused. | ||
* @param bool[] $data The expected boolean return value for list and array. | ||
* | ||
* @return void | ||
*/ | ||
public function testValidDataProvider($ignore, $data) | ||
{ | ||
$forbidden = [ | ||
'list' => 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, | ||
], | ||
], | ||
]; | ||
} | ||
} |