TokenTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. /**
  66. * @return iterable<array{Token, bool}>
  67. */
  68. public static function provideIsCastCases(): iterable
  69. {
  70. yield [self::getBraceToken(), false];
  71. yield [self::getForeachToken(), false];
  72. yield [new Token([T_ARRAY_CAST, '(array)', 1]), true];
  73. yield [new Token([T_BOOL_CAST, '(bool)', 1]), true];
  74. yield [new Token([T_DOUBLE_CAST, '(double)', 1]), true];
  75. yield [new Token([T_INT_CAST, '(int)', 1]), true];
  76. yield [new Token([T_OBJECT_CAST, '(object)', 1]), true];
  77. yield [new Token([T_STRING_CAST, '(string)', 1]), true];
  78. yield [new Token([T_UNSET_CAST, '(unset)', 1]), true];
  79. }
  80. /**
  81. * @dataProvider provideIsClassyCases
  82. */
  83. public function testIsClassy(Token $token, bool $isClassy): void
  84. {
  85. self::assertSame($isClassy, $token->isClassy());
  86. }
  87. /**
  88. * @return iterable<array{Token, bool}>
  89. */
  90. public static function provideIsClassyCases(): iterable
  91. {
  92. yield [self::getBraceToken(), false];
  93. yield [self::getForeachToken(), false];
  94. yield [new Token([T_CLASS, 'class', 1]), true];
  95. yield [new Token([T_INTERFACE, 'interface', 1]), true];
  96. yield [new Token([T_TRAIT, 'trait', 1]), true];
  97. }
  98. /**
  99. * @requires PHP 8.1
  100. */
  101. public function testEnumIsClassy(): void
  102. {
  103. $enumToken = new Token([T_ENUM, 'enum', 1]);
  104. self::assertTrue($enumToken->isClassy());
  105. }
  106. /**
  107. * @dataProvider provideIsCommentCases
  108. */
  109. public function testIsComment(Token $token, bool $isComment): void
  110. {
  111. self::assertSame($isComment, $token->isComment());
  112. }
  113. /**
  114. * @return iterable<array{Token, bool}>
  115. */
  116. public static function provideIsCommentCases(): iterable
  117. {
  118. yield [self::getBraceToken(), false];
  119. yield [self::getForeachToken(), false];
  120. yield [new Token([T_COMMENT, '/* comment */', 1]), true];
  121. yield [new Token([T_DOC_COMMENT, '/** docs */', 1]), true];
  122. // @TODO: drop condition when PHP 8.0+ is required
  123. if (\defined('T_ATTRIBUTE')) {
  124. yield [new Token([T_ATTRIBUTE, '#[', 1]), false];
  125. }
  126. }
  127. /**
  128. * @dataProvider provideIsObjectOperatorCases
  129. */
  130. public function testIsObjectOperator(Token $token, bool $isObjectOperator): void
  131. {
  132. self::assertSame($isObjectOperator, $token->isObjectOperator());
  133. }
  134. /**
  135. * @return iterable<array{Token, bool}>
  136. */
  137. public static function provideIsObjectOperatorCases(): iterable
  138. {
  139. yield [self::getBraceToken(), false];
  140. yield [self::getForeachToken(), false];
  141. yield [new Token([T_COMMENT, '/* comment */']), false];
  142. yield [new Token([T_DOUBLE_COLON, '::']), false];
  143. yield [new Token([T_OBJECT_OPERATOR, '->']), true];
  144. if (\defined('T_NULLSAFE_OBJECT_OPERATOR')) {
  145. yield [new Token([T_NULLSAFE_OBJECT_OPERATOR, '?->']), true];
  146. }
  147. }
  148. public function testIsGivenKind(): void
  149. {
  150. $braceToken = self::getBraceToken();
  151. $foreachToken = self::getForeachToken();
  152. self::assertFalse($braceToken->isGivenKind(T_FOR));
  153. self::assertFalse($braceToken->isGivenKind(T_FOREACH));
  154. self::assertFalse($braceToken->isGivenKind([T_FOR]));
  155. self::assertFalse($braceToken->isGivenKind([T_FOREACH]));
  156. self::assertFalse($braceToken->isGivenKind([T_FOR, T_FOREACH]));
  157. self::assertFalse($foreachToken->isGivenKind(T_FOR));
  158. self::assertTrue($foreachToken->isGivenKind(T_FOREACH));
  159. self::assertFalse($foreachToken->isGivenKind([T_FOR]));
  160. self::assertTrue($foreachToken->isGivenKind([T_FOREACH]));
  161. self::assertTrue($foreachToken->isGivenKind([T_FOR, T_FOREACH]));
  162. }
  163. public function testIsKeywords(): void
  164. {
  165. self::assertTrue(self::getForeachToken()->isKeyword());
  166. self::assertFalse(self::getBraceToken()->isKeyword());
  167. }
  168. /**
  169. * @dataProvider provideIsMagicConstantCases
  170. */
  171. public function testIsMagicConstant(?int $tokenId, string $content, bool $isConstant = true): void
  172. {
  173. $token = new Token(
  174. null === $tokenId ? $content : [$tokenId, $content]
  175. );
  176. self::assertSame($isConstant, $token->isMagicConstant());
  177. }
  178. /**
  179. * @return iterable<array{0: null|int, 1: string, 2?: bool}>
  180. */
  181. public static function provideIsMagicConstantCases(): iterable
  182. {
  183. $cases = [
  184. [T_CLASS_C, '__CLASS__'],
  185. [T_DIR, '__DIR__'],
  186. [T_FILE, '__FILE__'],
  187. [T_FUNC_C, '__FUNCTION__'],
  188. [T_LINE, '__LINE__'],
  189. [T_METHOD_C, '__METHOD__'],
  190. [T_NS_C, '__NAMESPACE__'],
  191. [T_TRAIT_C, '__TRAIT__'],
  192. ];
  193. foreach ($cases as $case) {
  194. yield [$case[0], strtolower($case[1])];
  195. }
  196. $foreachToken = self::getForeachToken();
  197. yield [$foreachToken->getId(), $foreachToken->getContent(), false];
  198. yield [$foreachToken->getId(), strtoupper($foreachToken->getContent()), false];
  199. $braceToken = self::getBraceToken();
  200. yield [$braceToken->getId(), $braceToken->getContent(), false];
  201. }
  202. /**
  203. * @dataProvider provideIsNativeConstantCases
  204. */
  205. public function testIsNativeConstant(Token $token, bool $isNativeConstant): void
  206. {
  207. self::assertSame($isNativeConstant, $token->isNativeConstant());
  208. }
  209. /**
  210. * @return iterable<array{Token, bool}>
  211. */
  212. public static function provideIsNativeConstantCases(): iterable
  213. {
  214. yield [self::getBraceToken(), false];
  215. yield [self::getForeachToken(), false];
  216. yield [new Token([T_STRING, 'null', 1]), true];
  217. yield [new Token([T_STRING, 'false', 1]), true];
  218. yield [new Token([T_STRING, 'true', 1]), true];
  219. yield [new Token([T_STRING, 'tRuE', 1]), true];
  220. yield [new Token([T_STRING, 'TRUE', 1]), true];
  221. }
  222. /**
  223. * @dataProvider provideIsWhitespaceCases
  224. */
  225. public function testIsWhitespace(Token $token, bool $isWhitespace, ?string $whitespaces = null): void
  226. {
  227. if (null !== $whitespaces) {
  228. self::assertSame($isWhitespace, $token->isWhitespace($whitespaces));
  229. } else {
  230. self::assertSame($isWhitespace, $token->isWhitespace(null));
  231. self::assertSame($isWhitespace, $token->isWhitespace());
  232. }
  233. }
  234. /**
  235. * @return iterable<array{0: Token, 1: bool, 2?: string}>
  236. */
  237. public static function provideIsWhitespaceCases(): iterable
  238. {
  239. yield [self::getBraceToken(), false];
  240. yield [self::getForeachToken(), false];
  241. yield [new Token(' '), true];
  242. yield [new Token("\t "), true];
  243. yield [new Token("\t "), false, ' '];
  244. yield [new Token([T_WHITESPACE, "\r", 1]), true];
  245. yield [new Token([T_WHITESPACE, "\0", 1]), true];
  246. yield [new Token([T_WHITESPACE, "\x0B", 1]), true];
  247. yield [new Token([T_WHITESPACE, "\n", 1]), true];
  248. yield [new Token([T_WHITESPACE, "\n", 1]), false, " \t"];
  249. }
  250. /**
  251. * @param mixed $prototype
  252. * @param ?class-string<\Throwable> $expectedExceptionClass
  253. *
  254. * @dataProvider provideCreatingTokenCases
  255. */
  256. public function testCreatingToken($prototype, ?int $expectedId, ?string $expectedContent, ?bool $expectedIsArray, ?string $expectedExceptionClass = null): void
  257. {
  258. if (null !== $expectedExceptionClass) {
  259. $this->expectException($expectedExceptionClass);
  260. }
  261. $token = new Token($prototype);
  262. self::assertSame($expectedId, $token->getId());
  263. self::assertSame($expectedContent, $token->getContent());
  264. self::assertSame($expectedIsArray, $token->isArray());
  265. }
  266. public static function provideCreatingTokenCases(): iterable
  267. {
  268. yield [[T_FOREACH, 'foreach'], T_FOREACH, 'foreach', true];
  269. yield ['(', null, '(', false];
  270. yield [123, null, null, null, \InvalidArgumentException::class];
  271. yield [false, null, null, null, \InvalidArgumentException::class];
  272. yield [null, null, null, null, \InvalidArgumentException::class];
  273. }
  274. public function testEqualsDefaultIsCaseSensitive(): void
  275. {
  276. $token = new Token([T_FUNCTION, 'function', 1]);
  277. self::assertTrue($token->equals([T_FUNCTION, 'function']));
  278. self::assertFalse($token->equals([T_FUNCTION, 'Function']));
  279. }
  280. /**
  281. * @param array{0: int, 1?: string}|string|Token $other
  282. *
  283. * @dataProvider provideEqualsCases
  284. */
  285. public function testEquals(Token $token, bool $equals, $other, bool $caseSensitive = true): void
  286. {
  287. self::assertSame($equals, $token->equals($other, $caseSensitive));
  288. }
  289. public static function provideEqualsCases(): iterable
  290. {
  291. $brace = self::getBraceToken();
  292. $function = new Token([T_FUNCTION, 'function', 1]);
  293. yield [$brace, false, '!'];
  294. yield [$brace, false, '!', false];
  295. yield [$brace, true, '('];
  296. yield [$brace, true, '(', false];
  297. yield [$function, false, '('];
  298. yield [$function, false, '(', false];
  299. yield [$function, false, [T_NAMESPACE]];
  300. yield [$function, false, [T_NAMESPACE], false];
  301. yield [$function, false, [T_VARIABLE, 'function']];
  302. yield [$function, false, [T_VARIABLE, 'function'], false];
  303. yield [$function, false, [T_VARIABLE, 'Function']];
  304. yield [$function, false, [T_VARIABLE, 'Function'], false];
  305. yield [$function, true, [T_FUNCTION]];
  306. yield [$function, true, [T_FUNCTION], false];
  307. yield [$function, true, [T_FUNCTION, 'function']];
  308. yield [$function, true, [T_FUNCTION, 'function'], false];
  309. yield [$function, false, [T_FUNCTION, 'Function']];
  310. yield [$function, true, [T_FUNCTION, 'Function'], false];
  311. yield [$function, false, [T_FUNCTION, 'junction'], false];
  312. yield [$function, true, new Token([T_FUNCTION, 'function'])];
  313. yield [$function, false, new Token([T_FUNCTION, 'Function'])];
  314. yield [$function, true, new Token([T_FUNCTION, 'Function']), false];
  315. // if it is an array any additional field is checked too
  316. yield [$function, false, [T_FUNCTION, 'function', 'unexpected']];
  317. yield [new Token('&'), true, '&'];
  318. if (\defined('T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG')) { // @TODO: drop condition when PHP 8.1+ is required
  319. yield [new Token('&'), true, new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  320. yield [new Token('&'), true, new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  321. yield [new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, '&'];
  322. yield [new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, new Token([T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  323. yield [new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, '&'];
  324. yield [new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&']), true, new Token([T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG, '&'])];
  325. }
  326. }
  327. public function testEqualsAnyDefaultIsCaseSensitive(): void
  328. {
  329. $token = new Token([T_FUNCTION, 'function', 1]);
  330. self::assertTrue($token->equalsAny([[T_FUNCTION, 'function']]));
  331. self::assertFalse($token->equalsAny([[T_FUNCTION, 'Function']]));
  332. }
  333. /**
  334. * @param list<array{0: int, 1?: string}|string|Token> $other
  335. *
  336. * @dataProvider provideEqualsAnyCases
  337. */
  338. public function testEqualsAny(bool $equalsAny, array $other, bool $caseSensitive = true): void
  339. {
  340. $token = new Token([T_FUNCTION, 'function', 1]);
  341. self::assertSame($equalsAny, $token->equalsAny($other, $caseSensitive));
  342. }
  343. public static function provideEqualsAnyCases(): iterable
  344. {
  345. $brace = self::getBraceToken();
  346. $foreach = self::getForeachToken();
  347. yield [false, []];
  348. yield [false, [$brace]];
  349. yield [false, [$brace, $foreach]];
  350. yield [true, [$brace, $foreach, [T_FUNCTION]]];
  351. yield [true, [$brace, $foreach, [T_FUNCTION, 'function']]];
  352. yield [false, [$brace, $foreach, [T_FUNCTION, 'Function']]];
  353. yield [true, [$brace, $foreach, [T_FUNCTION, 'Function']], false];
  354. yield [false, [[T_VARIABLE, 'junction'], [T_FUNCTION, 'junction']], false];
  355. }
  356. /**
  357. * @param bool|list<bool> $caseSensitive
  358. *
  359. * @dataProvider provideIsKeyCaseSensitiveCases
  360. *
  361. * @group legacy
  362. */
  363. public function testIsKeyCaseSensitive(bool $isKeyCaseSensitive, $caseSensitive, int $key): void
  364. {
  365. $this->expectDeprecation('Method "PhpCsFixer\Tokenizer\Token::isKeyCaseSensitive" is deprecated and will be removed in the next major version.');
  366. self::assertSame($isKeyCaseSensitive, Token::isKeyCaseSensitive($caseSensitive, $key));
  367. }
  368. public static function provideIsKeyCaseSensitiveCases(): iterable
  369. {
  370. yield [true, true, 0];
  371. yield [true, true, 1];
  372. yield [true, [], 0];
  373. yield [true, [true], 0];
  374. yield [true, [false, true], 1];
  375. yield [true, [false, true, false], 1];
  376. yield [true, [false], 10];
  377. yield [false, false, 10];
  378. yield [false, [false], 0];
  379. yield [false, [true, false], 1];
  380. yield [false, [true, false, true], 1];
  381. yield [false, [1 => false], 1];
  382. }
  383. /**
  384. * @dataProvider provideTokenGetNameForIdCases
  385. */
  386. public function testTokenGetNameForId(?string $expected, int $id): void
  387. {
  388. self::assertSame($expected, Token::getNameForId($id));
  389. }
  390. /**
  391. * @return iterable<array{null|string, int}>
  392. */
  393. public static function provideTokenGetNameForIdCases(): iterable
  394. {
  395. yield [
  396. null,
  397. -1,
  398. ];
  399. yield [
  400. 'T_CLASS',
  401. T_CLASS,
  402. ];
  403. yield [
  404. 'CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE',
  405. CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
  406. ];
  407. }
  408. /**
  409. * @dataProvider provideGetNameCases
  410. */
  411. public function testGetName(Token $token, ?string $expected = null): void
  412. {
  413. self::assertSame($expected, $token->getName());
  414. }
  415. /**
  416. * @return iterable<array{Token, null|string}>
  417. */
  418. public static function provideGetNameCases(): iterable
  419. {
  420. yield [
  421. new Token([T_FUNCTION, 'function', 1]),
  422. 'T_FUNCTION',
  423. ];
  424. yield [
  425. new Token(')'),
  426. null,
  427. ];
  428. yield [
  429. new Token(''),
  430. null,
  431. ];
  432. }
  433. /**
  434. * @param array<string, mixed> $expected
  435. *
  436. * @dataProvider provideToArrayCases
  437. */
  438. public function testToArray(Token $token, array $expected): void
  439. {
  440. self::assertSame($expected, $token->toArray());
  441. }
  442. public static function provideToArrayCases(): iterable
  443. {
  444. yield [
  445. new Token([T_FUNCTION, 'function', 1]),
  446. [
  447. 'id' => T_FUNCTION,
  448. 'name' => 'T_FUNCTION',
  449. 'content' => 'function',
  450. 'isArray' => true,
  451. 'changed' => false,
  452. ],
  453. ];
  454. yield [
  455. new Token(')'),
  456. [
  457. 'id' => null,
  458. 'name' => null,
  459. 'content' => ')',
  460. 'isArray' => false,
  461. 'changed' => false,
  462. ],
  463. ];
  464. yield [
  465. new Token(''),
  466. [
  467. 'id' => null,
  468. 'name' => null,
  469. 'content' => '',
  470. 'isArray' => false,
  471. 'changed' => false,
  472. ],
  473. ];
  474. }
  475. public function testGetClassyTokenKinds(): void
  476. {
  477. if (\defined('T_ENUM')) {
  478. self::assertSame([T_CLASS, T_TRAIT, T_INTERFACE, T_ENUM], Token::getClassyTokenKinds());
  479. } else {
  480. self::assertSame([T_CLASS, T_TRAIT, T_INTERFACE], Token::getClassyTokenKinds());
  481. }
  482. }
  483. private static function getBraceToken(): Token
  484. {
  485. return new Token(self::getBraceTokenPrototype());
  486. }
  487. private static function getBraceTokenPrototype(): string
  488. {
  489. return '(';
  490. }
  491. private static function getForeachToken(): Token
  492. {
  493. return new Token(self::getForeachTokenPrototype());
  494. }
  495. /**
  496. * @return array{int, string}
  497. */
  498. private static function getForeachTokenPrototype(): array
  499. {
  500. static $prototype = [T_FOREACH, 'foreach'];
  501. return $prototype;
  502. }
  503. }