diff --git a/PHPCSUtils/Internal/IsShortArrayOrList.php b/PHPCSUtils/Internal/IsShortArrayOrList.php index 8a665ad5..150afc16 100644 --- a/PHPCSUtils/Internal/IsShortArrayOrList.php +++ b/PHPCSUtils/Internal/IsShortArrayOrList.php @@ -72,15 +72,6 @@ final class IsShortArrayOrList */ private $phpcsFile; - /** - * The current stack pointer. - * - * @since 1.0.0-alpha4 - * - * @var int - */ - private $stackPtr; - /** * The token stack from the current file. * @@ -141,7 +132,7 @@ final class IsShortArrayOrList * @since 1.0.0-alpha4 * * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the short array bracket token. + * @param int $stackPtr The position of the short array opener token. * * @return void */ @@ -155,15 +146,8 @@ public function __construct(File $phpcsFile, $stackPtr) } $this->phpcsFile = $phpcsFile; - $this->stackPtr = $stackPtr; $this->tokens = $tokens; - - $this->opener = $stackPtr; - if (isset($this->tokens[$stackPtr]['bracket_opener']) - && $stackPtr !== $this->tokens[$stackPtr]['bracket_opener'] - ) { - $this->opener = $this->tokens[$stackPtr]['bracket_opener']; - } + $this->opener = $stackPtr; $this->closer = $stackPtr; if (isset($this->tokens[$stackPtr]['bracket_closer']) diff --git a/PHPCSUtils/Internal/IsShortArrayOrListWithCache.php b/PHPCSUtils/Internal/IsShortArrayOrListWithCache.php new file mode 100644 index 00000000..56eaf1d2 --- /dev/null +++ b/PHPCSUtils/Internal/IsShortArrayOrListWithCache.php @@ -0,0 +1,272 @@ + IsShortArrayOrList::SHORT_ARRAY, + IsShortArrayOrList::SHORT_LIST => IsShortArrayOrList::SHORT_LIST, + IsShortArrayOrList::SQUARE_BRACKETS => IsShortArrayOrList::SQUARE_BRACKETS, + ]; + + /** + * Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short array construct + * and not a short list. + * + * This method also accepts `T_OPEN/CLOSE_SQUARE_BRACKET` tokens to allow it to be + * PHPCS cross-version compatible as the short array tokenizing has been plagued by + * a number of bugs over time. + * + * @since 1.0.0-alpha4 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the short array bracket token. + * + * @return bool `TRUE` if the token passed is the open/close bracket of a short array. + * `FALSE` if the token is a short list bracket, a plain square bracket + * or not one of the accepted tokens. + */ + public static function isShortArray(File $phpcsFile, $stackPtr) + { + return (self::getType($phpcsFile, $stackPtr) === IsShortArrayOrList::SHORT_ARRAY); + } + + /** + * Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short list construct + * in contrast to a short array. + * + * This method also accepts `T_OPEN/CLOSE_SQUARE_BRACKET` tokens to allow it to be + * PHPCS cross-version compatible as the short array tokenizing has been plagued by + * a number of bugs over time, which affects the short list determination. + * + * @since 1.0.0-alpha4 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the short array bracket token. + * + * @return bool `TRUE` if the token passed is the open/close bracket of a short list. + * `FALSE` if the token is a short array bracket or plain square bracket + * or not one of the accepted tokens. + */ + public static function isShortList(File $phpcsFile, $stackPtr) + { + return (self::getType($phpcsFile, $stackPtr) === IsShortArrayOrList::SHORT_LIST); + } + + /** + * Determine whether a T_OPEN/CLOSE_SHORT_ARRAY token is a short array or short list construct. + * + * This method also accepts `T_OPEN/CLOSE_SQUARE_BRACKET` tokens to allow it to be + * PHPCS cross-version compatible as the short array tokenizing has been plagued by + * a number of bugs over time, which affects the short list determination. + * + * @since 1.0.0-alpha4 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the short array bracket token. + * + * @return string|false The type of construct this bracket was determined to be. + * Either 'short array', 'short list' or 'square brackets'. + * Or FALSE is this was not a bracket token. + */ + public static function getType(File $phpcsFile, $stackPtr) + { + return (new self($phpcsFile, $stackPtr))->process(); + } + + /** + * Constructor. + * + * @since 1.0.0-alpha4 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the short array bracket token. + * + * @return void + */ + private function __construct(File $phpcsFile, $stackPtr) + { + $this->phpcsFile = $phpcsFile; + $this->tokens = $phpcsFile->getTokens(); + $this->stackPtr = $stackPtr; + } + + /** + * Determine whether a T_[OPEN|CLOSE}_[SHORT_ARRAY|SQUARE_BRACKET] token is a short array + * or short list construct using previously cached results whenever possible. + * + * @since 1.0.0-alpha4 + * + * @return string|false The type of construct this bracket was determined to be. + * Either 'short array', 'short list' or 'square brackets'. + * Or FALSE is this was not a bracket token. + */ + private function process() + { + if ($this->isValidStackPtr() === false) { + return false; + } + + $this->opener = $this->getOpener(); + + /* + * Check the cache in case we've seen this token before. + */ + $type = $this->getFromCache(); + if ($type !== false) { + return $type; + } + + /* + * If we've not seen the token before, try and solve it and cache the results. + */ + $solver = new IsShortArrayOrList($this->phpcsFile, $this->opener); + $type = $solver->solve(); + + $this->updateCache($type); + + return $type; + } + + /** + * Verify the passed token could potentially be a short array or short list token. + * + * @since 1.0.0-alpha4 + * + * @return bool + */ + private function isValidStackPtr() + { + return (isset($this->tokens[$this->stackPtr]) === true + && isset(Collections::shortArrayTokensBC()[$this->tokens[$this->stackPtr]['code']]) === true); + } + + /** + * Get the stack pointer to the short array/list opener. + * + * @since 1.0.0-alpha4 + * + * @return int + */ + private function getOpener() + { + $opener = $this->stackPtr; + if (isset($this->tokens[$this->stackPtr]['bracket_opener'])) { + $opener = $this->tokens[$this->stackPtr]['bracket_opener']; + } + + return $opener; + } + + /** + * Retrieve the bracket "type" of a token from the cache. + * + * @since 1.0.0-alpha4 + * + * @return string|false The previously determined type (which could be an empty string) + * or FALSE if no cache entry was found for this token. + */ + private function getFromCache() + { + if (Cache::isCached($this->phpcsFile, __CLASS__, $this->opener) === true) { + return Cache::get($this->phpcsFile, __CLASS__, $this->opener); + } + + return false; + } + + /** + * Update the cache with information about a particular bracket token. + * + * @since 1.0.0-alpha4 + * + * @param string $type The type this bracket has been determined to be. + * Either 'short array', 'short list' or 'square brackets'. + * + * @return void + */ + private function updateCache($type) + { + $result = false; + if (isset($this->validTypes[$type]) === true) { + $result = $type; + } + + Cache::set($this->phpcsFile, __CLASS__, $this->opener, $result); + } +} diff --git a/PHPCSUtils/Utils/Arrays.php b/PHPCSUtils/Utils/Arrays.php index b248ee94..328e3291 100644 --- a/PHPCSUtils/Utils/Arrays.php +++ b/PHPCSUtils/Utils/Arrays.php @@ -13,7 +13,7 @@ use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHPCSUtils\Internal\Cache; -use PHPCSUtils\Internal\IsShortArrayOrList; +use PHPCSUtils\Internal\IsShortArrayOrListWithCache; use PHPCSUtils\Tokens\Collections; /** @@ -64,25 +64,7 @@ final class Arrays */ public static function isShortArray(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - - // Is this one of the tokens this function handles ? - if (isset($tokens[$stackPtr]) === false - || isset(Collections::shortArrayTokensBC()[$tokens[$stackPtr]['code']]) === false - ) { - return false; - } - - if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) { - return Cache::get($phpcsFile, __METHOD__, $stackPtr); - } - - $solver = new IsShortArrayOrList($phpcsFile, $stackPtr); - $type = $solver->solve(); - $returnValue = ($type === IsShortArrayOrList::SHORT_ARRAY); - - Cache::set($phpcsFile, __METHOD__, $stackPtr, $returnValue); - return $returnValue; + return IsShortArrayOrListWithCache::isShortArray($phpcsFile, $stackPtr); } /** diff --git a/PHPCSUtils/Utils/Lists.php b/PHPCSUtils/Utils/Lists.php index 8315ae7e..0c768763 100644 --- a/PHPCSUtils/Utils/Lists.php +++ b/PHPCSUtils/Utils/Lists.php @@ -14,7 +14,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Util\Tokens; use PHPCSUtils\Internal\Cache; -use PHPCSUtils\Internal\IsShortArrayOrList; +use PHPCSUtils\Internal\IsShortArrayOrListWithCache; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\GetTokensAsString; @@ -66,25 +66,7 @@ final class Lists */ public static function isShortList(File $phpcsFile, $stackPtr) { - $tokens = $phpcsFile->getTokens(); - - // Is this one of the tokens this function handles ? - if (isset($tokens[$stackPtr]) === false - || isset(Collections::shortListTokensBC()[$tokens[$stackPtr]['code']]) === false - ) { - return false; - } - - if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) { - return Cache::get($phpcsFile, __METHOD__, $stackPtr); - } - - $solver = new IsShortArrayOrList($phpcsFile, $stackPtr); - $type = $solver->solve(); - $returnValue = ($type === IsShortArrayOrList::SHORT_LIST); - - Cache::set($phpcsFile, __METHOD__, $stackPtr, $returnValue); - return $returnValue; + return IsShortArrayOrListWithCache::isShortList($phpcsFile, $stackPtr); } /** diff --git a/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.inc b/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.inc index 6b3194fb..c1e54e03 100644 --- a/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.inc +++ b/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.inc @@ -64,7 +64,7 @@ echo 'just here to prevent the below test running into a tokenizer issue tested /* testShortList */ [$b] = $c; -/* testShortListDetectOnCloseBracket */ +/* testShortListMultiItem */ [$a, $b] = $c; /* testShortListWithKeys */ diff --git a/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.php b/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.php index bea096b3..880e515e 100644 --- a/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.php +++ b/Tests/Internal/IsShortArrayOrList/IsShortArrayOrListTest.php @@ -61,7 +61,7 @@ public function dataNotShortArrayShortListBracket() return [ 'array-assignment-no-key' => [ '/* testArrayAssignmentEmpty */', - \T_CLOSE_SQUARE_BRACKET, + \T_OPEN_SQUARE_BRACKET, ], 'array-assignment-string-key' => [ '/* testArrayAssignmentStringKey */', @@ -77,7 +77,7 @@ public function dataNotShortArrayShortListBracket() ], 'array-access-string-key' => [ '/* testArrayAccessStringKey */', - \T_CLOSE_SQUARE_BRACKET, + \T_OPEN_SQUARE_BRACKET, ], 'array-access-int-key-1' => [ '/* testArrayAccessIntKey1 */', @@ -211,10 +211,9 @@ public function dataIsShortArrayOrList() '/* testShortList */', IsShortArrayOrList::SHORT_LIST, ], - 'short-list-detect-on-close-bracket' => [ - '/* testShortListDetectOnCloseBracket */', + 'short-list-multi-item' => [ + '/* testShortListMultiItem */', IsShortArrayOrList::SHORT_LIST, - \T_CLOSE_SHORT_ARRAY, ], 'short-list-with-keys' => [ '/* testShortListWithKeys */', @@ -236,10 +235,9 @@ public function dataIsShortArrayOrList() '/* testShortListInForeachNested */', IsShortArrayOrList::SHORT_LIST, ], - 'short-list-in-foreach-with-keys-detect-on-close-bracket' => [ + 'short-list-in-foreach-with-keys' => [ '/* testShortListInForeachWithKeysDetectOnCloseBracket */', IsShortArrayOrList::SHORT_LIST, - \T_CLOSE_SHORT_ARRAY, ], 'short-list-in-chained-assignment' => [ @@ -249,7 +247,6 @@ public function dataIsShortArrayOrList() 'short-array-in-chained-assignment' => [ '/* testShortArrayMultiAssign */', IsShortArrayOrList::SHORT_ARRAY, - \T_CLOSE_SHORT_ARRAY, ], 'short-array-with-nesting-and-keys' => [ '/* testShortArrayWithNestingAndKeys */', @@ -328,70 +325,4 @@ public function dataIsShortArrayOrList() ], ]; } - - /** - * Verify that the build-in caching is used when caching is enabled. - * - * @covers \PHPCSUtils\Utils\Arrays::isShortArray - * - * @return void - */ - public function testIsShortArrayResultIsCached() - { - $methodName = 'PHPCSUtils\\Utils\\Arrays::isShortArray'; - $testMarker = '/* testShortArrayUnionFirst */'; - $expected = true; - - $stackPtr = $this->getTargetToken($testMarker, \T_OPEN_SHORT_ARRAY); - - // Verify the caching works. - $origStatus = Cache::$enabled; - Cache::$enabled = true; - - $resultFirstRun = Arrays::isShortArray(self::$phpcsFile, $stackPtr); - $isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr); - $resultSecondRun = Arrays::isShortArray(self::$phpcsFile, $stackPtr); - - if ($origStatus === false) { - Cache::clear(); - } - Cache::$enabled = $origStatus; - - $this->assertSame($expected, $resultFirstRun, 'First result did not match expectation'); - $this->assertTrue($isCached, 'Cache::isCached() could not find the cached value'); - $this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first'); - } - - /** - * Verify that the build-in caching is used when caching is enabled. - * - * @covers \PHPCSUtils\Utils\Lists::isShortList - * - * @return void - */ - public function testIsShortListResultIsCached() - { - $methodName = 'PHPCSUtils\\Utils\\Lists::isShortList'; - $testMarker = '/* testShortListWithNestingAndKeys */'; - $expected = true; - - $stackPtr = $this->getTargetToken($testMarker, \T_OPEN_SHORT_ARRAY); - - // Verify the caching works. - $origStatus = Cache::$enabled; - Cache::$enabled = true; - - $resultFirstRun = Lists::isShortList(self::$phpcsFile, $stackPtr); - $isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr); - $resultSecondRun = Lists::isShortList(self::$phpcsFile, $stackPtr); - - if ($origStatus === false) { - Cache::clear(); - } - Cache::$enabled = $origStatus; - - $this->assertSame($expected, $resultFirstRun, 'First result did not match expectation'); - $this->assertTrue($isCached, 'Cache::isCached() could not find the cached value'); - $this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first'); - } } diff --git a/Tests/Internal/IsShortArrayOrListWithCache/CachingTest.php b/Tests/Internal/IsShortArrayOrListWithCache/CachingTest.php new file mode 100644 index 00000000..d5c8fbf8 --- /dev/null +++ b/Tests/Internal/IsShortArrayOrListWithCache/CachingTest.php @@ -0,0 +1,126 @@ +getTargetToken($testMarker, Collections::shortArrayListOpenTokensBC()); + $closer = $this->getTargetToken($testMarker, [\T_CLOSE_SHORT_ARRAY, \T_CLOSE_SQUARE_BRACKET]); + + // Verify the caching works. + $origStatus = Cache::$enabled; + Cache::$enabled = true; + + $resultFirstRun = IsShortArrayOrListWithCache::getType(self::$phpcsFile, $closer); + $isCachedOpener = Cache::isCached(self::$phpcsFile, self::CACHE_KEY, $opener); + $isCachedCloser = Cache::isCached(self::$phpcsFile, self::CACHE_KEY, $closer); + $resultSecondRun = IsShortArrayOrListWithCache::getType(self::$phpcsFile, $opener); + + if ($origStatus === false) { + Cache::clear(); + } + Cache::$enabled = $origStatus; + + $this->assertSame($expected, $resultFirstRun, 'First result did not match expectation'); + $this->assertTrue($isCachedOpener, 'Cache::isCached() could not find the cached value'); + $this->assertFalse($isCachedCloser, 'Cache::isCached() erroneously found a cached value for the closer'); + $this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first'); + } + + /** + * Data provider. + * + * @return array + */ + public function dataResultIsCached() + { + return [ + 'short array' => [ + 'testMarker' => '/* testShortArray */', + 'expected' => IsShortArrayOrList::SHORT_ARRAY, + ], + 'short list' => [ + 'testMarker' => '/* testShortList */', + 'expected' => IsShortArrayOrList::SHORT_LIST, + ], + 'square bracket' => [ + 'testMarker' => '/* testSquareBrackets */', + 'expected' => IsShortArrayOrList::SQUARE_BRACKETS, + ], + ]; + } + + /** + * Verify that the result is not cached when an invalid token is passed. + * + * @return void + */ + public function testInvalidTokenResultIsNotCached() + { + $stackPtr = $this->getTargetToken('/* testLongArray */', \T_ARRAY); + + // Verify the caching works. + $origStatus = Cache::$enabled; + Cache::$enabled = true; + + $resultFirstRun = IsShortArrayOrListWithCache::getType(self::$phpcsFile, $stackPtr); + $isCached = Cache::isCached(self::$phpcsFile, self::CACHE_KEY, $stackPtr); + $resultSecondRun = IsShortArrayOrListWithCache::getType(self::$phpcsFile, $stackPtr); + + if ($origStatus === false) { + Cache::clear(); + } + Cache::$enabled = $origStatus; + + $this->assertFalse($resultFirstRun, 'First result did not match expectation'); + $this->assertFalse($isCached, 'Cache::isCached() erroneously found a cached value'); + $this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first'); + } +} diff --git a/Tests/Internal/IsShortArrayOrListWithCache/EntryPointsTest.php b/Tests/Internal/IsShortArrayOrListWithCache/EntryPointsTest.php new file mode 100644 index 00000000..fd237465 --- /dev/null +++ b/Tests/Internal/IsShortArrayOrListWithCache/EntryPointsTest.php @@ -0,0 +1,152 @@ +getTargetToken($testMarker, $targetToken); + $this->assertIsBool(Arrays::isShortArray(self::$phpcsFile, $target)); + } + + /** + * Validate that the `Lists::isShortList()` method is accessible and always returns a boolean value. + * + * @dataProvider dataEntryPoints + * + * @covers \PHPCSUtils\Utils\Lists::isShortList + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetToken The token type(s) to look for. + * + * @return void + */ + public function testIsShortListApi($testMarker, $targetToken) + { + $target = $this->getTargetToken($testMarker, $targetToken); + $this->assertIsBool(Lists::isShortList(self::$phpcsFile, $target)); + } + + /** + * Validate that the `IsShortArrayOrListWithCache::isShortArray()` method is accessible + * and always returns a boolean value. + * + * @dataProvider dataEntryPoints + * + * @covers \PHPCSUtils\Internal\IsShortArrayOrListWithCache::isShortArray + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetToken The token type(s) to look for. + * + * @return void + */ + public function testIsShortArrayInternal($testMarker, $targetToken) + { + $target = $this->getTargetToken($testMarker, $targetToken); + $this->assertIsBool(IsShortArrayOrListWithCache::isShortArray(self::$phpcsFile, $target)); + } + + /** + * Validate that the `IsShortArrayOrListWithCache::isShortList()` method is accessible + * and always returns a boolean value. + * + * @dataProvider dataEntryPoints + * + * @covers \PHPCSUtils\Internal\IsShortArrayOrListWithCache::isShortList + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetToken The token type(s) to look for. + * + * @return void + */ + public function testIsShortListInternal($testMarker, $targetToken) + { + $target = $this->getTargetToken($testMarker, $targetToken); + $this->assertIsBool(IsShortArrayOrListWithCache::isShortList(self::$phpcsFile, $target)); + } + + /** + * Validate that the `IsShortArrayOrListWithCache::getType()` method is accessible + * and always returns a boolean value. + * + * @dataProvider dataEntryPoints + * + * @covers \PHPCSUtils\Internal\IsShortArrayOrListWithCache::getType + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetToken The token type(s) to look for. + * + * @return void + */ + public function testGetTypeInternal($testMarker, $targetToken) + { + $target = $this->getTargetToken($testMarker, $targetToken); + $this->assertContains(IsShortArrayOrListWithCache::getType(self::$phpcsFile, $target), $this->validValues); + } + + /** + * Data provider. + * + * @return array + */ + public function dataEntryPoints() + { + $defaultTargets = Collections::shortArrayListOpenTokensBC(); + + return [ + 'not a square bracket' => [ + 'testMarker' => '/* testLongArray */', + 'targetToken' => \T_ARRAY, + ], + 'short array' => [ + 'testMarker' => '/* testShortArray */', + 'targetToken' => $defaultTargets, + ], + 'short list' => [ + 'testMarker' => '/* testShortList */', + 'targetToken' => $defaultTargets, + ], + 'square bracket' => [ + 'testMarker' => '/* testSquareBrackets */', + 'targetToken' => $defaultTargets, + ], + ]; + } +} diff --git a/Tests/Internal/IsShortArrayOrListWithCache/IsShortArrayOrListWithCacheTest.inc b/Tests/Internal/IsShortArrayOrListWithCache/IsShortArrayOrListWithCacheTest.inc new file mode 100644 index 00000000..b422081f --- /dev/null +++ b/Tests/Internal/IsShortArrayOrListWithCache/IsShortArrayOrListWithCacheTest.inc @@ -0,0 +1,23 @@ +assertFalse(IsShortArrayOrListWithCache::getType(self::$phpcsFile, 100000)); + } + + /** + * Test that false is returned when a non-bracket token is passed. + * + * @dataProvider dataNotBracket + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetToken The token type(s) to look for. + * + * @return void + */ + public function testNotBracket($testMarker, $targetToken) + { + $target = $this->getTargetToken($testMarker, $targetToken); + $this->assertFalse(IsShortArrayOrListWithCache::getType(self::$phpcsFile, $target)); + } + + /** + * Data provider. + * + * @see testNotBracket() For the array format. + * + * @return array + */ + public function dataNotBracket() + { + return [ + 'long-array' => [ + '/* testLongArray */', + \T_ARRAY, + ], + 'long-list' => [ + '/* testLongList */', + \T_LIST, + ], + ]; + } + + /** + * Test that the returned type is one of the valid values when a valid bracket token is passed. + * + * This test also safeguards that the class supports both open, as well as close brackets. + * + * @dataProvider dataValidBracket + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetToken The token type(s) to look for. + * + * @return void + */ + public function testValidBracket($testMarker, $targetToken) + { + $target = $this->getTargetToken($testMarker, $targetToken); + $result = IsShortArrayOrListWithCache::getType(self::$phpcsFile, $target); + + $this->assertContains($result, $this->validValues); + } + + /** + * Data provider. + * + * @see testNotBracket() For the array format. + * + * @return array + */ + public function dataValidBracket() + { + return [ + 'open square bracket' => [ + '/* testSquareBrackets */', + \T_OPEN_SQUARE_BRACKET, + ], + 'close square bracket' => [ + '/* testSquareBrackets */', + \T_CLOSE_SQUARE_BRACKET, + ], + 'open short array token' => [ + '/* testShortArray */', + \T_OPEN_SHORT_ARRAY, + ], + 'close short list token' => [ + '/* testShortList */', + \T_CLOSE_SHORT_ARRAY, + ], + ]; + } +} diff --git a/Tests/Internal/IsShortArrayOrListWithCache/ProcessTest.php b/Tests/Internal/IsShortArrayOrListWithCache/ProcessTest.php new file mode 100644 index 00000000..6dd3223d --- /dev/null +++ b/Tests/Internal/IsShortArrayOrListWithCache/ProcessTest.php @@ -0,0 +1,107 @@ +getTargetToken('/* testLongArray */', \T_ARRAY); + $this->assertFalse(IsShortArrayOrListWithCache::getType(self::$phpcsFile, $target)); + } + + /** + * Test the process method works for all supported token types which are allowed to be passed to it. + * + * @dataProvider dataSupportedBrackets + * + * @param string $testMarker The comment which prefaces the target token in the test file. + * @param int|string|array $targetType The token type(s) to look for. + * @param string|false $expected The expected function return value. + * + * @return void + */ + public function testSupportedBrackets($testMarker, $targetType, $expected) + { + $target = $this->getTargetToken($testMarker, $targetType); + $this->assertSame($expected, IsShortArrayOrListWithCache::getType(self::$phpcsFile, $target)); + } + + /** + * Data provider. + * + * @see testSupportedBrackets() For the array format. + * + * @return array + */ + public function dataSupportedBrackets() + { + return [ + 'short-array-open-bracket' => [ + '/* testShortArray */', + \T_OPEN_SHORT_ARRAY, + IsShortArrayOrList::SHORT_ARRAY, + ], + 'short-array-close-bracket' => [ + '/* testShortArray */', + \T_CLOSE_SHORT_ARRAY, + IsShortArrayOrList::SHORT_ARRAY, + ], + 'short-list-open-bracket' => [ + '/* testShortList */', + \T_OPEN_SHORT_ARRAY, + IsShortArrayOrList::SHORT_LIST, + ], + 'short-list-close-bracket' => [ + '/* testShortList */', + \T_CLOSE_SHORT_ARRAY, + IsShortArrayOrList::SHORT_LIST, + ], + 'square-brackets-open-bracket' => [ + '/* testSquareBrackets */', + \T_OPEN_SQUARE_BRACKET, + IsShortArrayOrList::SQUARE_BRACKETS, + ], + 'square-brackets-close-bracket' => [ + '/* testSquareBrackets */', + \T_CLOSE_SQUARE_BRACKET, + IsShortArrayOrList::SQUARE_BRACKETS, + ], + 'parse-error-close-bracket' => [ + '/* testParseError */', + \T_CLOSE_SQUARE_BRACKET, + IsShortArrayOrList::SQUARE_BRACKETS, + ], + ]; + } +}