diff --git a/PHPCSUtils/Utils/GetTokensAsString.php b/PHPCSUtils/Utils/GetTokensAsString.php index b0ea65d5..0be4e8ff 100644 --- a/PHPCSUtils/Utils/GetTokensAsString.php +++ b/PHPCSUtils/Utils/GetTokensAsString.php @@ -33,15 +33,7 @@ class GetTokensAsString * Retrieve the tab-replaced content of the tokens from the specified start position in * the token stack to the specified end position (inclusive). * - * This is the default behaviour for PHPCS. - * - * If the `tabWidth` is set, either via a (custom) ruleset, the config file or by passing it - * on the command-line, PHPCS will automatically replace tabs with spaces. - * The `'content'` index key in the `$tokens` array will contain the tab-replaced content. - * The `'orig_content'` index key in the `$tokens` array will contain the original content. - * - * @see \PHP_CodeSniffer\Files\File::getTokensAsString() Similar length-based function. - * @see \PHPCSUtils\BackCompat\BCFile::getTokensAsString() Cross-version compatible version of the original. + * Alias for the {@see \PHPCSUtils\Utils\GetTokensAsString::tabReplaced()} method. * * @since 1.0.0 * @@ -55,14 +47,22 @@ class GetTokensAsString */ public static function normal(File $phpcsFile, $start, $end) { - return self::getString($phpcsFile, $start, $end); + return self::tabReplaced($phpcsFile, $start, $end); } /** * Retrieve the tab-replaced content of the tokens from the specified start position in * the token stack to the specified end position (inclusive). * - * Alias for the {@see \PHPCSUtils\Utils\GetTokensAsString::normal()} method. + * This is the default behaviour for PHPCS. + * + * If the `tabWidth` is set, either via a (custom) ruleset, the config file or by passing it + * on the command-line, PHPCS will automatically replace tabs with spaces. + * The `'content'` index key in the `$tokens` array will contain the tab-replaced content. + * The `'orig_content'` index key in the `$tokens` array will contain the original content. + * + * @see \PHP_CodeSniffer\Files\File::getTokensAsString() Similar length-based function. + * @see \PHPCSUtils\BackCompat\BCFile::getTokensAsString() Cross-version compatible version of the original. * * @since 1.0.0 * @@ -76,14 +76,14 @@ public static function normal(File $phpcsFile, $start, $end) */ public static function tabReplaced(File $phpcsFile, $start, $end) { - return self::normal($phpcsFile, $start, $end); + return self::getString($phpcsFile, $start, $end); } /** * Retrieve the original content of the tokens from the specified start position in * the token stack to the specified end position (inclusive). * - * In contrast to the {@see GetTokensAsString::normal()} method, this method will return + * In contrast to the {@see GetTokensAsString::tabReplaced()} method, this method will return * the original token content for the specified tokens. * That means that if the original content contained tabs, the return value of this function * will also contain tabs. @@ -135,7 +135,7 @@ public static function noComments(File $phpcsFile, $start, $end) * Retrieve the code-tokens only content of the tokens from the specified start position * in the token stack to the specified end position (inclusive) without whitespace or comments. * - * This is, for instance, useful to retrieve a namespace name without stray whitespace or comments. + * This is useful, for instance, to retrieve a namespace name without stray whitespace or comments. * Use this function selectively and with care! * * @see \PHP_CodeSniffer\Files\File::getTokensAsString() Loosely related function. diff --git a/Tests/Utils/GetTokensAsString/GetTokensAsStringTest.php b/Tests/Utils/GetTokensAsString/GetTokensAsStringTest.php index c301bd69..50b4ab1f 100644 --- a/Tests/Utils/GetTokensAsString/GetTokensAsStringTest.php +++ b/Tests/Utils/GetTokensAsString/GetTokensAsStringTest.php @@ -125,8 +125,8 @@ public function testNormal($testMarker, $startTokenType, $expected) $start = $this->getTargetToken($testMarker, $startTokenType); $end = $this->getTargetToken($testMarker, \T_SEMICOLON); - $result = GetTokensAsString::normal(self::$phpcsFile, $start, $end); - $this->assertSame($expected['normal'], $result); + $result = GetTokensAsString::tabReplaced(self::$phpcsFile, $start, $end); + $this->assertSame($expected['tab_replaced'], $result); } /** @@ -248,81 +248,81 @@ public function dataGetTokensAsString() 'marker' => '/* testNamespace */', 'type' => \T_NAMESPACE, 'expected' => [ - 'normal' => 'namespace Foo\Bar\Baz;', - 'orig' => 'namespace Foo\Bar\Baz;', - 'no_comments' => 'namespace Foo\Bar\Baz;', - 'no_empties' => 'namespaceFoo\Bar\Baz;', - 'compact' => 'namespace Foo\Bar\Baz;', - 'compact_nc' => 'namespace Foo\Bar\Baz;', + 'tab_replaced' => 'namespace Foo\Bar\Baz;', + 'orig' => 'namespace Foo\Bar\Baz;', + 'no_comments' => 'namespace Foo\Bar\Baz;', + 'no_empties' => 'namespaceFoo\Bar\Baz;', + 'compact' => 'namespace Foo\Bar\Baz;', + 'compact_nc' => 'namespace Foo\Bar\Baz;', ], ], 'use-with-comments' => [ 'marker' => '/* testUseWithComments */', 'type' => \T_STRING, 'expected' => [ - 'normal' => 'Foo /*comment*/ \ Bar + 'tab_replaced' => 'Foo /*comment*/ \ Bar // phpcs:ignore Stnd.Cat.Sniff -- For reasons. \ Bah;', - 'orig' => 'Foo /*comment*/ \ Bar + 'orig' => 'Foo /*comment*/ \ Bar // phpcs:ignore Stnd.Cat.Sniff -- For reasons. \ Bah;', - 'no_comments' => 'Foo \ Bar + 'no_comments' => 'Foo \ Bar \ Bah;', - 'no_empties' => 'Foo\Bar\Bah;', - 'compact' => 'Foo /*comment*/ \ Bar // phpcs:ignore Stnd.Cat.Sniff -- For reasons. + 'no_empties' => 'Foo\Bar\Bah;', + 'compact' => 'Foo /*comment*/ \ Bar // phpcs:ignore Stnd.Cat.Sniff -- For reasons. \ Bah;', - 'compact_nc' => 'Foo \ Bar \ Bah;', + 'compact_nc' => 'Foo \ Bar \ Bah;', ], ], 'calculation' => [ 'marker' => '/* testCalculation */', 'type' => \T_LNUMBER, 'expected' => [ - 'normal' => '1 + 2 + + 'tab_replaced' => '1 + 2 + // Comment. 3 + 4 + 5 + 6 + 7 > 20;', - 'orig' => '1 + 2 + + 'orig' => '1 + 2 + // Comment. 3 + 4 + 5 + 6 + 7 > 20;', - 'no_comments' => '1 + 2 + + 'no_comments' => '1 + 2 + 3 + 4 + 5 + 6 + 7 > 20;', - 'no_empties' => '1+2+3+4+5+6+7>20;', - 'compact' => '1 + 2 + // Comment. + 'no_empties' => '1+2+3+4+5+6+7>20;', + 'compact' => '1 + 2 + // Comment. 3 + 4 + 5 + 6 + 7 > 20;', - 'compact_nc' => '1 + 2 + 3 + 4 + 5 + 6 + 7 > 20;', + 'compact_nc' => '1 + 2 + 3 + 4 + 5 + 6 + 7 > 20;', ], ], 'echo-with-tabs' => [ 'marker' => '/* testEchoWithTabs */', 'type' => \T_ECHO, 'expected' => [ - 'normal' => 'echo \'foo\', + 'tab_replaced' => 'echo \'foo\', \'bar\' , \'baz\';', - 'orig' => 'echo \'foo\', + 'orig' => 'echo \'foo\', \'bar\' , \'baz\';', - 'no_comments' => 'echo \'foo\', + 'no_comments' => 'echo \'foo\', \'bar\' , \'baz\';', - 'no_empties' => 'echo\'foo\',\'bar\',\'baz\';', - 'compact' => 'echo \'foo\', \'bar\' , \'baz\';', - 'compact_nc' => 'echo \'foo\', \'bar\' , \'baz\';', + 'no_empties' => 'echo\'foo\',\'bar\',\'baz\';', + 'compact' => 'echo \'foo\', \'bar\' , \'baz\';', + 'compact_nc' => 'echo \'foo\', \'bar\' , \'baz\';', ], ], 'end-of-file' => [ 'marker' => '/* testEndOfFile */', 'type' => \T_ECHO, 'expected' => [ - 'normal' => 'echo $foo;', - 'orig' => 'echo $foo;', - 'no_comments' => 'echo $foo;', - 'no_empties' => 'echo$foo;', - 'compact' => 'echo $foo;', - 'compact_nc' => 'echo $foo;', + 'tab_replaced' => 'echo $foo;', + 'orig' => 'echo $foo;', + 'no_comments' => 'echo $foo;', + 'no_empties' => 'echo$foo;', + 'compact' => 'echo $foo;', + 'compact_nc' => 'echo $foo;', ], ], ];