TokensAnalyzerTest.php 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143
  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\Tokens;
  15. use PhpCsFixer\Tokenizer\TokensAnalyzer;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. * @author Max Voloshin <voloshin.dp@gmail.com>
  19. * @author Gregor Harlan <gharlan@web.de>
  20. *
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Tokenizer\TokensAnalyzer
  24. */
  25. final class TokensAnalyzerTest extends TestCase
  26. {
  27. /**
  28. * @param array<int, array{classIndex: int, type: string}> $expectedElements
  29. *
  30. * @dataProvider provideGetClassyElementsCases
  31. */
  32. public function testGetClassyElements(array $expectedElements, string $source): void
  33. {
  34. $tokens = Tokens::fromCode($source);
  35. array_walk(
  36. $expectedElements,
  37. static function (array &$element, int $index) use ($tokens): void {
  38. $element['token'] = $tokens[$index];
  39. ksort($element);
  40. }
  41. );
  42. $tokensAnalyzer = new TokensAnalyzer($tokens);
  43. self::assertSame(
  44. $expectedElements,
  45. $tokensAnalyzer->getClassyElements()
  46. );
  47. }
  48. public static function provideGetClassyElementsCases(): iterable
  49. {
  50. yield 'trait import' => [
  51. [
  52. 10 => [
  53. 'classIndex' => 4,
  54. 'type' => 'trait_import',
  55. ],
  56. 19 => [
  57. 'classIndex' => 4,
  58. 'type' => 'trait_import',
  59. ],
  60. 24 => [
  61. 'classIndex' => 4,
  62. 'type' => 'const',
  63. ],
  64. 35 => [
  65. 'classIndex' => 4,
  66. 'type' => 'method',
  67. ],
  68. 55 => [
  69. 'classIndex' => 49,
  70. 'type' => 'trait_import',
  71. ],
  72. 64 => [
  73. 'classIndex' => 49,
  74. 'type' => 'method',
  75. ],
  76. ],
  77. '<?php
  78. /** */
  79. class Foo
  80. {
  81. use A\B;
  82. //
  83. use Foo;
  84. const A = 1;
  85. public function foo()
  86. {
  87. $a = new class()
  88. {
  89. use Z; // nested trait import
  90. public function bar()
  91. {
  92. echo 123;
  93. }
  94. };
  95. $a->bar();
  96. }
  97. }',
  98. ];
  99. yield [
  100. [
  101. 9 => [
  102. 'classIndex' => 1,
  103. 'type' => 'property',
  104. ],
  105. 14 => [
  106. 'classIndex' => 1,
  107. 'type' => 'property',
  108. ],
  109. 19 => [
  110. 'classIndex' => 1,
  111. 'type' => 'property',
  112. ],
  113. 28 => [
  114. 'classIndex' => 1,
  115. 'type' => 'property',
  116. ],
  117. 42 => [
  118. 'classIndex' => 1,
  119. 'type' => 'const',
  120. ],
  121. 53 => [
  122. 'classIndex' => 1,
  123. 'type' => 'method',
  124. ],
  125. 83 => [
  126. 'classIndex' => 1,
  127. 'type' => 'method',
  128. ],
  129. 140 => [
  130. 'classIndex' => 1,
  131. 'type' => 'method',
  132. ],
  133. 164 => [
  134. 'classIndex' => 158,
  135. 'type' => 'const',
  136. ],
  137. 173 => [
  138. 'classIndex' => 158,
  139. 'type' => 'trait_import',
  140. ],
  141. ],
  142. <<<'PHP'
  143. <?php
  144. class Foo
  145. {
  146. public $prop0;
  147. protected $prop1;
  148. private $prop2 = 1;
  149. var $prop3 = array(1,2,3);
  150. const CONSTANT = 'constant value';
  151. public function bar4()
  152. {
  153. $a = 5;
  154. return " ({$a})";
  155. }
  156. public function bar5($data)
  157. {
  158. $message = $data;
  159. $example = function ($arg) use ($message) {
  160. echo $arg . ' ' . $message;
  161. };
  162. $example('hello');
  163. }function A(){}
  164. }
  165. function test(){}
  166. class Foo2
  167. {
  168. const CONSTANT = 'constant value';
  169. use Foo\Bar; // expected in the return value
  170. }
  171. PHP,
  172. ];
  173. }
  174. public function testGetClassyElementsWithNullableProperties(): void
  175. {
  176. $source = <<<'PHP'
  177. <?php
  178. class Foo
  179. {
  180. public int $prop0;
  181. protected ?array $prop1;
  182. private string $prop2 = 1;
  183. var ? Foo\Bar $prop3 = array(1,2,3);
  184. }
  185. PHP;
  186. $tokens = Tokens::fromCode($source);
  187. $tokensAnalyzer = new TokensAnalyzer($tokens);
  188. $elements = $tokensAnalyzer->getClassyElements();
  189. self::assertSame(
  190. [
  191. 11 => [
  192. 'classIndex' => 1,
  193. 'token' => $tokens[11],
  194. 'type' => 'property',
  195. ],
  196. 19 => [
  197. 'classIndex' => 1,
  198. 'token' => $tokens[19],
  199. 'type' => 'property',
  200. ],
  201. 26 => [
  202. 'classIndex' => 1,
  203. 'token' => $tokens[26],
  204. 'type' => 'property',
  205. ],
  206. 41 => [
  207. 'classIndex' => 1,
  208. 'token' => $tokens[41],
  209. 'type' => 'property',
  210. ],
  211. ],
  212. $elements
  213. );
  214. }
  215. public function testGetClassyElementsWithAnonymousClass(): void
  216. {
  217. $source = <<<'PHP'
  218. <?php
  219. class A {
  220. public $A;
  221. private function B()
  222. {
  223. return new class(){
  224. protected $level1;
  225. private function XYZ() {
  226. return new class(){private $level2 = 1;};
  227. }
  228. };
  229. }
  230. private function C() {
  231. }
  232. }
  233. function B() {} // do not count this
  234. PHP;
  235. $tokens = Tokens::fromCode($source);
  236. $tokensAnalyzer = new TokensAnalyzer($tokens);
  237. $elements = $tokensAnalyzer->getClassyElements();
  238. self::assertSame(
  239. [
  240. 9 => [
  241. 'classIndex' => 1,
  242. 'token' => $tokens[9],
  243. 'type' => 'property', // $A
  244. ],
  245. 14 => [
  246. 'classIndex' => 1,
  247. 'token' => $tokens[14],
  248. 'type' => 'method', // B
  249. ],
  250. 33 => [
  251. 'classIndex' => 26,
  252. 'token' => $tokens[33],
  253. 'type' => 'property', // $level1
  254. ],
  255. 38 => [
  256. 'classIndex' => 26,
  257. 'token' => $tokens[38],
  258. 'type' => 'method', // XYZ
  259. ],
  260. 56 => [
  261. 'classIndex' => 50,
  262. 'token' => $tokens[56],
  263. 'type' => 'property', // $level2
  264. ],
  265. 74 => [
  266. 'classIndex' => 1,
  267. 'token' => $tokens[74],
  268. 'type' => 'method', // C
  269. ],
  270. ],
  271. $elements
  272. );
  273. }
  274. public function testGetClassyElementsWithMultipleAnonymousClass(): void
  275. {
  276. $source = <<<'PHP'
  277. <?php class A0
  278. {
  279. public function AA0()
  280. {
  281. return new class
  282. {
  283. public function BB0()
  284. {
  285. }
  286. };
  287. }
  288. public function otherFunction0()
  289. {
  290. }
  291. }
  292. class A1
  293. {
  294. public function AA1()
  295. {
  296. return new class
  297. {
  298. public function BB1()
  299. {
  300. return new class
  301. {
  302. public function CC1()
  303. {
  304. return new class
  305. {
  306. public function DD1()
  307. {
  308. return new class{};
  309. }
  310. public function DD2()
  311. {
  312. return new class{};
  313. }
  314. };
  315. }
  316. };
  317. }
  318. public function BB2()
  319. {
  320. return new class
  321. {
  322. public function CC2()
  323. {
  324. return new class
  325. {
  326. public function DD2()
  327. {
  328. return new class{};
  329. }
  330. };
  331. }
  332. };
  333. }
  334. };
  335. }
  336. public function otherFunction1()
  337. {
  338. }
  339. }
  340. PHP;
  341. $tokens = Tokens::fromCode($source);
  342. $tokensAnalyzer = new TokensAnalyzer($tokens);
  343. $elements = $tokensAnalyzer->getClassyElements();
  344. self::assertSame(
  345. [
  346. 9 => [
  347. 'classIndex' => 1,
  348. 'token' => $tokens[9],
  349. 'type' => 'method',
  350. ],
  351. 27 => [
  352. 'classIndex' => 21,
  353. 'token' => $tokens[27],
  354. 'type' => 'method',
  355. ],
  356. 44 => [
  357. 'classIndex' => 1,
  358. 'token' => $tokens[44],
  359. 'type' => 'method',
  360. ],
  361. 64 => [
  362. 'classIndex' => 56,
  363. 'token' => $tokens[64],
  364. 'type' => 'method',
  365. ],
  366. 82 => [
  367. 'classIndex' => 76,
  368. 'token' => $tokens[82],
  369. 'type' => 'method',
  370. ],
  371. 100 => [
  372. 'classIndex' => 94,
  373. 'token' => $tokens[100],
  374. 'type' => 'method',
  375. ],
  376. 118 => [
  377. 'classIndex' => 112,
  378. 'token' => $tokens[118],
  379. 'type' => 'method',
  380. ],
  381. 139 => [
  382. 'classIndex' => 112,
  383. 'token' => $tokens[139],
  384. 'type' => 'method',
  385. ],
  386. 170 => [
  387. 'classIndex' => 76,
  388. 'token' => $tokens[170],
  389. 'type' => 'method',
  390. ],
  391. 188 => [
  392. 'classIndex' => 182,
  393. 'token' => $tokens[188],
  394. 'type' => 'method',
  395. ],
  396. 206 => [
  397. 'classIndex' => 200,
  398. 'token' => $tokens[206],
  399. 'type' => 'method',
  400. ],
  401. 242 => [
  402. 'classIndex' => 56,
  403. 'token' => $tokens[242],
  404. 'type' => 'method',
  405. ],
  406. ],
  407. $elements
  408. );
  409. }
  410. public function testGetClassyElements74(): void
  411. {
  412. $source = <<<'PHP'
  413. <?php
  414. class Foo
  415. {
  416. public int $bar = 3;
  417. protected ?string $baz;
  418. private ?string $bazNull = null;
  419. public static iterable $staticProp;
  420. public float $x, $y;
  421. var bool $flag1;
  422. var ?bool $flag2;
  423. }
  424. PHP;
  425. $tokens = Tokens::fromCode($source);
  426. $tokensAnalyzer = new TokensAnalyzer($tokens);
  427. $elements = $tokensAnalyzer->getClassyElements();
  428. $expected = [];
  429. foreach ([11, 23, 31, 44, 51, 54, 61, 69] as $index) {
  430. $expected[$index] = [
  431. 'classIndex' => 1,
  432. 'token' => $tokens[$index],
  433. 'type' => 'property',
  434. ];
  435. }
  436. self::assertSame($expected, $elements);
  437. }
  438. /**
  439. * @param array<int, array{classIndex: int, type: string}> $expected
  440. *
  441. * @dataProvider provideGetClassyElements81Cases
  442. *
  443. * @requires PHP 8.1
  444. */
  445. public function testGetClassyElements81(array $expected, string $source): void
  446. {
  447. $tokens = Tokens::fromCode($source);
  448. $tokensAnalyzer = new TokensAnalyzer($tokens);
  449. $elements = $tokensAnalyzer->getClassyElements();
  450. array_walk(
  451. $expected,
  452. static function (array &$element, int $index) use ($tokens): void {
  453. $element['token'] = $tokens[$index];
  454. ksort($element);
  455. }
  456. );
  457. self::assertSame($expected, $elements);
  458. }
  459. public static function provideGetClassyElements81Cases(): iterable
  460. {
  461. yield [
  462. [
  463. 11 => [
  464. 'classIndex' => 1,
  465. 'type' => 'property', // $prop1
  466. ],
  467. 20 => [
  468. 'classIndex' => 1,
  469. 'type' => 'property', // $prop2
  470. ],
  471. 29 => [
  472. 'classIndex' => 1,
  473. 'type' => 'property', // $prop13
  474. ],
  475. ],
  476. '<?php
  477. class Foo
  478. {
  479. readonly string $prop1;
  480. readonly public string $prop2;
  481. public readonly string $prop3;
  482. }
  483. ',
  484. ];
  485. yield 'final const' => [
  486. [
  487. 11 => [
  488. 'classIndex' => 1,
  489. 'type' => 'const', // A
  490. ],
  491. 24 => [
  492. 'classIndex' => 1,
  493. 'type' => 'const', // B
  494. ],
  495. ],
  496. '<?php
  497. class Foo
  498. {
  499. final public const A = "1";
  500. public final const B = "2";
  501. }
  502. ',
  503. ];
  504. yield 'enum final const' => [
  505. [
  506. 11 => [
  507. 'classIndex' => 1,
  508. 'type' => 'const', // A
  509. ],
  510. 24 => [
  511. 'classIndex' => 1,
  512. 'type' => 'const', // B
  513. ],
  514. ],
  515. '<?php
  516. enum Foo
  517. {
  518. final public const A = "1";
  519. public final const B = "2";
  520. }
  521. ',
  522. ];
  523. yield 'enum case' => [
  524. [
  525. 12 => [
  526. 'classIndex' => 1,
  527. 'type' => 'const', // Spades
  528. ],
  529. 21 => [
  530. 'classIndex' => 1,
  531. 'type' => 'case', // Hearts
  532. ],
  533. 32 => [
  534. 'classIndex' => 1,
  535. 'type' => 'method', // function tests
  536. ],
  537. 81 => [
  538. 'classIndex' => 75,
  539. 'type' => 'method', // function bar123
  540. ],
  541. 135 => [
  542. 'classIndex' => 127,
  543. 'type' => 'method', // function bar7
  544. ],
  545. ],
  546. '<?php
  547. enum Foo: string
  548. {
  549. private const Spades = 123;
  550. case Hearts = "H";
  551. private function test($foo) {
  552. switch ($foo) {
  553. case 1:
  554. // case 2
  555. case 3:
  556. echo 123;
  557. break;
  558. }
  559. return new class {
  560. public function bar123($foo) {
  561. switch ($foo) {
  562. case 1:
  563. // case 2
  564. case 3:
  565. echo 123;
  566. };
  567. }
  568. };
  569. }
  570. }
  571. class Bar {
  572. public function bar7($foo) {
  573. switch ($foo) {
  574. case 1:
  575. // case 2
  576. case 3:
  577. echo 123;
  578. };
  579. }
  580. }
  581. ',
  582. ];
  583. yield 'enum' => [
  584. [
  585. 10 => [
  586. 'classIndex' => 1,
  587. 'type' => 'case',
  588. ],
  589. 19 => [
  590. 'classIndex' => 1,
  591. 'type' => 'case',
  592. ],
  593. 28 => [
  594. 'classIndex' => 1,
  595. 'type' => 'method',
  596. ],
  597. ],
  598. '<?php
  599. enum Foo: string
  600. {
  601. case Bar = "bar";
  602. case Baz = "baz";
  603. function qux() {
  604. switch (true) {
  605. case "x": break;
  606. }
  607. }
  608. }
  609. ',
  610. ];
  611. }
  612. /**
  613. * @param array<int, array{classIndex: int, type: string}> $expected
  614. *
  615. * @dataProvider provideGetClassyElements82Cases
  616. *
  617. * @requires PHP 8.2
  618. */
  619. public function testGetClassyElements82(array $expected, string $source): void
  620. {
  621. $tokens = Tokens::fromCode($source);
  622. $tokensAnalyzer = new TokensAnalyzer($tokens);
  623. $elements = $tokensAnalyzer->getClassyElements();
  624. array_walk(
  625. $expected,
  626. static function (array &$element, int $index) use ($tokens): void {
  627. $element['token'] = $tokens[$index];
  628. ksort($element);
  629. },
  630. );
  631. self::assertSame($expected, $elements);
  632. }
  633. public static function provideGetClassyElements82Cases(): iterable
  634. {
  635. yield 'constant in trait' => [
  636. [
  637. 7 => [
  638. 'classIndex' => 1,
  639. 'type' => 'const',
  640. ],
  641. 18 => [
  642. 'classIndex' => 1,
  643. 'type' => 'const',
  644. ],
  645. ],
  646. <<<'PHP'
  647. <?php
  648. trait Foo
  649. {
  650. const BAR = 0;
  651. final const BAZ = 1;
  652. }
  653. PHP,
  654. ];
  655. }
  656. /**
  657. * @param array<int, bool> $expected
  658. *
  659. * @dataProvider provideIsAnonymousClassCases
  660. */
  661. public function testIsAnonymousClass(array $expected, string $source): void
  662. {
  663. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  664. foreach ($expected as $index => $expectedValue) {
  665. self::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  666. }
  667. }
  668. /**
  669. * @return iterable<array{array<int, bool>, string}>
  670. */
  671. public static function provideIsAnonymousClassCases(): iterable
  672. {
  673. yield [
  674. [1 => false],
  675. '<?php class foo {}',
  676. ];
  677. yield [
  678. [7 => true],
  679. '<?php $foo = new class() {};',
  680. ];
  681. yield [
  682. [7 => true],
  683. '<?php $foo = new class() extends Foo implements Bar, Baz {};',
  684. ];
  685. yield [
  686. [1 => false, 19 => true],
  687. '<?php class Foo { function bar() { return new class() {}; } }',
  688. ];
  689. yield [
  690. [7 => true, 11 => true],
  691. '<?php $a = new class(new class($d->a) implements B{}) extends C{};',
  692. ];
  693. yield [
  694. [1 => false],
  695. '<?php interface foo {}',
  696. ];
  697. }
  698. /**
  699. * @param array<int, bool> $expected
  700. *
  701. * @dataProvider provideIsAnonymousClass80Cases
  702. *
  703. * @requires PHP 8.0
  704. */
  705. public function testIsAnonymousClass80(array $expected, string $source): void
  706. {
  707. $this->testIsAnonymousClass($expected, $source);
  708. }
  709. /**
  710. * @return iterable<array{array<int, bool>, string}>
  711. */
  712. public static function provideIsAnonymousClass80Cases(): iterable
  713. {
  714. yield [
  715. [11 => true],
  716. '<?php $object = new #[ExampleAttribute] class(){};',
  717. ];
  718. yield [
  719. [27 => true],
  720. '<?php $object = new #[A] #[B] #[C]#[D]/* */ /** */#[E]class(){};',
  721. ];
  722. }
  723. /**
  724. * @param array<int, bool> $expected
  725. *
  726. * @dataProvider provideIsAnonymousClass81Cases
  727. *
  728. * @requires PHP 8.1
  729. */
  730. public function testIsAnonymousClass81(array $expected, string $source): void
  731. {
  732. $this->testIsAnonymousClass($expected, $source);
  733. }
  734. /**
  735. * @return iterable<array{array<int, bool>, string}>
  736. */
  737. public static function provideIsAnonymousClass81Cases(): iterable
  738. {
  739. yield [
  740. [1 => false],
  741. '<?php enum foo {}',
  742. ];
  743. }
  744. /**
  745. * @param array<int, bool> $expected
  746. *
  747. * @dataProvider provideIsAnonymousClass83Cases
  748. *
  749. * @requires PHP 8.3
  750. */
  751. public function testIsAnonymousClass83(array $expected, string $source): void
  752. {
  753. $this->testIsAnonymousClass($expected, $source);
  754. }
  755. /**
  756. * @return iterable<array{array<int, bool>, string}>
  757. */
  758. public static function provideIsAnonymousClass83Cases(): iterable
  759. {
  760. yield 'simple readonly anonymous class' => [
  761. [9 => true],
  762. '<?php $instance = new readonly class {};',
  763. ];
  764. yield 'readonly anonymous class with attribute' => [
  765. [13 => true],
  766. '<?php $instance = new #[Foo] readonly class {};',
  767. ];
  768. yield 'readonly anonymous class with multiple attributes' => [
  769. [17 => true],
  770. '<?php $instance = new #[Foo] #[BAR] readonly class {};',
  771. ];
  772. }
  773. /**
  774. * @param array<int, bool> $expected
  775. *
  776. * @dataProvider provideIsLambdaCases
  777. */
  778. public function testIsLambda(array $expected, string $source): void
  779. {
  780. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  781. foreach ($expected as $index => $isLambda) {
  782. self::assertSame($isLambda, $tokensAnalyzer->isLambda($index));
  783. }
  784. }
  785. public static function provideIsLambdaCases(): iterable
  786. {
  787. yield [
  788. [1 => false],
  789. '<?php function foo () {};',
  790. ];
  791. yield [
  792. [1 => false],
  793. '<?php function /** foo */ foo () {};',
  794. ];
  795. yield [
  796. [5 => true],
  797. '<?php $foo = function () {};',
  798. ];
  799. yield [
  800. [5 => true],
  801. '<?php $foo = function /** foo */ () {};',
  802. ];
  803. yield [
  804. [7 => true],
  805. '<?php
  806. preg_replace_callback(
  807. "/(^|[a-z])/",
  808. function (array $matches) {
  809. return "a";
  810. },
  811. $string
  812. );',
  813. ];
  814. yield [
  815. [5 => true],
  816. '<?php $foo = function &() {};',
  817. ];
  818. yield [
  819. [6 => true],
  820. '<?php
  821. $a = function (): array {
  822. return [];
  823. };',
  824. ];
  825. yield [
  826. [2 => false],
  827. '<?php
  828. function foo (): array {
  829. return [];
  830. };',
  831. ];
  832. yield [
  833. [6 => true],
  834. '<?php
  835. $a = function (): void {
  836. return [];
  837. };',
  838. ];
  839. yield [
  840. [2 => false],
  841. '<?php
  842. function foo (): void {
  843. return [];
  844. };',
  845. ];
  846. yield [
  847. [6 => true],
  848. '<?php
  849. $a = function (): ?int {
  850. return [];
  851. };',
  852. ];
  853. yield [
  854. [6 => true],
  855. '<?php
  856. $a = function (): int {
  857. return [];
  858. };',
  859. ];
  860. yield [
  861. [2 => false],
  862. '<?php
  863. function foo (): ?int {
  864. return [];
  865. };',
  866. ];
  867. yield [
  868. [5 => true],
  869. '<?php $fn = fn() => [];',
  870. ];
  871. yield [
  872. [5 => true],
  873. '<?php $fn = fn () => [];',
  874. ];
  875. }
  876. /**
  877. * @param array<int, bool> $expected
  878. *
  879. * @dataProvider provideIsLambda80Cases
  880. *
  881. * @requires PHP 8.0
  882. */
  883. public function testIsLambda80(array $expected, string $source): void
  884. {
  885. $this->testIsLambda($expected, $source);
  886. }
  887. public static function provideIsLambda80Cases(): iterable
  888. {
  889. yield [
  890. [6 => true],
  891. '<?php
  892. $a = function (): ?static {
  893. return [];
  894. };',
  895. ];
  896. yield [
  897. [6 => true],
  898. '<?php
  899. $a = function (): static {
  900. return [];
  901. };',
  902. ];
  903. yield [
  904. [14 => true],
  905. '<?php
  906. $c = 4; //
  907. $a = function(
  908. $a,
  909. $b,
  910. ) use (
  911. $c,
  912. ) {
  913. echo $a + $b + $c;
  914. };
  915. $a(1,2);',
  916. ];
  917. }
  918. public function testIsLambdaInvalid(): void
  919. {
  920. $this->expectException(\LogicException::class);
  921. $this->expectExceptionMessage('No T_FUNCTION or T_FN at given index 0, got "T_OPEN_TAG".');
  922. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  923. $tokensAnalyzer->isLambda(0);
  924. }
  925. /**
  926. * @param array<int, bool> $expected
  927. *
  928. * @dataProvider provideIsConstantInvocationCases
  929. */
  930. public function testIsConstantInvocation(array $expected, string $source): void
  931. {
  932. $this->doIsConstantInvocationTest($expected, $source);
  933. }
  934. public static function provideIsConstantInvocationCases(): iterable
  935. {
  936. yield [
  937. [3 => true],
  938. '<?php echo FOO;',
  939. ];
  940. yield [
  941. [4 => true],
  942. '<?php echo \FOO;',
  943. ];
  944. yield [
  945. [3 => false, 5 => false, 7 => true],
  946. '<?php echo Foo\Bar\BAR;',
  947. ];
  948. yield [
  949. [3 => true, 7 => true, 11 => true],
  950. '<?php echo FOO ? BAR : BAZ;',
  951. ];
  952. yield 'Bitwise & and bitwise |' => [
  953. [3 => true, 7 => true, 11 => true],
  954. '<?php echo FOO & BAR | BAZ;',
  955. ];
  956. yield 'Bitwise &' => [
  957. [3 => true],
  958. '<?php echo FOO & $bar;',
  959. ];
  960. yield [
  961. [5 => true],
  962. '<?php echo $foo[BAR];',
  963. ];
  964. yield [
  965. [3 => true, 5 => true],
  966. '<?php echo FOO[BAR];',
  967. ];
  968. yield [
  969. [1 => false, 3 => true, 6 => false, 8 => true],
  970. '<?php func(FOO, Bar\BAZ);',
  971. ];
  972. yield [
  973. [4 => true, 8 => true],
  974. '<?php if (FOO && BAR) {}',
  975. ];
  976. yield [
  977. [3 => true, 7 => false, 9 => false, 11 => true],
  978. '<?php return FOO * X\Y\BAR;',
  979. ];
  980. yield [
  981. [3 => false, 11 => true, 16 => true, 20 => true],
  982. '<?php function x() { yield FOO; yield FOO => BAR; }',
  983. ];
  984. yield [
  985. [11 => true],
  986. '<?php switch ($a) { case FOO: break; }',
  987. ];
  988. yield [
  989. [3 => false],
  990. '<?php namespace FOO;',
  991. ];
  992. yield [
  993. [3 => false],
  994. '<?php use FOO;',
  995. ];
  996. yield [
  997. [5 => false, 7 => false, 9 => false],
  998. '<?php use function FOO\BAR\BAZ;',
  999. ];
  1000. yield [
  1001. [3 => false, 8 => false],
  1002. '<?php namespace X; const FOO = 1;',
  1003. ];
  1004. yield [
  1005. [3 => false],
  1006. '<?php class FOO {}',
  1007. ];
  1008. yield [
  1009. [3 => false],
  1010. '<?php interface FOO {}',
  1011. ];
  1012. yield [
  1013. [3 => false],
  1014. '<?php trait FOO {}',
  1015. ];
  1016. yield [
  1017. [3 => false, 7 => false],
  1018. '<?php class x extends FOO {}',
  1019. ];
  1020. yield [
  1021. [3 => false, 7 => false],
  1022. '<?php class x implements FOO {}',
  1023. ];
  1024. yield [
  1025. [3 => false, 7 => false, 10 => false, 13 => false],
  1026. '<?php class x implements FOO, BAR, BAZ {}',
  1027. ];
  1028. yield [
  1029. [3 => false, 9 => false],
  1030. '<?php class x { const FOO = 1; }',
  1031. ];
  1032. yield [
  1033. [3 => false, 9 => false],
  1034. '<?php class x { use FOO; }',
  1035. ];
  1036. yield [
  1037. [3 => false, 9 => false, 12 => false, 16 => false, 18 => false, 22 => false],
  1038. '<?php class x { use FOO, BAR { FOO::BAZ insteadof BAR; } }',
  1039. ];
  1040. yield [
  1041. [3 => false, 6 => false, 11 => false, 17 => false],
  1042. '<?php function x (FOO $foo, BAR &$bar, BAZ ...$baz) {}',
  1043. ];
  1044. yield [
  1045. [1 => false],
  1046. '<?php FOO();',
  1047. ];
  1048. yield [
  1049. [1 => false, 3 => false],
  1050. '<?php FOO::x();',
  1051. ];
  1052. yield [
  1053. [1 => false, 3 => false],
  1054. '<?php x::FOO();',
  1055. ];
  1056. yield [
  1057. [5 => false],
  1058. '<?php $foo instanceof FOO;',
  1059. ];
  1060. yield [
  1061. [9 => false],
  1062. '<?php try {} catch (FOO $e) {}',
  1063. ];
  1064. yield [
  1065. [4 => false],
  1066. '<?php "$foo[BAR]";',
  1067. ];
  1068. yield [
  1069. [5 => true],
  1070. '<?php "{$foo[BAR]}";',
  1071. ];
  1072. yield [
  1073. [1 => false, 6 => false],
  1074. '<?php FOO: goto FOO;',
  1075. ];
  1076. yield [
  1077. [1 => false, 3 => true, 7 => true],
  1078. '<?php foo(E_USER_DEPRECATED | E_DEPRECATED);',
  1079. ];
  1080. yield [
  1081. [3 => false, 7 => false, 10 => false, 13 => false],
  1082. '<?php interface Foo extends Bar, Baz, Qux {}',
  1083. ];
  1084. yield [
  1085. [3 => false, 5 => false, 8 => false, 10 => false, 13 => false, 15 => false],
  1086. '<?php use Foo\Bar, Foo\Baz, Foo\Qux;',
  1087. ];
  1088. yield [
  1089. [3 => false, 8 => false],
  1090. '<?php function x(): FOO {}',
  1091. ];
  1092. yield [
  1093. [3 => false, 5 => false, 8 => false, 11 => false, 15 => false, 18 => false],
  1094. '<?php use X\Y\{FOO, BAR as BAR2, BAZ};',
  1095. ];
  1096. yield [
  1097. [6 => false, 16 => false, 21 => false],
  1098. '<?php
  1099. abstract class Baz
  1100. {
  1101. abstract public function test(): Foo;
  1102. }
  1103. ',
  1104. ];
  1105. yield [
  1106. [3 => false, 6 => false],
  1107. '<?php function x(?FOO $foo) {}',
  1108. ];
  1109. yield [
  1110. [3 => false, 9 => false],
  1111. '<?php function x(): ?FOO {}',
  1112. ];
  1113. yield [
  1114. [9 => false, 11 => false, 13 => false],
  1115. '<?php try {} catch (FOO|BAR|BAZ $e) {}',
  1116. ];
  1117. yield [
  1118. [3 => false, 11 => false, 16 => false],
  1119. '<?php interface Foo { public function bar(): Baz; }',
  1120. ];
  1121. yield [
  1122. [3 => false, 11 => false, 17 => false],
  1123. '<?php interface Foo { public function bar(): \Baz; }',
  1124. ];
  1125. yield [
  1126. [3 => false, 11 => false, 17 => false],
  1127. '<?php interface Foo { public function bar(): ?Baz; }',
  1128. ];
  1129. yield [
  1130. [3 => false, 11 => false, 18 => false],
  1131. '<?php interface Foo { public function bar(): ?\Baz; }',
  1132. ];
  1133. yield [
  1134. [3 => true],
  1135. '<?php foreach(FOO as $foo) {}',
  1136. ];
  1137. yield [
  1138. [
  1139. 3 => false,
  1140. 5 => false,
  1141. 9 => false,
  1142. ],
  1143. '<?php use Foo\Bir as FBB;',
  1144. ];
  1145. }
  1146. /**
  1147. * @param array<int, bool> $expected
  1148. *
  1149. * @dataProvider provideIsConstantInvocationPhp80Cases
  1150. *
  1151. * @requires PHP 8.0
  1152. */
  1153. public function testIsConstantInvocationPhp80(array $expected, string $source): void
  1154. {
  1155. $this->doIsConstantInvocationTest($expected, $source);
  1156. }
  1157. public static function provideIsConstantInvocationPhp80Cases(): iterable
  1158. {
  1159. yield 'abstract method return alternation' => [
  1160. [6 => false, 16 => false, 21 => false, 23 => false],
  1161. '<?php
  1162. abstract class Baz
  1163. {
  1164. abstract public function test(): Foo|Bar;
  1165. }
  1166. ',
  1167. ];
  1168. yield 'function return alternation' => [
  1169. [3 => false, 8 => false, 10 => false],
  1170. '<?php function test(): Foo|Bar {}',
  1171. ];
  1172. yield 'nullsafe operator' => [
  1173. [3 => false, 5 => false],
  1174. '<?php $a?->b?->c;',
  1175. ];
  1176. yield 'non-capturing catch' => [
  1177. [9 => false],
  1178. '<?php try {} catch (Exception) {}',
  1179. ];
  1180. yield 'non-capturing catch 2' => [
  1181. [10 => false],
  1182. '<?php try {} catch (\Exception) {}',
  1183. ];
  1184. yield 'non-capturing multiple catch' => [
  1185. [9 => false, 13 => false],
  1186. '<?php try {} catch (Foo | Bar) {}',
  1187. ];
  1188. yield 'attribute 1' => [
  1189. [2 => false, 5 => false, 10 => false],
  1190. '<?php #[Foo, Bar] function foo() {}',
  1191. ];
  1192. yield 'attribute 2' => [
  1193. [2 => false, 7 => false, 14 => false],
  1194. '<?php #[Foo(), Bar()] function foo() {}',
  1195. ];
  1196. yield [
  1197. [2 => false, 9 => false],
  1198. '<?php #[Foo()] function foo() {}',
  1199. ];
  1200. yield [
  1201. [3 => false, 10 => false],
  1202. '<?php #[\Foo()] function foo() {}',
  1203. ];
  1204. yield [
  1205. [2 => false, 4 => false, 11 => false],
  1206. '<?php #[A\Foo()] function foo() {}',
  1207. ];
  1208. yield [
  1209. [3 => false, 5 => false, 12 => false],
  1210. '<?php #[\A\Foo()] function foo() {}',
  1211. ];
  1212. yield 'multiple type catch with variable' => [
  1213. [5 => false, 15 => false, 18 => false],
  1214. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
  1215. ];
  1216. yield 'multiple type catch without variable 1' => [
  1217. [5 => false, 15 => false, 18 => false],
  1218. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
  1219. ];
  1220. yield 'multiple type catch without variable 2' => [
  1221. [5 => false, 15 => false, 17 => false, 19 => false, 21 => false, 24 => false, 27 => false],
  1222. '<?php try { foo(); } catch(\D|Z|A\B|\InvalidArgumentException|\LogicException) {}',
  1223. ];
  1224. yield 'multiple type catch without variable 3' => [
  1225. [5 => false, 14 => false, 16 => false, 19 => false, 22 => false],
  1226. '<?php try { foo(); } catch(A\B|\InvalidArgumentException|\LogicException) {}',
  1227. ];
  1228. yield 'attribute before' => [
  1229. [4 => false, 6 => false, 8 => false, 13 => false, 17 => false, 23 => false, 25 => false],
  1230. '<?php
  1231. use Psr\Log\LoggerInterface;
  1232. function f( #[Target(\'xxx\')] LoggerInterface|null $logger) {}
  1233. ',
  1234. ];
  1235. }
  1236. /**
  1237. * @param array<int, bool> $expected
  1238. *
  1239. * @dataProvider provideIsConstantInvocationPhp81Cases
  1240. *
  1241. * @requires PHP 8.1
  1242. */
  1243. public function testIsConstantInvocationPhp81(array $expected, string $source): void
  1244. {
  1245. $this->doIsConstantInvocationTest($expected, $source);
  1246. }
  1247. public static function provideIsConstantInvocationPhp81Cases(): iterable
  1248. {
  1249. yield [
  1250. [5 => false, 15 => false],
  1251. '<?php
  1252. abstract class Baz
  1253. {
  1254. final public const Y = "i";
  1255. }
  1256. ',
  1257. ];
  1258. yield [
  1259. [4 => false, 12 => false, 23 => false],
  1260. '<?php
  1261. class Test {
  1262. public function __construct(
  1263. public $prop = new Foo,
  1264. ) {}
  1265. }
  1266. ',
  1267. ];
  1268. yield 'intersection' => [
  1269. [3 => false, 9 => false, 11 => false],
  1270. '<?php function foo(): \Foo&Bar {}',
  1271. ];
  1272. yield 'abstract method return intersection' => [
  1273. [6 => false, 16 => false, 21 => false, 23 => false, 25 => false, 27 => false, 29 => false],
  1274. '<?php
  1275. abstract class Baz
  1276. {
  1277. abstract public function foo(): Foo&Bar1&Bar2&Bar3&Bar4;
  1278. }
  1279. ',
  1280. ];
  1281. }
  1282. /**
  1283. * @param array<int, bool> $expected
  1284. *
  1285. * @dataProvider provideIsConstantInvocationPhp82Cases
  1286. *
  1287. * @requires PHP 8.2
  1288. */
  1289. public function testIsConstantInvocationPhp82(array $expected, string $source): void
  1290. {
  1291. $this->doIsConstantInvocationTest($expected, $source);
  1292. }
  1293. /**
  1294. * @return iterable<array{array<int, bool>, string}>
  1295. */
  1296. public static function provideIsConstantInvocationPhp82Cases(): iterable
  1297. {
  1298. yield [
  1299. [3 => false, 11 => false, 13 => false, 17 => false, 20 => false, 23 => false, 25 => false, 28 => false, 31 => false, 33 => false, 35 => false, 39 => false, 42 => false, 44 => false],
  1300. '<?php class Foo { public (\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var; }',
  1301. ];
  1302. yield [
  1303. [3 => false, 8 => false, 10 => false, 14 => false, 17 => false, 20 => false, 22 => false, 25 => false, 28 => false, 30 => false, 32 => false, 36 => false, 39 => false, 41 => false],
  1304. '<?php function foo ((\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var) {}',
  1305. ];
  1306. yield [
  1307. [3 => false, 6 => false, 12 => false],
  1308. '<?php enum someEnum: int
  1309. {
  1310. case E_ERROR = 123;
  1311. }',
  1312. ];
  1313. }
  1314. /**
  1315. * @param array<int, bool> $expected
  1316. *
  1317. * @dataProvider provideIsConstantInvocationPhp83Cases
  1318. *
  1319. * @requires PHP 8.3
  1320. */
  1321. public function testIsConstantInvocationPhp83(array $expected, string $source): void
  1322. {
  1323. $this->doIsConstantInvocationTest($expected, $source);
  1324. }
  1325. /**
  1326. * @return iterable<array{array<int, bool>, string}>
  1327. */
  1328. public static function provideIsConstantInvocationPhp83Cases(): iterable
  1329. {
  1330. yield [
  1331. [3 => false, 11 => false, 13 => false, 17 => true],
  1332. '<?php class Foo { public const string FOO = BAR; }',
  1333. ];
  1334. yield [
  1335. [3 => false, 11 => false, 13 => false, 17 => true],
  1336. '<?php class Foo { public const int FOO = BAR; }',
  1337. ];
  1338. yield [
  1339. [3 => false, 11 => false, 13 => false, 17 => true],
  1340. '<?php class Foo { public const bool FOO = BAR; }',
  1341. ];
  1342. yield [
  1343. [3 => false, 13 => false, 18 => true],
  1344. '<?php class Foo { public const array FOO = [BAR]; }',
  1345. ];
  1346. yield [
  1347. [3 => false, 11 => false, 13 => false, 17 => true],
  1348. '<?php class Foo { public const A FOO = BAR; }',
  1349. ];
  1350. yield [
  1351. [3 => false, 11 => false, 13 => false, 15 => false, 19 => true],
  1352. '<?php class Foo { public const A|B FOO = BAR; }',
  1353. ];
  1354. yield [
  1355. [3 => false, 11 => false, 13 => false, 15 => false, 19 => true],
  1356. '<?php class Foo { public const A&B FOO = BAR; }',
  1357. ];
  1358. yield [
  1359. [3 => false, 12 => false, 14 => false, 17 => false, 19 => false, 23 => true],
  1360. '<?php class Foo { public const (A&B)|C FOO = BAR; }',
  1361. ];
  1362. yield [
  1363. [3 => false, 12 => false, 14 => false, 18 => false, 20 => false, 23 => false, 27 => true],
  1364. '<?php class Foo { public const (A&B)|(C&D) FOO = BAR; }',
  1365. ];
  1366. yield [
  1367. [3 => false, 12 => false, 15 => false, 17 => false, 21 => false, 23 => false, 25 => false, 28 => false, 32 => true],
  1368. '<?php class Foo { public const (A&\B\C)|(D\E&F) FOO = BAR; }',
  1369. ];
  1370. }
  1371. public function testIsConstantInvocationInvalid(): void
  1372. {
  1373. $this->expectException(\LogicException::class);
  1374. $this->expectExceptionMessage('No T_STRING at given index 0, got "T_OPEN_TAG".');
  1375. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  1376. $tokensAnalyzer->isConstantInvocation(0);
  1377. }
  1378. /**
  1379. * @requires PHP 8.0
  1380. */
  1381. public function testIsConstantInvocationForNullSafeObjectOperator(): void
  1382. {
  1383. $tokens = Tokens::fromCode('<?php $a?->b?->c;');
  1384. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1385. foreach ($tokens as $index => $token) {
  1386. if (!$token->isGivenKind(T_STRING)) {
  1387. continue;
  1388. }
  1389. self::assertFalse($tokensAnalyzer->isConstantInvocation($index));
  1390. }
  1391. }
  1392. /**
  1393. * @param array<int, bool> $expected
  1394. *
  1395. * @dataProvider provideIsUnarySuccessorOperatorCases
  1396. */
  1397. public function testIsUnarySuccessorOperator(array $expected, string $source): void
  1398. {
  1399. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1400. foreach ($expected as $index => $isUnary) {
  1401. self::assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
  1402. if ($isUnary) {
  1403. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1404. self::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1405. }
  1406. }
  1407. }
  1408. public static function provideIsUnarySuccessorOperatorCases(): iterable
  1409. {
  1410. yield [
  1411. [2 => true],
  1412. '<?php $a++;',
  1413. ];
  1414. yield [
  1415. [2 => true],
  1416. '<?php $a--;',
  1417. ];
  1418. yield [
  1419. [3 => true],
  1420. '<?php $a ++;',
  1421. ];
  1422. yield [
  1423. [2 => true, 4 => false],
  1424. '<?php $a++ + 1;',
  1425. ];
  1426. yield [
  1427. [5 => true],
  1428. '<?php ${"a"}++;',
  1429. ];
  1430. yield [
  1431. [4 => true],
  1432. '<?php $foo->bar++;',
  1433. ];
  1434. yield [
  1435. [6 => true],
  1436. '<?php $foo->{"bar"}++;',
  1437. ];
  1438. yield 'array access' => [
  1439. [5 => true],
  1440. '<?php $a["foo"]++;',
  1441. ];
  1442. }
  1443. /**
  1444. * @param array<int, bool> $expected
  1445. *
  1446. * @dataProvider provideIsUnarySuccessorOperatorPre84Cases
  1447. *
  1448. * @requires PHP <8.4
  1449. */
  1450. public function testIsUnarySuccessorOperatorPre84(array $expected, string $source): void
  1451. {
  1452. $this->testIsUnarySuccessorOperator($expected, $source);
  1453. }
  1454. /**
  1455. * @return iterable<array{array<int, bool>, string}>
  1456. */
  1457. public static function provideIsUnarySuccessorOperatorPre84Cases(): iterable
  1458. {
  1459. yield 'array curly access' => [
  1460. [5 => true],
  1461. '<?php $a{"foo"}++;',
  1462. ];
  1463. }
  1464. /**
  1465. * @param list<int> $expected
  1466. *
  1467. * @dataProvider provideIsUnaryPredecessorOperatorCases
  1468. */
  1469. public function testIsUnaryPredecessorOperator(array $expected, string $source): void
  1470. {
  1471. $tokens = Tokens::fromCode($source);
  1472. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1473. foreach ($tokens as $index => $token) {
  1474. $expect = \in_array($index, $expected, true);
  1475. self::assertSame(
  1476. $expect,
  1477. $tokensAnalyzer->isUnaryPredecessorOperator($index),
  1478. \sprintf('Expected %sunary predecessor operator, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1479. );
  1480. if ($expect) {
  1481. self::assertFalse(
  1482. $tokensAnalyzer->isUnarySuccessorOperator($index),
  1483. \sprintf('Expected no unary successor operator, got @ %d "%s".', $index, var_export($token, true))
  1484. );
  1485. self::assertFalse(
  1486. $tokensAnalyzer->isBinaryOperator($index),
  1487. \sprintf('Expected no binary operator, got @ %d "%s".', $index, var_export($token, true))
  1488. );
  1489. }
  1490. }
  1491. }
  1492. public static function provideIsUnaryPredecessorOperatorCases(): iterable
  1493. {
  1494. yield [
  1495. [1],
  1496. '<?php ++$a;',
  1497. ];
  1498. yield [
  1499. [1],
  1500. '<?php --$a;',
  1501. ];
  1502. yield [
  1503. [1],
  1504. '<?php -- $a;',
  1505. ];
  1506. yield [
  1507. [5],
  1508. '<?php $a + ++$b;',
  1509. ];
  1510. yield [
  1511. [1, 2],
  1512. '<?php !!$a;',
  1513. ];
  1514. yield [
  1515. [5],
  1516. '<?php $a = &$b;',
  1517. ];
  1518. yield [
  1519. [3],
  1520. '<?php function &foo() {}',
  1521. ];
  1522. yield [
  1523. [1],
  1524. '<?php @foo();',
  1525. ];
  1526. yield [
  1527. [3, 8],
  1528. '<?php foo(+ $a, -$b);',
  1529. ];
  1530. yield [
  1531. [5, 11, 17],
  1532. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  1533. ];
  1534. yield [
  1535. [8],
  1536. '<?php function foo($a, ...$b) {}',
  1537. ];
  1538. yield [
  1539. [5, 6],
  1540. '<?php function foo(&...$b) {}',
  1541. ];
  1542. yield [
  1543. [7],
  1544. '<?php function foo(array ...$b) {}',
  1545. ];
  1546. yield [
  1547. [7],
  1548. '<?php $foo = function(...$a) {};',
  1549. ];
  1550. yield [
  1551. [10],
  1552. '<?php $foo = function($a, ...$b) {};',
  1553. ];
  1554. yield [
  1555. [9],
  1556. '<?php $foo = function(int &$x) {};',
  1557. ];
  1558. yield [
  1559. [9],
  1560. '<?php $foo = fn(int &$x) => null;',
  1561. ];
  1562. yield [
  1563. [],
  1564. '<?php fn() => A_CONSTANT & $object->property;',
  1565. ];
  1566. }
  1567. /**
  1568. * @param list<int> $expected
  1569. *
  1570. * @dataProvider provideIsBinaryOperatorCases
  1571. */
  1572. public function testIsBinaryOperator(array $expected, string $source): void
  1573. {
  1574. $tokens = Tokens::fromCode($source);
  1575. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1576. foreach ($tokens as $index => $token) {
  1577. $expect = \in_array($index, $expected, true);
  1578. self::assertSame(
  1579. $expect,
  1580. $tokensAnalyzer->isBinaryOperator($index),
  1581. \sprintf('Expected %sbinary operator, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1582. );
  1583. if ($expect) {
  1584. self::assertFalse(
  1585. $tokensAnalyzer->isUnarySuccessorOperator($index),
  1586. \sprintf('Expected no unary successor operator, got @ %d "%s".', $index, var_export($token, true))
  1587. );
  1588. self::assertFalse(
  1589. $tokensAnalyzer->isUnaryPredecessorOperator($index),
  1590. \sprintf('Expected no unary predecessor operator, got @ %d "%s".', $index, var_export($token, true))
  1591. );
  1592. }
  1593. }
  1594. }
  1595. public static function provideIsBinaryOperatorCases(): iterable
  1596. {
  1597. yield [
  1598. [8],
  1599. '<?php echo $a[1] + 1;',
  1600. ];
  1601. yield [
  1602. [3],
  1603. '<?php $a .= $b; ?>',
  1604. ];
  1605. yield [
  1606. [3],
  1607. '<?php $a . \'a\' ?>',
  1608. ];
  1609. yield [
  1610. [3],
  1611. '<?php $a &+ $b;',
  1612. ];
  1613. yield [
  1614. [4],
  1615. '<?php [] + [];',
  1616. ];
  1617. yield [
  1618. [3],
  1619. '<?php 1 + $b;',
  1620. ];
  1621. yield [
  1622. [3],
  1623. '<?php 0.2 + $b;',
  1624. ];
  1625. yield [
  1626. [6],
  1627. '<?php $a[1] + $b;',
  1628. ];
  1629. yield [
  1630. [3],
  1631. '<?php FOO + $b;',
  1632. ];
  1633. yield [
  1634. [5],
  1635. '<?php foo() + $b;',
  1636. ];
  1637. yield [
  1638. [6],
  1639. '<?php ${"foo"} + $b;',
  1640. ];
  1641. yield [
  1642. [2],
  1643. '<?php $a+$b;',
  1644. ];
  1645. yield [
  1646. [5],
  1647. '<?php $a /* foo */ + /* bar */ $b;',
  1648. ];
  1649. yield [
  1650. [3],
  1651. '<?php $a =
  1652. $b;',
  1653. ];
  1654. yield [
  1655. [3],
  1656. '<?php $a
  1657. = $b;',
  1658. ];
  1659. yield [
  1660. [3, 9],
  1661. '<?php $a = array("b" => "c", );',
  1662. ];
  1663. yield [
  1664. [3],
  1665. '<?php $a * -$b;',
  1666. ];
  1667. yield [
  1668. [3, 8],
  1669. '<?php $a = -2 / +5;',
  1670. ];
  1671. yield [
  1672. [3, 5 => false],
  1673. '<?php $a = &$b;',
  1674. ];
  1675. yield [
  1676. [4],
  1677. '<?php $a++ + $b;',
  1678. ];
  1679. yield [
  1680. [3, 7],
  1681. '<?php $a = FOO & $bar;',
  1682. ];
  1683. yield [
  1684. [3],
  1685. '<?php __LINE__ - 1;',
  1686. ];
  1687. yield [
  1688. [5],
  1689. '<?php `echo 1` + 1;',
  1690. ];
  1691. yield [
  1692. [3],
  1693. '<?php $a ** $b;',
  1694. ];
  1695. yield [
  1696. [3],
  1697. '<?php $a **= $b;',
  1698. ];
  1699. yield [
  1700. [3],
  1701. '<?php $a = "{$value}-{$theSwitch}";',
  1702. ];
  1703. yield [
  1704. [],
  1705. '<?=$path?>-<?=$id?>',
  1706. ];
  1707. $operators = [
  1708. '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
  1709. '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=',
  1710. ];
  1711. foreach ($operators as $operator) {
  1712. yield [
  1713. [3],
  1714. '<?php $a '.$operator.' $b;',
  1715. ];
  1716. }
  1717. yield [
  1718. [3],
  1719. '<?php $a <=> $b;',
  1720. ];
  1721. yield [
  1722. [3],
  1723. '<?php $a ?? $b;',
  1724. ];
  1725. yield [
  1726. [],
  1727. '<?php try {} catch (A | B $e) {}',
  1728. ];
  1729. yield [
  1730. [5, 11],
  1731. '<?php fn() => $object->property & A_CONSTANT;',
  1732. ];
  1733. }
  1734. /**
  1735. * @dataProvider provideIsArrayCases
  1736. */
  1737. public function testIsArray(string $source, int $tokenIndex, bool $isMultiLineArray = false): void
  1738. {
  1739. $tokens = Tokens::fromCode($source);
  1740. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1741. self::assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
  1742. self::assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), \sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
  1743. }
  1744. /**
  1745. * @return iterable<array{0: string, 1: int, 2?: bool}>
  1746. */
  1747. public static function provideIsArrayCases(): iterable
  1748. {
  1749. yield [
  1750. '<?php
  1751. array("a" => 1);
  1752. ',
  1753. 2,
  1754. ];
  1755. yield [
  1756. '<?php
  1757. ["a" => 2];
  1758. ',
  1759. 2, false,
  1760. ];
  1761. yield [
  1762. '<?php
  1763. array(
  1764. "a" => 3
  1765. );
  1766. ',
  1767. 2, true,
  1768. ];
  1769. yield [
  1770. '<?php
  1771. [
  1772. "a" => 4
  1773. ];
  1774. ',
  1775. 2, true,
  1776. ];
  1777. yield [
  1778. '<?php
  1779. array(
  1780. "a" => array(5, 6, 7),
  1781. 8 => new \Exception(\'Hello\')
  1782. );
  1783. ',
  1784. 2, true,
  1785. ];
  1786. yield [
  1787. // mix short array syntax
  1788. '<?php
  1789. array(
  1790. "a" => [9, 10, 11],
  1791. 12 => new \Exception(\'Hello\')
  1792. );
  1793. ',
  1794. 2, true,
  1795. ];
  1796. // Windows/Max EOL testing
  1797. yield [
  1798. "<?php\r\narray('a' => 13);\r\n",
  1799. 1,
  1800. ];
  1801. yield [
  1802. "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
  1803. 2, true,
  1804. ];
  1805. }
  1806. /**
  1807. * @param list<int> $tokenIndexes
  1808. *
  1809. * @dataProvider provideIsArray71Cases
  1810. */
  1811. public function testIsArray71(string $source, array $tokenIndexes): void
  1812. {
  1813. $tokens = Tokens::fromCode($source);
  1814. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1815. foreach ($tokens as $index => $token) {
  1816. $expect = \in_array($index, $tokenIndexes, true);
  1817. self::assertSame(
  1818. $expect,
  1819. $tokensAnalyzer->isArray($index),
  1820. \sprintf('Expected %sarray, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1821. );
  1822. }
  1823. }
  1824. public static function provideIsArray71Cases(): iterable
  1825. {
  1826. yield [
  1827. '<?php
  1828. [$a] = $z;
  1829. ["a" => $a, "b" => $b] = $array;
  1830. $c = [$d, $e] = $array[$a];
  1831. [[$a, $b], [$c, $d]] = $d;
  1832. $array = []; $d = array();
  1833. ',
  1834. [76, 84],
  1835. ];
  1836. }
  1837. /**
  1838. * @param list<int> $expected
  1839. *
  1840. * @dataProvider provideIsBinaryOperator80Cases
  1841. *
  1842. * @requires PHP 8.0
  1843. */
  1844. public function testIsBinaryOperator80(array $expected, string $source): void
  1845. {
  1846. $this->testIsBinaryOperator($expected, $source);
  1847. }
  1848. public static function provideIsBinaryOperator80Cases(): iterable
  1849. {
  1850. yield [
  1851. [],
  1852. '<?php function foo(array|string $x) {}',
  1853. ];
  1854. yield [
  1855. [],
  1856. '<?php function foo(string|array $x) {}',
  1857. ];
  1858. yield [
  1859. [],
  1860. '<?php function foo(int|callable $x) {}',
  1861. ];
  1862. yield [
  1863. [],
  1864. '<?php function foo(callable|int $x) {}',
  1865. ];
  1866. yield [
  1867. [],
  1868. '<?php function foo(bool|int &$x) {}',
  1869. ];
  1870. }
  1871. /**
  1872. * @param list<int> $expected
  1873. *
  1874. * @dataProvider provideIsBinaryOperator81Cases
  1875. *
  1876. * @requires PHP 8.1
  1877. */
  1878. public function testIsBinaryOperator81(array $expected, string $source): void
  1879. {
  1880. $this->testIsBinaryOperator($expected, $source);
  1881. }
  1882. public static function provideIsBinaryOperator81Cases(): iterable
  1883. {
  1884. yield 'type intersection' => [
  1885. [],
  1886. '<?php function foo(array&string $x) {}',
  1887. ];
  1888. }
  1889. /**
  1890. * @param list<int> $expected
  1891. *
  1892. * @dataProvider provideIsBinaryOperator82Cases
  1893. *
  1894. * @requires PHP 8.2
  1895. */
  1896. public function testIsBinaryOperator82(array $expected, string $source): void
  1897. {
  1898. $this->testIsBinaryOperator($expected, $source);
  1899. }
  1900. public static function provideIsBinaryOperator82Cases(): iterable
  1901. {
  1902. yield [
  1903. [],
  1904. '<?php class Dnf { public static I|(P&S11) $f2;}',
  1905. ];
  1906. yield [
  1907. [],
  1908. '<?php function Foo((A&B)|I $x): (X&Z)|(p\f\G&Y\Z)|z { return foo();}',
  1909. ];
  1910. $particularEndOfFile = 'A|(B&C); }';
  1911. yield \sprintf('block "%s" at the end of file that is a type', $particularEndOfFile) => [
  1912. [],
  1913. '<?php abstract class A { abstract function foo(): '.$particularEndOfFile,
  1914. ];
  1915. yield \sprintf('block "%s" at the end of file that is not a type', $particularEndOfFile) => [
  1916. [12, 15],
  1917. '<?php function foo() { return '.$particularEndOfFile,
  1918. ];
  1919. }
  1920. /**
  1921. * @param list<int> $expected
  1922. *
  1923. * @dataProvider provideIsBinaryOperatorPre84Cases
  1924. *
  1925. * @requires PHP <8.4
  1926. */
  1927. public function testIsBinaryOperatorPre84(array $expected, string $source): void
  1928. {
  1929. $this->testIsBinaryOperator($expected, $source);
  1930. }
  1931. /**
  1932. * @return iterable<array{list<int>, string}>
  1933. */
  1934. public static function provideIsBinaryOperatorPre84Cases(): iterable
  1935. {
  1936. yield [
  1937. [8],
  1938. '<?php echo $a{1} + 1;',
  1939. ];
  1940. }
  1941. /**
  1942. * @dataProvider provideArrayExceptionsCases
  1943. */
  1944. public function testIsNotArray(string $source, int $tokenIndex): void
  1945. {
  1946. $tokens = Tokens::fromCode($source);
  1947. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1948. self::assertFalse($tokensAnalyzer->isArray($tokenIndex));
  1949. }
  1950. /**
  1951. * @dataProvider provideArrayExceptionsCases
  1952. */
  1953. public function testIsMultiLineArrayException(string $source, int $tokenIndex): void
  1954. {
  1955. $this->expectException(\InvalidArgumentException::class);
  1956. $tokens = Tokens::fromCode($source);
  1957. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1958. $tokensAnalyzer->isArrayMultiLine($tokenIndex);
  1959. }
  1960. /**
  1961. * @return iterable<array{string, int}>
  1962. */
  1963. public static function provideArrayExceptionsCases(): iterable
  1964. {
  1965. yield ['<?php $a;', 1];
  1966. yield ["<?php\n \$a = (0+1); // [0,1]", 4];
  1967. yield ['<?php $text = "foo $bbb[0] bar";', 8];
  1968. yield ['<?php $text = "foo ${aaa[123]} bar";', 9];
  1969. }
  1970. public function testIsBlockMultilineException(): void
  1971. {
  1972. $this->expectException(\LogicException::class);
  1973. $tokens = Tokens::fromCode('<?php foo(1, 2, 3);');
  1974. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1975. $tokensAnalyzer->isBlockMultiline($tokens, 1);
  1976. }
  1977. /**
  1978. * @dataProvider provideIsBlockMultilineCases
  1979. */
  1980. public function testIsBlockMultiline(bool $isBlockMultiline, string $source, int $tokenIndex): void
  1981. {
  1982. $tokens = Tokens::fromCode($source);
  1983. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1984. self::assertSame($isBlockMultiline, $tokensAnalyzer->isBlockMultiline($tokens, $tokenIndex));
  1985. }
  1986. /**
  1987. * @return iterable<array{bool, string, int}>
  1988. */
  1989. public static function provideIsBlockMultilineCases(): iterable
  1990. {
  1991. yield [
  1992. false,
  1993. '<?php foo(1, 2, 3);',
  1994. 2,
  1995. ];
  1996. yield [
  1997. true,
  1998. '<?php foo(1,
  1999. 2,
  2000. 3
  2001. );',
  2002. 2,
  2003. ];
  2004. yield [
  2005. false,
  2006. '<?php foo(1, "Multi
  2007. string", 2, 3);',
  2008. 2,
  2009. ];
  2010. yield [
  2011. false,
  2012. '<?php foo(1, havingNestedBlockThatIsMultilineDoesNotMakeTheMainBlockMultiline(
  2013. "a",
  2014. "b"
  2015. ), 2, 3);',
  2016. 2,
  2017. ];
  2018. }
  2019. /**
  2020. * @param array{visibility: ?int, static: bool, abstract: bool, final: bool} $expected
  2021. *
  2022. * @dataProvider provideGetFunctionPropertiesCases
  2023. */
  2024. public function testGetFunctionProperties(string $source, int $index, array $expected): void
  2025. {
  2026. $tokens = Tokens::fromCode($source);
  2027. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2028. $attributes = $tokensAnalyzer->getMethodAttributes($index);
  2029. self::assertSame($expected, $attributes);
  2030. }
  2031. public static function provideGetFunctionPropertiesCases(): iterable
  2032. {
  2033. $defaultAttributes = [
  2034. 'visibility' => null,
  2035. 'static' => false,
  2036. 'abstract' => false,
  2037. 'final' => false,
  2038. ];
  2039. $template = '
  2040. <?php
  2041. class TestClass {
  2042. %s function a() {
  2043. //
  2044. }
  2045. }
  2046. ';
  2047. $cases = [];
  2048. $attributes = $defaultAttributes;
  2049. $attributes['visibility'] = T_PRIVATE;
  2050. $cases[] = [\sprintf($template, 'private'), 10, $attributes];
  2051. $attributes = $defaultAttributes;
  2052. $attributes['visibility'] = T_PUBLIC;
  2053. $cases[] = [\sprintf($template, 'public'), 10, $attributes];
  2054. $attributes = $defaultAttributes;
  2055. $attributes['visibility'] = T_PROTECTED;
  2056. $cases[] = [\sprintf($template, 'protected'), 10, $attributes];
  2057. $attributes = $defaultAttributes;
  2058. $attributes['visibility'] = null;
  2059. $attributes['static'] = true;
  2060. $cases[] = [\sprintf($template, 'static'), 10, $attributes];
  2061. $attributes = $defaultAttributes;
  2062. $attributes['visibility'] = T_PUBLIC;
  2063. $attributes['static'] = true;
  2064. $attributes['final'] = true;
  2065. $cases[] = [\sprintf($template, 'final public static'), 14, $attributes];
  2066. $attributes = $defaultAttributes;
  2067. $attributes['visibility'] = null;
  2068. $attributes['abstract'] = true;
  2069. $cases[] = [\sprintf($template, 'abstract'), 10, $attributes];
  2070. $attributes = $defaultAttributes;
  2071. $attributes['visibility'] = T_PUBLIC;
  2072. $attributes['abstract'] = true;
  2073. $cases[] = [\sprintf($template, 'abstract public'), 12, $attributes];
  2074. $attributes = $defaultAttributes;
  2075. $cases[] = [\sprintf($template, ''), 8, $attributes];
  2076. return $cases;
  2077. }
  2078. public function testIsWhilePartOfDoWhile(): void
  2079. {
  2080. $source = <<<'SRC'
  2081. <?php
  2082. // `not do`
  2083. while(false) {
  2084. }
  2085. while (false);
  2086. while (false)?>
  2087. <?php
  2088. if(false){
  2089. }while(false);
  2090. if(false){
  2091. }while(false)?><?php
  2092. while(false){}while(false){}
  2093. while ($i <= 10):
  2094. echo $i;
  2095. $i++;
  2096. endwhile;
  2097. ?>
  2098. <?php while(false): ?>
  2099. <?php endwhile ?>
  2100. <?php
  2101. // `do`
  2102. do{
  2103. } while(false);
  2104. do{
  2105. } while(false)?>
  2106. <?php
  2107. if (false){}do{}while(false);
  2108. // `not do`, `do`
  2109. if(false){}while(false){}do{}while(false);
  2110. SRC;
  2111. $expected = [
  2112. 3 => false,
  2113. 12 => false,
  2114. 19 => false,
  2115. 34 => false,
  2116. 47 => false,
  2117. 53 => false,
  2118. 59 => false,
  2119. 66 => false,
  2120. 91 => false,
  2121. 112 => true,
  2122. 123 => true,
  2123. 139 => true,
  2124. 153 => false,
  2125. 162 => true,
  2126. ];
  2127. $tokens = Tokens::fromCode($source);
  2128. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2129. foreach ($tokens as $index => $token) {
  2130. if (!$token->isGivenKind(T_WHILE)) {
  2131. continue;
  2132. }
  2133. $isExpected = $expected[$index] ?? null;
  2134. self::assertSame(
  2135. $isExpected,
  2136. $tokensAnalyzer->isWhilePartOfDoWhile($index),
  2137. \sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $isExpected ? '' : 'not ')
  2138. );
  2139. }
  2140. }
  2141. /**
  2142. * @dataProvider provideIsEnumCaseCases
  2143. *
  2144. * @param array<int, bool> $expected
  2145. *
  2146. * @requires PHP 8.1
  2147. */
  2148. public function testIsEnumCase(string $source, array $expected): void
  2149. {
  2150. $tokens = Tokens::fromCode($source);
  2151. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2152. foreach ($tokens as $index => $token) {
  2153. if (!$token->isGivenKind(T_CASE)) {
  2154. try {
  2155. $tokensAnalyzer->isEnumCase($index);
  2156. self::fail('TokensAnalyzer::isEnumCase() did not throw LogicException.');
  2157. } catch (\Throwable $e) {
  2158. self::assertInstanceOf(\LogicException::class, $e);
  2159. self::assertMatchesRegularExpression('/^No T_CASE given at index \d+, got \S+ instead\.$/', $e->getMessage());
  2160. }
  2161. continue;
  2162. }
  2163. self::assertSame($expected[$index], $tokensAnalyzer->isEnumCase($index));
  2164. }
  2165. }
  2166. public static function provideIsEnumCaseCases(): iterable
  2167. {
  2168. yield 'switch only' => [
  2169. '<?php
  2170. function bar(string $a): string
  2171. {
  2172. switch ($a) {
  2173. case \'one\':
  2174. return $a;
  2175. case \'two\':
  2176. default:
  2177. return strtoupper($a);
  2178. }
  2179. }
  2180. ',
  2181. [
  2182. 23 => false,
  2183. 33 => false,
  2184. ],
  2185. ];
  2186. yield 'pure enum' => [
  2187. '<?php
  2188. enum Foo
  2189. {
  2190. case One;
  2191. case Two;
  2192. }
  2193. ',
  2194. [
  2195. 7 => true,
  2196. 12 => true,
  2197. ],
  2198. ];
  2199. yield 'pure enum with switch' => [
  2200. '<?php
  2201. enum Foo
  2202. {
  2203. case One;
  2204. case Two;
  2205. public static function getLowerName(self $instance): string
  2206. {
  2207. switch ($instance->name) {
  2208. case \'One\':
  2209. case \'Two\':
  2210. return strtolower($instance->name);
  2211. }
  2212. }
  2213. }
  2214. ',
  2215. [
  2216. 7 => true,
  2217. 12 => true,
  2218. 45 => false,
  2219. 50 => false,
  2220. ],
  2221. ];
  2222. yield 'backed enum' => [
  2223. '<?php
  2224. enum Suit: string
  2225. {
  2226. case Hearts = \'hearts\';
  2227. case Spades = \'spades\';
  2228. case Clubs = \'clubs\';
  2229. case Diamonds = \'diamonds\';
  2230. }
  2231. ',
  2232. [
  2233. 10 => true,
  2234. 19 => true,
  2235. 28 => true,
  2236. 37 => true,
  2237. ],
  2238. ];
  2239. yield 'backed enum with switch' => [
  2240. '<?php
  2241. enum Suit: string
  2242. {
  2243. case Hearts = \'hearts\';
  2244. case Spades = \'spades\';
  2245. case Clubs = \'clubs\';
  2246. case Diamonds = \'diamonds\';
  2247. public static function getUppercasedValue(self $instance): string
  2248. {
  2249. switch ($instance->value) {
  2250. case \'hearts\':
  2251. case \'spades\':
  2252. return strtoupper($instance->value);
  2253. default:
  2254. return $instance->value;
  2255. }
  2256. }
  2257. }
  2258. ',
  2259. [
  2260. 10 => true,
  2261. 19 => true,
  2262. 28 => true,
  2263. 37 => true,
  2264. 74 => false,
  2265. 79 => false,
  2266. ],
  2267. ];
  2268. }
  2269. /**
  2270. * @param array<int, list<int>>|list<int> $expected
  2271. *
  2272. * @dataProvider provideGetImportUseIndexesCases
  2273. */
  2274. public function testGetImportUseIndexes(array $expected, string $input, bool $perNamespace = false): void
  2275. {
  2276. $tokens = Tokens::fromCode($input);
  2277. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2278. self::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  2279. }
  2280. public static function provideGetImportUseIndexesCases(): iterable
  2281. {
  2282. yield [
  2283. [1, 8],
  2284. '<?php use E\F?><?php use A\B;',
  2285. ];
  2286. yield [
  2287. [[1], [14], [29]],
  2288. '<?php
  2289. use T\A;
  2290. namespace A { use D\C; }
  2291. namespace b { use D\C; }
  2292. ',
  2293. true,
  2294. ];
  2295. yield [
  2296. [[1, 8]],
  2297. '<?php use D\B; use A\C?>',
  2298. true,
  2299. ];
  2300. yield [
  2301. [1, 8],
  2302. '<?php use D\B; use A\C?>',
  2303. ];
  2304. yield [
  2305. [7, 22],
  2306. '<?php
  2307. namespace A { use D\C; }
  2308. namespace b { use D\C; }
  2309. ',
  2310. ];
  2311. yield [
  2312. [3, 10, 34, 45, 54, 59, 77, 95],
  2313. <<<'EOF'
  2314. use Zoo\Bar;
  2315. use Foo\Bar;
  2316. use Foo\Zar\Baz;
  2317. <?php
  2318. use Foo\Bar;
  2319. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  2320. use Foo\Bir as FBB;
  2321. use Foo\Zar\Baz;
  2322. use SomeClass;
  2323. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  2324. use Zoo\Bar;
  2325. $a = new someclass();
  2326. use Zoo\Tar;
  2327. class AnnotatedClass
  2328. {
  2329. }
  2330. EOF,
  2331. ];
  2332. yield [
  2333. [1, 22, 41],
  2334. '<?php
  2335. use some\a\{ClassA, ClassB, ClassC as C};
  2336. use function some\a\{fn_a, fn_b, fn_c};
  2337. use const some\a\{ConstA, ConstB, ConstC};
  2338. ',
  2339. ];
  2340. yield [
  2341. [[1, 22, 41]],
  2342. '<?php
  2343. use some\a\{ClassA, ClassB, ClassC as C};
  2344. use function some\a\{fn_a, fn_b, fn_c};
  2345. use const some\a\{ConstA, ConstB, ConstC};
  2346. ',
  2347. true,
  2348. ];
  2349. yield [
  2350. [1, 23, 43],
  2351. '<?php
  2352. use some\a\{ClassA, ClassB, ClassC as C,};
  2353. use function some\a\{fn_a, fn_b, fn_c,};
  2354. use const some\a\{ConstA, ConstB, ConstC,};
  2355. ',
  2356. ];
  2357. yield [
  2358. [[1, 23, 43]],
  2359. '<?php
  2360. use some\a\{ClassA, ClassB, ClassC as C,};
  2361. use function some\a\{fn_a, fn_b, fn_c,};
  2362. use const some\a\{ConstA, ConstB, ConstC,};
  2363. ',
  2364. true,
  2365. ];
  2366. }
  2367. public function testGetClassyElementsWithMultipleNestedAnonymousClass(): void
  2368. {
  2369. $source = '<?php
  2370. class MyTestWithAnonymousClass extends TestCase
  2371. {
  2372. public function setUp()
  2373. {
  2374. $provider = new class(function () {}) {};
  2375. }
  2376. public function testSomethingWithMoney(
  2377. Money $amount
  2378. ) {
  2379. $a = new class(function () {
  2380. new class(function () {
  2381. new class(function () {})
  2382. {
  2383. const A=1;
  2384. };
  2385. })
  2386. {
  2387. const B=1;
  2388. public function foo() {
  2389. $c = new class() {const AA=3;};
  2390. $d = new class {const AB=3;};
  2391. }
  2392. };
  2393. })
  2394. {
  2395. const C=1;
  2396. };
  2397. }
  2398. }';
  2399. $tokens = Tokens::fromCode($source);
  2400. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2401. $elements = $tokensAnalyzer->getClassyElements();
  2402. self::assertSame([
  2403. 13 => [
  2404. 'classIndex' => 1,
  2405. 'token' => $tokens[13],
  2406. 'type' => 'method', // setUp
  2407. ],
  2408. 46 => [
  2409. 'classIndex' => 1,
  2410. 'token' => $tokens[46],
  2411. 'type' => 'method', // testSomethingWithMoney
  2412. ],
  2413. 100 => [
  2414. 'classIndex' => 87,
  2415. 'token' => $tokens[100], // const A
  2416. 'type' => 'const',
  2417. ],
  2418. 115 => [
  2419. 'classIndex' => 65,
  2420. 'token' => $tokens[115], // const B
  2421. 'type' => 'const',
  2422. ],
  2423. 124 => [
  2424. 'classIndex' => 65, // $a
  2425. 'token' => $tokens[124],
  2426. 'type' => 'method', // foo
  2427. ],
  2428. 143 => [
  2429. 'classIndex' => 138,
  2430. 'token' => $tokens[143], // const AA
  2431. 'type' => 'const',
  2432. ],
  2433. 161 => [
  2434. 'classIndex' => 158,
  2435. 'token' => $tokens[161], // const AB
  2436. 'type' => 'const',
  2437. ],
  2438. ], $elements);
  2439. }
  2440. /**
  2441. * @dataProvider provideIsSuperGlobalCases
  2442. */
  2443. public function testIsSuperGlobal(bool $expected, string $source, int $index): void
  2444. {
  2445. $tokens = Tokens::fromCode($source);
  2446. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2447. self::assertSame($expected, $tokensAnalyzer->isSuperGlobal($index));
  2448. }
  2449. /**
  2450. * @return iterable<array{bool, string, int}>
  2451. */
  2452. public static function provideIsSuperGlobalCases(): iterable
  2453. {
  2454. $superNames = [
  2455. '$_COOKIE',
  2456. '$_ENV',
  2457. '$_FILES',
  2458. '$_GET',
  2459. '$_POST',
  2460. '$_REQUEST',
  2461. '$_SERVER',
  2462. '$_SESSION',
  2463. '$GLOBALS',
  2464. ];
  2465. $cases = [];
  2466. foreach ($superNames as $superName) {
  2467. $cases[] = [
  2468. true,
  2469. \sprintf('<?php echo %s[0];', $superName),
  2470. 3,
  2471. ];
  2472. }
  2473. $notGlobalCodeCases = [
  2474. '<?php echo 1; $a = static function($b) use ($a) { $a->$b(); }; // $_SERVER',
  2475. '<?php class Foo{}?> <?php $_A = 1; /* $_SESSION */',
  2476. ];
  2477. foreach ($notGlobalCodeCases as $notGlobalCodeCase) {
  2478. $tokensCount = \count(Tokens::fromCode($notGlobalCodeCase));
  2479. for ($i = 0; $i < $tokensCount; ++$i) {
  2480. $cases[] = [
  2481. false,
  2482. $notGlobalCodeCase,
  2483. $i,
  2484. ];
  2485. }
  2486. }
  2487. return $cases;
  2488. }
  2489. /**
  2490. * @dataProvider provideGetClassyModifiersCases
  2491. *
  2492. * @param array<string, null|int> $expectedModifiers
  2493. */
  2494. public function testGetClassyModifiers(array $expectedModifiers, int $index, string $source): void
  2495. {
  2496. $tokens = Tokens::fromCode($source);
  2497. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2498. self::assertSame($expectedModifiers, $tokensAnalyzer->getClassyModifiers($index));
  2499. }
  2500. public static function provideGetClassyModifiersCases(): iterable
  2501. {
  2502. yield 'final' => [
  2503. ['final' => 1, 'abstract' => null, 'readonly' => null],
  2504. 3,
  2505. '<?php final class Foo {}',
  2506. ];
  2507. yield 'abstract' => [
  2508. ['final' => null, 'abstract' => 3, 'readonly' => null],
  2509. 5,
  2510. '<?php /* comment */ abstract class Foo {}',
  2511. ];
  2512. }
  2513. /**
  2514. * @requires PHP 8.2
  2515. *
  2516. * @dataProvider provideGetClassyModifiersOnPhp82Cases
  2517. *
  2518. * @param array<string, null|int> $expectedModifiers
  2519. */
  2520. public function testGetClassyModifiersOnPhp82(array $expectedModifiers, int $index, string $source): void
  2521. {
  2522. $this->testGetClassyModifiers($expectedModifiers, $index, $source);
  2523. }
  2524. public static function provideGetClassyModifiersOnPhp82Cases(): iterable
  2525. {
  2526. yield 'readonly' => [
  2527. ['final' => null, 'abstract' => null, 'readonly' => 1],
  2528. 3,
  2529. '<?php readonly class Foo {}',
  2530. ];
  2531. yield 'readonly final' => [
  2532. ['final' => 3, 'abstract' => null, 'readonly' => 1],
  2533. 5,
  2534. '<?php readonly final class Foo {}',
  2535. ];
  2536. yield 'readonly final reversed' => [
  2537. ['final' => 1, 'abstract' => null, 'readonly' => 3],
  2538. 5,
  2539. '<?php final readonly class Foo {}',
  2540. ];
  2541. yield 'readonly final reversed + comment' => [
  2542. ['final' => 1, 'abstract' => null, 'readonly' => 5],
  2543. 7,
  2544. '<?php final /* comment */ readonly class Foo {}',
  2545. ];
  2546. yield 'readonly abstract' => [
  2547. ['final' => null, 'abstract' => 3, 'readonly' => 1],
  2548. 5,
  2549. '<?php readonly abstract class Foo {}',
  2550. ];
  2551. yield 'readonly abstract reversed' => [
  2552. ['final' => null, 'abstract' => 1, 'readonly' => 3],
  2553. 5,
  2554. '<?php abstract readonly class Foo {}',
  2555. ];
  2556. yield 'readonly abstract reversed + comment' => [
  2557. ['final' => null, 'abstract' => 1, 'readonly' => 5],
  2558. 7,
  2559. '<?php abstract /* comment */ readonly class Foo {}',
  2560. ];
  2561. }
  2562. public function testGetClassyModifiersForNonClassyThrowsAnException(): void
  2563. {
  2564. $tokens = Tokens::fromCode('<?php echo 1;');
  2565. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2566. $this->expectException(\InvalidArgumentException::class);
  2567. $tokensAnalyzer->getClassyModifiers(1);
  2568. }
  2569. /**
  2570. * @dataProvider provideGetLastTokenIndexOfArrowFunctionCases
  2571. *
  2572. * @param array<int, int> $expectations
  2573. */
  2574. public function testGetLastTokenIndexOfArrowFunction(array $expectations, string $source): void
  2575. {
  2576. $tokens = Tokens::fromCode($source);
  2577. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2578. $indices = [];
  2579. foreach ($expectations as $index => $expectedEndIndex) {
  2580. $indices[$index] = $tokensAnalyzer->getLastTokenIndexOfArrowFunction($index);
  2581. }
  2582. self::assertSame($expectations, $indices);
  2583. }
  2584. public static function provideGetLastTokenIndexOfArrowFunctionCases(): iterable
  2585. {
  2586. yield 'simple cases' => [
  2587. [
  2588. 2 => 11,
  2589. 16 => 25,
  2590. 28 => 39,
  2591. 46 => 61,
  2592. ],
  2593. '<?php
  2594. fn(array $x) => $x;
  2595. static fn(): int => $x;
  2596. fn($x = 42) => $x;
  2597. $eq = fn ($x, $y) => $x == $y;
  2598. ',
  2599. ];
  2600. yield 'references, splat and arrow cases' => [
  2601. [
  2602. 2 => 10,
  2603. 13 => 21,
  2604. 24 => 35,
  2605. 42 => 51,
  2606. 65 => 77,
  2607. ],
  2608. '<?php
  2609. fn(&$x) => $x;
  2610. fn&($x) => $x;
  2611. fn($x, ...$rest) => $rest;
  2612. $fn = fn(&$x) => $x++;
  2613. $y = &$fn($x);
  2614. fn($x, &...$rest) => 1;
  2615. ',
  2616. ];
  2617. yield 'different endings' => [
  2618. [
  2619. 9 => 21,
  2620. 31 => 43,
  2621. ],
  2622. '<?php
  2623. $results = array_map(
  2624. fn ($item) => $item * 2,
  2625. $list
  2626. );
  2627. return fn ($y) => $x * $y ?>
  2628. ',
  2629. ];
  2630. yield 'nested arrow function' => [
  2631. [
  2632. 1 => 26,
  2633. 14 => 25,
  2634. ],
  2635. '<?php fn(array $x, $z) => (fn(int $z):bool => $z);',
  2636. ];
  2637. yield 'arrow function as argument' => [
  2638. [
  2639. 5 => 14,
  2640. ],
  2641. '<?php return foo(fn(array $x) => $x);',
  2642. ];
  2643. yield 'arrow function as collection item' => [
  2644. [
  2645. 9 => 18,
  2646. 26 => 35,
  2647. 46 => 55,
  2648. 62 => 69,
  2649. ],
  2650. '<?php return [
  2651. [1, fn(array $x) => $x1, 1],
  2652. [fn(array $x) => $x2, 1],
  2653. [1, fn(array $x) => $x3],
  2654. ([(fn($x4) => $x5)]),
  2655. ];',
  2656. ];
  2657. yield 'nested inside anonymous class' => [
  2658. [
  2659. 1 => 46,
  2660. 33 => 41,
  2661. ],
  2662. '<?php fn($x) => $a = new class($x) { public function foo() { return fn(&$x) => $x; } };',
  2663. ];
  2664. yield 'array destructuring' => [
  2665. [
  2666. 4 => 13,
  2667. ],
  2668. '<?php return [fn(array $x) => $x1] = $x;',
  2669. ];
  2670. yield 'array_map() callback with different token blocks' => [
  2671. [
  2672. 9 => 28,
  2673. ],
  2674. '<?php
  2675. $a = array_map(
  2676. fn (array $item) => $item[\'callback\']($item[\'value\']),
  2677. [/* items */]
  2678. );
  2679. ',
  2680. ];
  2681. yield 'arrow function returning array' => [
  2682. [
  2683. 5 => 21,
  2684. ],
  2685. '<?php $z = fn ($a) => [0, 1, $a];',
  2686. ];
  2687. }
  2688. public function testCannotGetLastTokenIndexOfArrowFunctionForNonFnToken(): void
  2689. {
  2690. $tokens = Tokens::fromCode('<?php echo 1;');
  2691. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2692. $this->expectException(\InvalidArgumentException::class);
  2693. $tokensAnalyzer->getLastTokenIndexOfArrowFunction(1);
  2694. }
  2695. /**
  2696. * @param array<int, bool> $expected
  2697. */
  2698. private function doIsConstantInvocationTest(array $expected, string $source): void
  2699. {
  2700. $tokens = Tokens::fromCode($source);
  2701. self::assertCount(
  2702. $tokens->countTokenKind(T_STRING),
  2703. $expected,
  2704. 'All T_STRING tokens must be tested'
  2705. );
  2706. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2707. foreach ($expected as $index => $expectedValue) {
  2708. self::assertSame(
  2709. $expectedValue,
  2710. $tokensAnalyzer->isConstantInvocation($index),
  2711. \sprintf('Token at index '.$index.' should match the expected value (%s).', $expectedValue ? 'true' : 'false')
  2712. );
  2713. }
  2714. }
  2715. }