From e8363e0efba59490116f7aaa596ea9dc73d5e1f2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 31 Oct 2024 14:00:08 +0100 Subject: [PATCH 1/2] Tests: add tests for the Util\Tokens::tokenName() method --- tests/Core/Util/Tokens/TokenNameTest.php | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/Core/Util/Tokens/TokenNameTest.php diff --git a/tests/Core/Util/Tokens/TokenNameTest.php b/tests/Core/Util/Tokens/TokenNameTest.php new file mode 100644 index 0000000000..435df07139 --- /dev/null +++ b/tests/Core/Util/Tokens/TokenNameTest.php @@ -0,0 +1,81 @@ + + * @copyright 2024 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Util\Tokens; + +use PHP_CodeSniffer\Util\Tokens; +use PHPUnit\Framework\TestCase; + +/** + * Tests for the \PHP_CodeSniffer\Util\Tokens::tokenName() method. + * + * @covers \PHP_CodeSniffer\Util\Tokens::tokenName + */ +final class TokenNameTest extends TestCase +{ + + + /** + * Test the method. + * + * @param int|string $tokenCode The PHP/PHPCS token code to get the name for. + * @param string $expected The expected token name. + * + * @dataProvider dataTokenName + * + * @return void + */ + public function testTokenName($tokenCode, $expected) + { + $this->assertSame($expected, Tokens::tokenName($tokenCode)); + + }//end testTokenName() + + + /** + * Data provider. + * + * @return array> + */ + public static function dataTokenName() + { + return [ + 'PHP native token: T_ECHO' => [ + 'tokenCode' => T_ECHO, + 'expected' => 'T_ECHO', + ], + 'PHP native token: T_FUNCTION' => [ + 'tokenCode' => T_FUNCTION, + 'expected' => 'T_FUNCTION', + ], + 'PHPCS native token: T_CLOSURE' => [ + 'tokenCode' => T_CLOSURE, + 'expected' => 'T_CLOSURE', + ], + 'PHPCS native token: T_STRING_CONCAT' => [ + 'tokenCode' => T_STRING_CONCAT, + 'expected' => 'T_STRING_CONCAT', + ], + + // Document the current behaviour for invalid input. + // This behaviour is subject to change. + 'Non-token integer passed' => [ + 'tokenCode' => 100000, + 'expected' => 'UNKNOWN', + ], + 'Non-token string passed' => [ + 'tokenCode' => 'something', + 'expected' => 'ing', + ], + ]; + + }//end dataTokenName() + + +}//end class From 7bdd340106176ca0338a7c6405ee11936d76bcfe Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 31 Oct 2024 14:55:38 +0100 Subject: [PATCH 2/2] Tests: add tests for the Util\Tokens::getHighestWeightedToken() method Includes fixing the docs for the method as it didn't represent correctly the actual behaviour of the function. --- src/Util/Tokens.php | 6 +- .../Tokens/GetHighestWeightedTokenTest.php | 162 ++++++++++++++++++ 2 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 tests/Core/Util/Tokens/GetHighestWeightedTokenTest.php diff --git a/src/Util/Tokens.php b/src/Util/Tokens.php index 5ec913df2e..5554cc9aae 100644 --- a/src/Util/Tokens.php +++ b/src/Util/Tokens.php @@ -777,12 +777,14 @@ public static function tokenName($token) * For example T_CLASS tokens appear very infrequently in a file, and * therefore have a high weighting. * - * Returns false if there are no weightings for any of the specified tokens. + * If there are no weightings for any of the specified tokens, the first token + * seen in the passed array will be returned. * * @param array $tokens The token types to get the highest weighted * type for. * - * @return int|false The highest weighted token. + * @return int The highest weighted token. + * On equal "weight", returns the first token of that particular weight. */ public static function getHighestWeightedToken(array $tokens) { diff --git a/tests/Core/Util/Tokens/GetHighestWeightedTokenTest.php b/tests/Core/Util/Tokens/GetHighestWeightedTokenTest.php new file mode 100644 index 0000000000..f03d20a393 --- /dev/null +++ b/tests/Core/Util/Tokens/GetHighestWeightedTokenTest.php @@ -0,0 +1,162 @@ + + * @copyright 2024 PHPCSStandards and contributors + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Util\Tokens; + +use PHP_CodeSniffer\Util\Tokens; +use PHPUnit\Framework\TestCase; + +/** + * Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method. + * + * @covers \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken + */ +final class GetHighestWeightedTokenTest extends TestCase +{ + + + /** + * Test the method. + * + * @param array $tokens The tokens to find the heighest weighted one. + * @param int|false $expected The expected function return value. + * + * @dataProvider dataGetHighestWeightedToken + * + * @return void + */ + public function testGetHighestWeightedToken($tokens, $expected) + { + $this->assertSame($expected, Tokens::getHighestWeightedToken($tokens)); + + }//end testGetHighestWeightedToken() + + + /** + * Data provider. + * + * @return array> + */ + public static function dataGetHighestWeightedToken() + { + $data = [ + 'Array of non-tokens passed, returns first' => [ + 'tokens' => [ + PHP_SAPI, + PHP_MAJOR_VERSION, + PHP_OS, + ], + 'expected' => PHP_SAPI, + ], + 'No weightings available for any of the selected tokens, first one wins' => [ + 'tokens' => [ + T_VARIABLE, + T_STRING, + T_EXTENDS, + T_IMPLEMENTS, + ], + 'expected' => T_VARIABLE, + ], + 'single token always returns that token' => [ + 'tokens' => [T_VARIABLE], + 'expected' => T_VARIABLE, + ], + 'Unknown and known token, known token wins' => [ + 'tokens' => [ + T_VARIABLE, + T_SELF, + ], + 'expected' => T_SELF, + ], + 'Known and unknown token, known token wins' => [ + 'tokens' => [ + T_CLOSURE, + T_STRING, + ], + 'expected' => T_CLOSURE, + ], + 'Two tokens with equal weights passed, first one wins' => [ + 'tokens' => [ + T_CLOSURE, + T_FUNCTION, + ], + 'expected' => T_CLOSURE, + ], + 'Five tokens with equal weights passed, first one wins' => [ + 'tokens' => [ + T_NAMESPACE, + T_TRAIT, + T_ENUM, + T_CLASS, + T_INTERFACE, + ], + 'expected' => T_NAMESPACE, + ], + 'Tokens with different weights passed, heightest (25) wins' => [ + 'tokens' => [ + T_BITWISE_OR, + T_SELF, + T_MUL_EQUAL, + ], + 'expected' => T_SELF, + ], + 'Tokens with different weights passed, heightest (50) wins' => [ + 'tokens' => [ + T_BITWISE_XOR, + T_CATCH, + T_SPACESHIP, + T_PARENT, + ], + 'expected' => T_CATCH, + ], + ]; + + $high100 = [ + T_MULTIPLY, + T_BITWISE_AND, + T_SELF, + T_FOREACH, + T_CLOSURE, + ]; + $data['Tokens with different weights passed, ordered low-high, heightest (100) wins'] = [ + 'tokens' => $high100, + 'expected' => T_CLOSURE, + ]; + + shuffle($high100); + $data['Tokens with different weights passed, order random, heightest (100) wins'] = [ + 'tokens' => $high100, + 'expected' => T_CLOSURE, + ]; + + $high1000 = [ + T_ENUM, + T_FUNCTION, + T_ELSEIF, + T_PARENT, + T_BITWISE_OR, + T_MODULUS, + ]; + $data['Tokens with different weights passed, ordered low-high, heightest (1000) wins'] = [ + 'tokens' => $high1000, + 'expected' => T_ENUM, + ]; + + shuffle($high1000); + $data['Tokens with different weights passed, order random, heightest (1000) wins'] = [ + 'tokens' => $high1000, + 'expected' => T_ENUM, + ]; + + return $data; + + }//end dataGetHighestWeightedToken() + + +}//end class