TokenTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Tokenizer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\CT;
  15. use PhpCsFixer\Tokenizer\Token;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Tokenizer\Token
  22. */
  23. final class TokenTest extends TestCase
  24. {
  25. /**
  26. * @param mixed $input
  27. *
  28. * @dataProvider provideConstructorValidationCases
  29. */
  30. public function testConstructorValidation($input): void
  31. {
  32. $this->expectException(\InvalidArgumentException::class);
  33. new Token($input);
  34. }
  35. public static function provideConstructorValidationCases(): iterable
  36. {
  37. yield [null];
  38. yield [123];
  39. yield [new \stdClass()];
  40. yield [['asd', 'asd']];
  41. yield [[null, 'asd']];
  42. yield [[new \stdClass(), 'asd']];
  43. yield [[T_WHITESPACE, null]];
  44. yield [[T_WHITESPACE, 123]];
  45. yield [[T_WHITESPACE, '']];
  46. yield [[T_WHITESPACE, new \stdClass()]];
  47. }
  48. public function testGetPrototype(): void
  49. {
  50. self::assertSame(self::getBraceTokenPrototype(), self::getBraceToken()->getPrototype());
  51. self::assertSame(self::getForeachTokenPrototype(), self::getForeachToken()->getPrototype());
  52. }
  53. public function testIsArray(): void
  54. {
  55. self::assertFalse(self::getBraceToken()->isArray());
  56. self::assertTrue(self::getForeachToken()->isArray());
  57. }
  58. /**
  59. * @dataProvider provideIsCastCases
  60. */
  61. public function testIsCast(Token $token, bool $isCast): void
  62. {
  63. self::assertSame($isCast, $token->isCast());
  64. }
  65. public static function provideIsCastCases(): iterable
  66. {
  67. yield [self::getBraceToken(), false];
  68. yield [self::getForeachToken(), false];
  69. yield [new Token([T_ARRAY_CAST, '(array)', 1]), true];
  70. yield [new Token([T_BOOL_CAST, '(bool)', 1]), true];
  71. yield [new Token([T_DOUBLE_CAST, '(double)', 1]), true];
  72. yield [new Token([T_INT_CAST, '(int)', 1]), true];
  73. yield [new Token([T_OBJECT_CAST, '(object)', 1]), true];
  74. yield [new Token([T_STRING_CAST, '(string)', 1]), true];
  75. yield [new Token([T_UNSET_CAST, '(unset)', 1]), true];
  76. }
  77. /**
  78. * @dataProvider provideIsClassyCases
  79. */
  80. public function testIsClassy(Token $token, bool $isClassy): void
  81. {
  82. self::assertSame($isClassy, $token->isClassy());
  83. }
  84. public static function provideIsClassyCases(): iterable
  85. {
  86. yield [self::getBraceToken(), false];
  87. yield [self::getForeachToken(), false];
  88. yield [new Token([T_CLASS, 'class', 1]), true];
  89. yield [new Token([T_INTERFACE, 'interface', 1]), true];
  90. yield [new Token([T_TRAIT, 'trait', 1]), true];
  91. }
  92. /**
  93. * @requires PHP 8.1
  94. */
  95. public function testEnumIsClassy(): void
  96. {
  97. $enumToken = new Token([T_ENUM, 'enum', 1]);
  98. self::assertTrue($enumToken->isClassy());
  99. }
  100. /**
  101. * @dataProvider provideIsCommentCases
  102. */
  103. public function testIsComment(Token $token, bool $isComment): void
  104. {
  105. self::assertSame($isComment, $token->isComment());
  106. }
  107. public static function provideIsCommentCases(): iterable
  108. {
  109. yield [self::getBraceToken(), false];
  110. yield [self::getForeachToken(), false];
  111. yield [new Token([T_COMMENT, '/* comment */', 1]), true];
  112. yield [new Token([T_DOC_COMMENT, '/** docs */', 1]), true];
  113. // @TODO: drop condition when PHP 8.0+ is required
  114. if (\defined('T_ATTRIBUTE')) {
  115. yield [new Token([T_ATTRIBUTE, '#[', 1]), false];
  116. }
  117. }
  118. /**
  119. * @dataProvider provideIsObjectOperatorCases
  120. */
  121. public function testIsObjectOperator(Token $token, bool $isObjectOperator): void
  122. {
  123. self::assertSame($isObjectOperator, $token->isObjectOperator());
  124. }
  125. public static function provideIsObjectOperatorCases(): iterable
  126. {
  127. yield [self::getBraceToken(), false];
  128. yield [self::getForeachToken(), false];
  129. yield [new Token([T_COMMENT, '/* comment */']), false];
  130. yield [new Token([T_DOUBLE_COLON, '::']), false];
  131. yield [new Token([T_OBJECT_OPERATOR, '->']), true];
  132. if (\defined('T_NULLSAFE_OBJECT_OPERATOR')) {
  133. yield [new Token([T_NULLSAFE_OBJECT_OPERATOR, '?->']), true];
  134. }
  135. }
  136. public function testIsGivenKind(): void
  137. {
  138. $braceToken = self::getBraceToken();
  139. $foreachToken = self::getForeachToken();
  140. self::assertFalse($braceToken->isGivenKind(T_FOR));
  141. self::assertFalse($braceToken->isGivenKind(T_FOREACH));
  142. self::assertFalse($braceToken->isGivenKind([T_FOR]));
  143. self::assertFalse($braceToken->isGivenKind([T_FOREACH]));
  144. self::assertFalse($braceToken->isGivenKind([T_FOR, T_FOREACH]));
  145. self::assertFalse($foreachToken->isGivenKind(T_FOR));
  146. self::assertTrue($foreachToken->isGivenKind(T_FOREACH));
  147. self::assertFalse($foreachToken->isGivenKind([T_FOR]));
  148. self::assertTrue($foreachToken->isGivenKind([T_FOREACH]));
  149. self::assertTrue($foreachToken->isGivenKind([T_FOR, T_FOREACH]));
  150. }
  151. public function testIsKeywords(): void
  152. {
  153. self::assertTrue(self::getForeachToken()->isKeyword());
  154. self::assertFalse(self::getBraceToken()->isKeyword());
  155. }
  156. /**
  157. * @param ?int $tokenId
  158. *
  159. * @dataProvider provideIsMagicConstantCases
  160. */
  161. public function testIsMagicConstant(?int $tokenId, string $content, bool $isConstant = true): void
  162. {
  163. $token = new Token(
  164. null === $tokenId ? $content : [$tokenId, $content]
  165. );
  166. self::assertSame($isConstant, $token->isMagicConstant());
  167. }
  168. public static function provideIsMagicConstantCases(): iterable
  169. {
  170. $cases = [
  171. [T_CLASS_C, '__CLASS__'],
  172. [T_DIR, '__DIR__'],
  173. [T_FILE, '__FILE__'],
  174. [T_FUNC_C, '__FUNCTION__'],
  175. [T_LINE, '__LINE__'],
  176. [T_METHOD_C, '__METHOD__'],
  177. [T_NS_C, '__NAMESPACE__'],
  178. [T_TRAIT_C, '__TRAIT__'],
  179. ];
  180. foreach ($cases as $case) {
  181. yield [$case[0], strtolower($case[1])];
  182. }
  183. foreach ([self::getForeachToken(), self::getBraceToken()] as $token) {
  184. yield [$token->getId(), $token->getContent(), false];
  185. yield [$token->getId(), strtolower($token->getContent()), false];
  186. }
  187. }
  188. /**
  189. * @dataProvider provideIsNativeConstantCases
  190. */
  191. public function testIsNativeConstant(Token $token, bool $isNativeConstant): void
  192. {
  193. self::assertSame($isNativeConstant, $token->isNativeConstant());
  194. }
  195. public static function provideIsNativeConstantCases(): iterable
  196. {
  197. yield [self::getBraceToken(), false];
  198. yield [self::getForeachToken(), false];
  199. yield [new Token([T_STRING, 'null', 1]), true];
  200. yield [new Token([T_STRING, 'false', 1]), true];
  201. yield [new Token([T_STRING, 'true', 1]), true];
  202. yield [new Token([T_STRING, 'tRuE', 1]), true];
  203. yield [new Token([T_STRING, 'TRUE', 1]), true];
  204. }
  205. /**
  206. * @dataProvider provideIsWhitespaceCases
  207. */
  208. public function testIsWhitespace(Token $token, bool $isWhitespace, ?string $whitespaces = null): void
  209. {
  210. if (null !== $whitespaces) {
  211. self::assertSame($isWhitespace, $token->isWhitespace($whitespaces));
  212. } else {
  213. self::assertSame($isWhitespace, $token->isWhitespace(null));
  214. self::assertSame($isWhitespace, $token->isWhitespace());
  215. }
  216. }
  217. public static function provideIsWhitespaceCases(): iterable
  218. {
  219. yield [self::getBraceToken(), false];
  220. yield [self::getForeachToken(), false];
  221. yield [new Token(' '), true];
  222. yield [new Token("\t "), true];
  223. yield [new Token("\t "), false, ' '];
  224. yield [new Token([T_WHITESPACE, "\r", 1]), true];
  225. yield [new Token([T_WHITESPACE, "\0", 1]), true];
  226. yield [new Token([T_WHITESPACE, "\x0B", 1]), true];
  227. yield [new Token([T_WHITESPACE, "\n", 1]), true];
  228. yield [new Token([T_WHITESPACE, "\n", 1]), false, " \t"];
  229. }
  230. /**
  231. * @param mixed $prototype
  232. *
  233. * @dataProvider provideCreatingTokenCases
  234. */
  235. public function testCreatingToken($prototype, ?int $expectedId, ?string $expectedContent, ?bool $expectedIsArray, ?string $expectedExceptionClass = null): void
  236. {
  237. if (null !== $expectedExceptionClass) {
  238. $this->expectException($expectedExceptionClass);
  239. }
  240. $token = new Token($prototype);
  241. self::assertSame($expectedId, $token->getId());
  242. self::assertSame($expectedContent, $token->getContent());
  243. self::assertSame($expectedIsArray, $token->isArray());
  244. }
  245. public static function provideCreatingTokenCases(): iterable
  246. {
  247. yield [[T_FOREACH, 'foreach'], T_FOREACH, 'foreach', true];
  248. yield ['(', null, '(', false];
  249. yield [123, null, null, null, \InvalidArgumentException::class];
  250. yield [false, null, null, null, \InvalidArgumentException::class];
  251. yield [null, null, null, null, \InvalidArgumentException::class];
  252. }
  253. public function testEqualsDefaultIsCaseSensitive(): void
  254. {
  255. $token = new Token([T_FUNCTION, 'function', 1]);
  256. self::assertTrue($token->equals([T_FUNCTION, 'function']));
  257. self::assertFalse($token->equals([T_FUNCTION, 'Function']));
  258. }
  259. /**
  260. * @param array{0: int, 1?: string}|string|Token $other
  261. *
  262. * @dataProvider provideEqualsCases
  263. */
  264. public function testEquals(Token $token, bool $equals, $other, bool $caseSensitive = true): void
  265. {
  266. self::assertSame($equals, $token->equals($other, $caseSensitive));
  267. }
  268. public static function provideEqualsCases(): iterable
  269. {
  270. $brace = self::getBraceToken();
  271. $function = new Token([T_FUNCTION, 'function', 1]);
  272. yield [$brace, false, '!'];
  273. yield [$brace, false, '!', false];
  274. yield [$brace, true, '('];
  275. yield [$brace, true, '(', false];
  276. yield [$function, false, '('];
  277. yield [$function, false, '(', false];
  278. yield [$function, false, [T_NAMESPACE]];
  279. yield [$function, false, [T_NAMESPACE], false];
  280. yield [$function, false, [T_VARIABLE, 'function']];
  281. yield [$function, false, [T_VARIABLE, 'function'], false];
  282. yield [$function, false, [T_VARIABLE, 'Function']];
  283. yield [$function, false, [T_VARIABLE, 'Function'], false];
  284. yield [$function, true, [T_FUNCTION]];
  285. yield [$function, true, [T_FUNCTION], false];
  286. yield [$function, true, [T_FUNCTION, 'function']];
  287. yield [$function, true, [T_FUNCTION, 'function'], false];
  288. yield [$function, false, [T_FUNCTION, 'Function']];
  289. yield [$function, true, [T_FUNCTION, 'Function'], false];
  290. yield [$function, false, [T_FUNCTION, 'junction'], false];
  291. yield [$function, true, new Token([T_FUNCTION, 'function'])];
  292. yield [$function, false, new Token([T_FUNCTION, 'Function'])];
  293. yield [$function, true, new Token([T_FUNCTION, 'Function']), false];
  294. // if it is an array any additional field is checked too
  295. yield [$function, false, [T_FUNCTION, 'function', 'unexpected']];
  296. yield [new Token('&'), true, '&'];
  297. if (\defined('T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG')) { // @TODO: drop condition with new MAJOR release 4.0
  298. yield [new Token('&'), true, new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  299. yield [new Token('&'), true, new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  300. yield [new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, '&'];
  301. yield [new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  302. yield [new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, '&'];
  303. yield [new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  304. }
  305. }
  306. public function testEqualsAnyDefaultIsCaseSensitive(): void
  307. {
  308. $token = new Token([T_FUNCTION, 'function', 1]);
  309. self::assertTrue($token->equalsAny([[T_FUNCTION, 'function']]));
  310. self::assertFalse($token->equalsAny([[T_FUNCTION, 'Function']]));
  311. }
  312. /**
  313. * @param list<array{0: int, 1?: string}|string|Token> $other
  314. *
  315. * @dataProvider provideEqualsAnyCases
  316. */
  317. public function testEqualsAny(bool $equalsAny, array $other, bool $caseSensitive = true): void
  318. {
  319. $token = new Token([T_FUNCTION, 'function', 1]);
  320. self::assertSame($equalsAny, $token->equalsAny($other, $caseSensitive));
  321. }
  322. public static function provideEqualsAnyCases(): iterable
  323. {
  324. $brace = self::getBraceToken();
  325. $foreach = self::getForeachToken();
  326. yield [false, []];
  327. yield [false, [$brace]];
  328. yield [false, [$brace, $foreach]];
  329. yield [true, [$brace, $foreach, [T_FUNCTION]]];
  330. yield [true, [$brace, $foreach, [T_FUNCTION, 'function']]];
  331. yield [false, [$brace, $foreach, [T_FUNCTION, 'Function']]];
  332. yield [true, [$brace, $foreach, [T_FUNCTION, 'Function']], false];
  333. yield [false, [[T_VARIABLE, 'junction'], [T_FUNCTION, 'junction']], false];
  334. }
  335. /**
  336. * @param bool|list<bool> $caseSensitive
  337. *
  338. * @dataProvider provideIsKeyCaseSensitiveCases
  339. */
  340. public function testIsKeyCaseSensitive(bool $isKeyCaseSensitive, $caseSensitive, int $key): void
  341. {
  342. self::assertSame($isKeyCaseSensitive, Token::isKeyCaseSensitive($caseSensitive, $key));
  343. }
  344. public static function provideIsKeyCaseSensitiveCases(): iterable
  345. {
  346. yield [true, true, 0];
  347. yield [true, true, 1];
  348. yield [true, [], 0];
  349. yield [true, [true], 0];
  350. yield [true, [false, true], 1];
  351. yield [true, [false, true, false], 1];
  352. yield [true, [false], 10];
  353. yield [false, false, 10];
  354. yield [false, [false], 0];
  355. yield [false, [true, false], 1];
  356. yield [false, [true, false, true], 1];
  357. yield [false, [1 => false], 1];
  358. }
  359. /**
  360. * @dataProvider provideTokenGetNameForIdCases
  361. */
  362. public function testTokenGetNameForId(?string $expected, int $id): void
  363. {
  364. self::assertSame($expected, Token::getNameForId($id));
  365. }
  366. public static function provideTokenGetNameForIdCases(): iterable
  367. {
  368. yield [
  369. null,
  370. -1,
  371. ];
  372. yield [
  373. 'T_CLASS',
  374. T_CLASS,
  375. ];
  376. yield [
  377. 'CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE',
  378. CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
  379. ];
  380. }
  381. /**
  382. * @dataProvider provideGetNameCases
  383. */
  384. public function testGetName(Token $token, ?string $expected = null): void
  385. {
  386. self::assertSame($expected, $token->getName());
  387. }
  388. public static function provideGetNameCases(): iterable
  389. {
  390. yield [
  391. new Token([T_FUNCTION, 'function', 1]),
  392. 'T_FUNCTION',
  393. ];
  394. yield [
  395. new Token(')'),
  396. null,
  397. ];
  398. yield [
  399. new Token(''),
  400. null,
  401. ];
  402. }
  403. /**
  404. * @param array<string, mixed> $expected
  405. *
  406. * @dataProvider provideToArrayCases
  407. */
  408. public function testToArray(Token $token, array $expected): void
  409. {
  410. self::assertSame($expected, $token->toArray());
  411. }
  412. public static function provideToArrayCases(): iterable
  413. {
  414. yield [
  415. new Token([T_FUNCTION, 'function', 1]),
  416. [
  417. 'id' => T_FUNCTION,
  418. 'name' => 'T_FUNCTION',
  419. 'content' => 'function',
  420. 'isArray' => true,
  421. 'changed' => false,
  422. ],
  423. ];
  424. yield [
  425. new Token(')'),
  426. [
  427. 'id' => null,
  428. 'name' => null,
  429. 'content' => ')',
  430. 'isArray' => false,
  431. 'changed' => false,
  432. ],
  433. ];
  434. yield [
  435. new Token(''),
  436. [
  437. 'id' => null,
  438. 'name' => null,
  439. 'content' => '',
  440. 'isArray' => false,
  441. 'changed' => false,
  442. ],
  443. ];
  444. }
  445. public function testGetClassyTokenKinds(): void
  446. {
  447. if (\defined('T_ENUM')) {
  448. self::assertSame([T_CLASS, T_TRAIT, T_INTERFACE, T_ENUM], Token::getClassyTokenKinds());
  449. } else {
  450. self::assertSame([T_CLASS, T_TRAIT, T_INTERFACE], Token::getClassyTokenKinds());
  451. }
  452. }
  453. private static function getBraceToken(): Token
  454. {
  455. return new Token(self::getBraceTokenPrototype());
  456. }
  457. private static function getBraceTokenPrototype(): string
  458. {
  459. return '(';
  460. }
  461. private static function getForeachToken(): Token
  462. {
  463. return new Token(self::getForeachTokenPrototype());
  464. }
  465. /**
  466. * @return array{int, string}
  467. */
  468. private static function getForeachTokenPrototype(): array
  469. {
  470. static $prototype = [T_FOREACH, 'foreach'];
  471. return $prototype;
  472. }
  473. }