1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Tokenizer;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\Tokenizer\Tokens;
- use PhpCsFixer\Tokenizer\TokensAnalyzer;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- * @author Max Voloshin <voloshin.dp@gmail.com>
- * @author Gregor Harlan <gharlan@web.de>
- * @author SpacePossum
- *
- * @internal
- *
- * @covers \PhpCsFixer\Tokenizer\TokensAnalyzer
- */
- final class TokensAnalyzerTest extends TestCase
- {
- /**
- * @dataProvider provideGetClassyElementsCases
- */
- public function testGetClassyElements(array $expectedElements, string $source): void
- {
- $tokens = Tokens::fromCode($source);
- foreach ($expectedElements as $index => $element) {
- $expectedElements[$index] = [
- 'token' => $tokens[$index],
- 'type' => $element['type'],
- 'classIndex' => $element['classIndex'],
- ];
- }
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertSame(
- $expectedElements,
- $tokensAnalyzer->getClassyElements()
- );
- }
- public function provideGetClassyElementsCases()
- {
- yield 'trait import' => [
- [
- 10 => [
- 'type' => 'trait_import',
- 'classIndex' => 4,
- ],
- 19 => [
- 'type' => 'trait_import',
- 'classIndex' => 4,
- ],
- 24 => [
- 'type' => 'const',
- 'classIndex' => 4,
- ],
- 35 => [
- 'type' => 'method',
- 'classIndex' => 4,
- ],
- 55 => [
- 'type' => 'trait_import',
- 'classIndex' => 49,
- ],
- 64 => [
- 'type' => 'method',
- 'classIndex' => 49,
- ],
- ],
- '<?php
- /** */
- class Foo
- {
- use A\B;
- //
- use Foo;
- const A = 1;
- public function foo()
- {
- $a = new class()
- {
- use Z; // nested trait import
- public function bar()
- {
- echo 123;
- }
- };
- $a->bar();
- }
- }',
- ];
- yield [
- [
- 9 => [
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 14 => [
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 19 => [
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 28 => [
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 42 => [
- 'type' => 'const',
- 'classIndex' => 1,
- ],
- 53 => [
- 'type' => 'method',
- 'classIndex' => 1,
- ],
- 83 => [
- 'type' => 'method',
- 'classIndex' => 1,
- ],
- 140 => [
- 'type' => 'method',
- 'classIndex' => 1,
- ],
- 164 => [
- 'type' => 'const',
- 'classIndex' => 158,
- ],
- 173 => [
- 'type' => 'trait_import',
- 'classIndex' => 158,
- ],
- ],
- <<<'PHP'
- <?php
- class Foo
- {
- public $prop0;
- protected $prop1;
- private $prop2 = 1;
- var $prop3 = array(1,2,3);
- const CONSTANT = 'constant value';
- public function bar4()
- {
- $a = 5;
- return " ({$a})";
- }
- public function bar5($data)
- {
- $message = $data;
- $example = function ($arg) use ($message) {
- echo $arg . ' ' . $message;
- };
- $example('hello');
- }function A(){}
- }
- function test(){}
- class Foo2
- {
- const CONSTANT = 'constant value';
- use Foo\Bar; // expected in the return value
- }
- PHP
- ,
- ];
- }
- /**
- * @requires PHP 7.4
- */
- public function testGetClassyElementsWithNullableProperties(): void
- {
- $source = <<<'PHP'
- <?php
- class Foo
- {
- public int $prop0;
- protected ?array $prop1;
- private string $prop2 = 1;
- var ? Foo\Bar $prop3 = array(1,2,3);
- }
- PHP;
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $elements = $tokensAnalyzer->getClassyElements();
- static::assertSame(
- [
- 11 => [
- 'token' => $tokens[11],
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 19 => [
- 'token' => $tokens[19],
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 26 => [
- 'token' => $tokens[26],
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- 41 => [
- 'token' => $tokens[41],
- 'type' => 'property',
- 'classIndex' => 1,
- ],
- ],
- $elements
- );
- }
- public function testGetClassyElementsWithAnonymousClass(): void
- {
- $source = <<<'PHP'
- <?php
- class A {
- public $A;
- private function B()
- {
- return new class(){
- protected $level1;
- private function XYZ() {
- return new class(){private $level2 = 1;};
- }
- };
- }
- private function C() {
- }
- }
- function B() {} // do not count this
- PHP;
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $elements = $tokensAnalyzer->getClassyElements();
- static::assertSame(
- [
- 9 => [
- 'token' => $tokens[9],
- 'type' => 'property', // $A
- 'classIndex' => 1,
- ],
- 14 => [
- 'token' => $tokens[14],
- 'type' => 'method', // B
- 'classIndex' => 1,
- ],
- 33 => [
- 'token' => $tokens[33],
- 'type' => 'property', // $level1
- 'classIndex' => 26,
- ],
- 38 => [
- 'token' => $tokens[38],
- 'type' => 'method', // XYZ
- 'classIndex' => 26,
- ],
- 56 => [
- 'token' => $tokens[56],
- 'type' => 'property', // $level2
- 'classIndex' => 50,
- ],
- 74 => [
- 'token' => $tokens[74],
- 'type' => 'method', // C
- 'classIndex' => 1,
- ],
- ],
- $elements
- );
- }
- public function testGetClassyElementsWithMultipleAnonymousClass(): void
- {
- $source = <<<'PHP'
- <?php class A0
- {
- public function AA0()
- {
- return new class
- {
- public function BB0()
- {
- }
- };
- }
- public function otherFunction0()
- {
- }
- }
- class A1
- {
- public function AA1()
- {
- return new class
- {
- public function BB1()
- {
- return new class
- {
- public function CC1()
- {
- return new class
- {
- public function DD1()
- {
- return new class{};
- }
- public function DD2()
- {
- return new class{};
- }
- };
- }
- };
- }
- public function BB2()
- {
- return new class
- {
- public function CC2()
- {
- return new class
- {
- public function DD2()
- {
- return new class{};
- }
- };
- }
- };
- }
- };
- }
- public function otherFunction1()
- {
- }
- }
- PHP;
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $elements = $tokensAnalyzer->getClassyElements();
- static::assertSame(
- [
- 9 => [
- 'token' => $tokens[9],
- 'type' => 'method',
- 'classIndex' => 1,
- ],
- 27 => [
- 'token' => $tokens[27],
- 'type' => 'method',
- 'classIndex' => 21,
- ],
- 44 => [
- 'token' => $tokens[44],
- 'type' => 'method',
- 'classIndex' => 1,
- ],
- 64 => [
- 'token' => $tokens[64],
- 'type' => 'method',
- 'classIndex' => 56,
- ],
- 82 => [
- 'token' => $tokens[82],
- 'type' => 'method',
- 'classIndex' => 76,
- ],
- 100 => [
- 'token' => $tokens[100],
- 'type' => 'method',
- 'classIndex' => 94,
- ],
- 118 => [
- 'token' => $tokens[118],
- 'type' => 'method',
- 'classIndex' => 112,
- ],
- 139 => [
- 'token' => $tokens[139],
- 'type' => 'method',
- 'classIndex' => 112,
- ],
- 170 => [
- 'token' => $tokens[170],
- 'type' => 'method',
- 'classIndex' => 76,
- ],
- 188 => [
- 'token' => $tokens[188],
- 'type' => 'method',
- 'classIndex' => 182,
- ],
- 206 => [
- 'token' => $tokens[206],
- 'type' => 'method',
- 'classIndex' => 200,
- ],
- 242 => [
- 'token' => $tokens[242],
- 'type' => 'method',
- 'classIndex' => 56,
- ],
- ],
- $elements
- );
- }
- /**
- * @requires PHP 7.4
- */
- public function testGetClassyElements74(): void
- {
- $source = <<<'PHP'
- <?php
- class Foo
- {
- public int $bar = 3;
- protected ?string $baz;
- private ?string $bazNull = null;
- public static iterable $staticProp;
- public float $x, $y;
- var bool $flag1;
- var ?bool $flag2;
- }
- PHP;
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $elements = $tokensAnalyzer->getClassyElements();
- $expected = [];
- foreach ([11, 23, 31, 44, 51, 54, 61, 69] as $index) {
- $expected[$index] = [
- 'token' => $tokens[$index],
- 'type' => 'property',
- 'classIndex' => 1,
- ];
- }
- static::assertSame($expected, $elements);
- }
- /**
- * @dataProvider provideIsAnonymousClassCases
- */
- public function testIsAnonymousClass(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $expectedValue) {
- static::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
- }
- }
- public function provideIsAnonymousClassCases()
- {
- return [
- [
- '<?php class foo {}',
- [1 => false],
- ],
- [
- '<?php $foo = new class() {};',
- [7 => true],
- ],
- [
- '<?php $foo = new class() extends Foo implements Bar, Baz {};',
- [7 => true],
- ],
- [
- '<?php class Foo { function bar() { return new class() {}; } }',
- [1 => false, 19 => true],
- ],
- [
- '<?php $a = new class(new class($d->a) implements B{}) extends C{};',
- [7 => true, 11 => true],
- ],
- [
- '<?php interface foo {}',
- [1 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsLambdaCases
- */
- public function testIsLambda(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isLambda) {
- static::assertSame($isLambda, $tokensAnalyzer->isLambda($index));
- }
- }
- public function provideIsLambdaCases()
- {
- return [
- [
- '<?php function foo () {};',
- [1 => false],
- ],
- [
- '<?php function /** foo */ foo () {};',
- [1 => false],
- ],
- [
- '<?php $foo = function () {};',
- [5 => true],
- ],
- [
- '<?php $foo = function /** foo */ () {};',
- [5 => true],
- ],
- [
- '<?php
- preg_replace_callback(
- "/(^|[a-z])/",
- function (array $matches) {
- return "a";
- },
- $string
- );',
- [7 => true],
- ],
- [
- '<?php $foo = function &() {};',
- [5 => true],
- ],
- ];
- }
- /**
- * @dataProvider provideIsLambda70Cases
- * @requires PHP 7.0
- */
- public function testIsLambda70(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $expectedValue) {
- static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
- }
- }
- public function provideIsLambda70Cases()
- {
- return [
- [
- '<?php
- $a = function (): array {
- return [];
- };',
- [6 => true],
- ],
- [
- '<?php
- function foo (): array {
- return [];
- };',
- [2 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsLambda74Cases
- * @requires PHP 7.4
- */
- public function testIsLambda74(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $expectedValue) {
- static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
- }
- }
- public function provideIsLambda74Cases()
- {
- return [
- [
- '<?php $fn = fn() => [];',
- [5 => true],
- ],
- [
- '<?php $fn = fn () => [];',
- [5 => true],
- ],
- ];
- }
- /**
- * @dataProvider provideIsLambda71Cases
- * @requires PHP 7.1
- */
- public function testIsLambda71(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $expectedValue) {
- static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
- }
- }
- public function provideIsLambda71Cases()
- {
- return [
- [
- '<?php
- $a = function (): void {
- return [];
- };',
- [6 => true],
- ],
- [
- '<?php
- function foo (): void {
- return [];
- };',
- [2 => false],
- ],
- [
- '<?php
- $a = function (): ?int {
- return [];
- };',
- [6 => true],
- ],
- [
- '<?php
- $a = function (): int {
- return [];
- };',
- [6 => true],
- ],
- [
- '<?php
- function foo (): ?int {
- return [];
- };',
- [2 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsLambda80Cases
- * @requires PHP 8.0
- */
- public function testIsLambda80(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $expectedValue) {
- static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
- }
- }
- public function provideIsLambda80Cases()
- {
- return [
- [
- '<?php
- $a = function (): ?static {
- return [];
- };',
- [6 => true],
- ],
- [
- '<?php
- $a = function (): static {
- return [];
- };',
- [6 => true],
- ],
- [
- '<?php
- $c = 4; //
- $a = function(
- $a,
- $b,
- ) use (
- $c,
- ) {
- echo $a + $b + $c;
- };
- $a(1,2);',
- [14 => true],
- ],
- ];
- }
- public function testIsLambdaInvalid(): void
- {
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage('No T_FUNCTION or T_FN at given index 0, got "T_OPEN_TAG".');
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
- $tokensAnalyzer->isLambda(0);
- }
- /**
- * @dataProvider provideIsConstantInvocationCases
- */
- public function testIsConstantInvocation(string $source, array $expected): void
- {
- $this->doIsConstantInvocationTest($source, $expected);
- }
- public function provideIsConstantInvocationCases()
- {
- return [
- [
- '<?php echo FOO;',
- [3 => true],
- ],
- [
- '<?php echo \FOO;',
- [4 => true],
- ],
- [
- '<?php echo Foo\Bar\BAR;',
- [3 => false, 5 => false, 7 => true],
- ],
- [
- '<?php echo FOO ? BAR : BAZ;',
- [3 => true, 7 => true, 11 => true],
- ],
- [
- '<?php echo FOO & BAR | BAZ;',
- [3 => true, 7 => true, 11 => true],
- ],
- [
- '<?php echo FOO & $bar;',
- [3 => true],
- ],
- [
- '<?php echo $foo[BAR];',
- [5 => true],
- ],
- [
- '<?php echo FOO[BAR];',
- [3 => true, 5 => true],
- ],
- [
- '<?php func(FOO, Bar\BAZ);',
- [1 => false, 3 => true, 6 => false, 8 => true],
- ],
- [
- '<?php if (FOO && BAR) {}',
- [4 => true, 8 => true],
- ],
- [
- '<?php return FOO * X\Y\BAR;',
- [3 => true, 7 => false, 9 => false, 11 => true],
- ],
- [
- '<?php function x() { yield FOO; yield FOO => BAR; }',
- [3 => false, 11 => true, 16 => true, 20 => true],
- ],
- [
- '<?php switch ($a) { case FOO: break; }',
- [11 => true],
- ],
- [
- '<?php namespace FOO;',
- [3 => false],
- ],
- [
- '<?php use FOO;',
- [3 => false],
- ],
- [
- '<?php use function FOO\BAR\BAZ;',
- [5 => false, 7 => false, 9 => false],
- ],
- [
- '<?php namespace X; const FOO = 1;',
- [3 => false, 8 => false],
- ],
- [
- '<?php class FOO {}',
- [3 => false],
- ],
- [
- '<?php interface FOO {}',
- [3 => false],
- ],
- [
- '<?php trait FOO {}',
- [3 => false],
- ],
- [
- '<?php class x extends FOO {}',
- [3 => false, 7 => false],
- ],
- [
- '<?php class x implements FOO {}',
- [3 => false, 7 => false],
- ],
- [
- '<?php class x implements FOO, BAR, BAZ {}',
- [3 => false, 7 => false, 10 => false, 13 => false],
- ],
- [
- '<?php class x { const FOO = 1; }',
- [3 => false, 9 => false],
- ],
- [
- '<?php class x { use FOO; }',
- [3 => false, 9 => false],
- ],
- [
- '<?php class x { use FOO, BAR { FOO::BAZ insteadof BAR; } }',
- [3 => false, 9 => false, 12 => false, 16 => false, 18 => false, 22 => false],
- ],
- [
- '<?php function x (FOO $foo, BAR &$bar, BAZ ...$baz) {}',
- [3 => false, 6 => false, 11 => false, 17 => false],
- ],
- [
- '<?php FOO();',
- [1 => false],
- ],
- [
- '<?php FOO::x();',
- [1 => false, 3 => false],
- ],
- [
- '<?php x::FOO();',
- [1 => false, 3 => false],
- ],
- [
- '<?php $foo instanceof FOO;',
- [5 => false],
- ],
- [
- '<?php try {} catch (FOO $e) {}',
- [9 => false],
- ],
- [
- '<?php "$foo[BAR]";',
- [4 => false],
- ],
- [
- '<?php "{$foo[BAR]}";',
- [5 => true],
- ],
- [
- '<?php FOO: goto FOO;',
- [1 => false, 6 => false],
- ],
- [
- '<?php foo(E_USER_DEPRECATED | E_DEPRECATED);',
- [1 => false, 3 => true, 7 => true],
- ],
- [
- '<?php interface Foo extends Bar, Baz, Qux {}',
- [3 => false, 7 => false, 10 => false, 13 => false],
- ],
- [
- '<?php use Foo\Bar, Foo\Baz, Foo\Qux;',
- [3 => false, 5 => false, 8 => false, 10 => false, 13 => false, 15 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsConstantInvocation70Cases
- * @requires PHP 7.0
- */
- public function testIsConstantInvocation70(string $source, array $expected): void
- {
- $this->doIsConstantInvocationTest($source, $expected);
- }
- public function provideIsConstantInvocation70Cases()
- {
- return [
- [
- '<?php function x(): FOO {}',
- [3 => false, 8 => false],
- ],
- [
- '<?php use X\Y\{FOO, BAR as BAR2, BAZ};',
- [3 => false, 5 => false, 8 => false, 11 => false, 15 => false, 18 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsConstantInvocation71Cases
- * @requires PHP 7.1
- */
- public function testIsConstantInvocation71(string $source, array $expected): void
- {
- $this->doIsConstantInvocationTest($source, $expected);
- }
- public function provideIsConstantInvocation71Cases()
- {
- return [
- [
- '<?php function x(?FOO $foo) {}',
- [3 => false, 6 => false],
- ],
- [
- '<?php function x(): ?FOO {}',
- [3 => false, 9 => false],
- ],
- [
- '<?php try {} catch (FOO|BAR|BAZ $e) {}',
- [9 => false, 11 => false, 13 => false],
- ],
- [
- '<?php interface Foo { public function bar(): Baz; }',
- [3 => false, 11 => false, 16 => false],
- ],
- [
- '<?php interface Foo { public function bar(): \Baz; }',
- [3 => false, 11 => false, 17 => false],
- ],
- [
- '<?php interface Foo { public function bar(): ?Baz; }',
- [3 => false, 11 => false, 17 => false],
- ],
- [
- '<?php interface Foo { public function bar(): ?\Baz; }',
- [3 => false, 11 => false, 18 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsConstantInvocationPhp80Cases
- * @requires PHP 8.0
- */
- public function testIsConstantInvocationPhp80(string $source, array $expected): void
- {
- $this->doIsConstantInvocationTest($source, $expected);
- }
- public function provideIsConstantInvocationPhp80Cases()
- {
- yield [
- '<?php $a?->b?->c;',
- [3 => false, 5 => false],
- ];
- yield [
- '<?php try {} catch (Exception) {}',
- [9 => false],
- ];
- yield [
- '<?php try {} catch (\Exception) {}',
- [10 => false],
- ];
- yield [
- '<?php try {} catch (Foo | Bar) {}',
- [9 => false, 13 => false],
- ];
- yield [
- '<?php #[Foo] function foo() {}',
- [2 => false, 7 => false],
- ];
- yield [
- '<?php #[Foo()] function foo() {}',
- [2 => false, 9 => false],
- ];
- }
- public function testIsConstantInvocationInvalid(): void
- {
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage('No T_STRING at given index 0, got "T_OPEN_TAG".');
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
- $tokensAnalyzer->isConstantInvocation(0);
- }
- /**
- * @requires PHP 8.0
- */
- public function testIsConstantInvocationForNullSafeObjectOperator(): void
- {
- $tokens = Tokens::fromCode('<?php $a?->b?->c;');
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- foreach ($tokens as $index => $token) {
- if (!$token->isGivenKind(T_STRING)) {
- continue;
- }
- static::assertFalse($tokensAnalyzer->isConstantInvocation($index));
- }
- }
- /**
- * @dataProvider provideIsUnarySuccessorOperatorCases
- */
- public function testIsUnarySuccessorOperator(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isUnary) {
- static::assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
- if ($isUnary) {
- static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
- static::assertFalse($tokensAnalyzer->isBinaryOperator($index));
- }
- }
- }
- public function provideIsUnarySuccessorOperatorCases()
- {
- return [
- [
- '<?php $a++;',
- [2 => true],
- ],
- [
- '<?php $a--;',
- [2 => true],
- ],
- [
- '<?php $a ++;',
- [3 => true],
- ],
- [
- '<?php $a++ + 1;',
- [2 => true, 4 => false],
- ],
- [
- '<?php ${"a"}++;',
- [5 => true],
- ],
- [
- '<?php $foo->bar++;',
- [4 => true],
- ],
- [
- '<?php $foo->{"bar"}++;',
- [6 => true],
- ],
- 'array access' => [
- '<?php $a["foo"]++;',
- [5 => true],
- ],
- 'array curly access' => [
- '<?php $a{"foo"}++;',
- [5 => true],
- ],
- ];
- }
- /**
- * @dataProvider provideIsUnaryPredecessorOperatorCases
- */
- public function testIsUnaryPredecessorOperator(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isUnary) {
- static::assertSame($isUnary, $tokensAnalyzer->isUnaryPredecessorOperator($index));
- if ($isUnary) {
- static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
- static::assertFalse($tokensAnalyzer->isBinaryOperator($index));
- }
- }
- }
- public function provideIsUnaryPredecessorOperatorCases()
- {
- return [
- [
- '<?php ++$a;',
- [1 => true],
- ],
- [
- '<?php --$a;',
- [1 => true],
- ],
- [
- '<?php -- $a;',
- [1 => true],
- ],
- [
- '<?php $a + ++$b;',
- [3 => false, 5 => true],
- ],
- [
- '<?php !!$a;',
- [1 => true, 2 => true],
- ],
- [
- '<?php $a = &$b;',
- [5 => true],
- ],
- [
- '<?php function &foo() {}',
- [3 => true],
- ],
- [
- '<?php @foo();',
- [1 => true],
- ],
- [
- '<?php foo(+ $a, -$b);',
- [3 => true, 8 => true],
- ],
- [
- '<?php function foo(&$a, array &$b, Bar &$c) {}',
- [5 => true, 11 => true, 17 => true],
- ],
- [
- '<?php function foo($a, ...$b) {}',
- [8 => true],
- ],
- [
- '<?php function foo(&...$b) {}',
- [5 => true, 6 => true],
- ],
- [
- '<?php function foo(array ...$b) {}',
- [7 => true],
- ],
- [
- '<?php $foo = function(...$a) {};',
- [7 => true],
- ],
- [
- '<?php $foo = function($a, ...$b) {};',
- [10 => true],
- ],
- ];
- }
- /**
- * @dataProvider provideIsBinaryOperatorCases
- */
- public function testIsBinaryOperator(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isBinary) {
- static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
- if ($isBinary) {
- static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
- static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperatorCases()
- {
- $cases = [
- [
- '<?php echo $a[1] + 1;',
- [8 => true],
- ],
- [
- '<?php echo $a{1} + 1;',
- [8 => true],
- ],
- [
- '<?php $a .= $b; ?>',
- [3 => true],
- ],
- [
- '<?php $a . \'a\' ?>',
- [3 => true],
- ],
- [
- '<?php $a &+ $b;',
- [3 => true],
- ],
- [
- '<?php $a && $b;',
- [3 => true],
- ],
- [
- '<?php $a & $b;',
- [3 => true],
- ],
- [
- '<?php [] + [];',
- [4 => true],
- ],
- [
- '<?php $a + $b;',
- [3 => true],
- ],
- [
- '<?php 1 + $b;',
- [3 => true],
- ],
- [
- '<?php 0.2 + $b;',
- [3 => true],
- ],
- [
- '<?php $a[1] + $b;',
- [6 => true],
- ],
- [
- '<?php FOO + $b;',
- [3 => true],
- ],
- [
- '<?php foo() + $b;',
- [5 => true],
- ],
- [
- '<?php ${"foo"} + $b;',
- [6 => true],
- ],
- [
- '<?php $a+$b;',
- [2 => true],
- ],
- [
- '<?php $a /* foo */ + /* bar */ $b;',
- [5 => true],
- ],
- [
- '<?php $a =
- $b;',
- [3 => true],
- ],
- [
- '<?php $a
- = $b;',
- [3 => true],
- ],
- [
- '<?php $a = array("b" => "c", );',
- [3 => true, 9 => true, 12 => false],
- ],
- [
- '<?php $a * -$b;',
- [3 => true, 5 => false],
- ],
- [
- '<?php $a = -2 / +5;',
- [3 => true, 5 => false, 8 => true, 10 => false],
- ],
- [
- '<?php $a = &$b;',
- [3 => true, 5 => false],
- ],
- [
- '<?php $a++ + $b;',
- [2 => false, 4 => true],
- ],
- [
- '<?php $a = FOO & $bar;',
- [7 => true],
- ],
- [
- '<?php __LINE__ - 1;',
- [3 => true],
- ],
- [
- '<?php `echo 1` + 1;',
- [5 => true],
- ],
- [
- '<?php $a ** $b;',
- [3 => true],
- ],
- [
- '<?php $a **= $b;',
- [3 => true],
- ],
- ];
- $operators = [
- '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
- '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
- ];
- foreach ($operators as $operator) {
- $cases[] = [
- '<?php $a '.$operator.' $b;',
- [3 => true],
- ];
- }
- return $cases;
- }
- /**
- * @dataProvider provideIsBinaryOperator70Cases
- * @requires PHP 7.0
- */
- public function testIsBinaryOperator70(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isBinary) {
- static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
- if ($isBinary) {
- static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
- static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperator70Cases()
- {
- return [
- [
- '<?php $a <=> $b;',
- [3 => true],
- ],
- [
- '<?php $a ?? $b;',
- [3 => true],
- ],
- ];
- }
- /**
- * @dataProvider provideIsArrayCases
- */
- public function testIsArray(string $source, int $tokenIndex, bool $isMultiLineArray = false): void
- {
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
- static::assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
- }
- public function provideIsArrayCases()
- {
- return [
- [
- '<?php
- array("a" => 1);
- ',
- 2,
- ],
- [
- '<?php
- ["a" => 2];
- ',
- 2, false,
- ],
- [
- '<?php
- array(
- "a" => 3
- );
- ',
- 2, true,
- ],
- [
- '<?php
- [
- "a" => 4
- ];
- ',
- 2, true,
- ],
- [
- '<?php
- array(
- "a" => array(5, 6, 7),
- 8 => new \Exception(\'Ellow\')
- );
- ',
- 2, true,
- ],
- [
- // mix short array syntax
- '<?php
- array(
- "a" => [9, 10, 11],
- 12 => new \Exception(\'Ellow\')
- );
- ',
- 2, true,
- ],
- // Windows/Max EOL testing
- [
- "<?php\r\narray('a' => 13);\r\n",
- 1,
- ],
- [
- "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
- 2, true,
- ],
- ];
- }
- /**
- * @param int[] $tokenIndexes
- *
- * @dataProvider provideIsArray71Cases
- * @requires PHP 7.1
- */
- public function testIsArray71(string $source, array $tokenIndexes): void
- {
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- foreach ($tokens as $index => $token) {
- $expect = \in_array($index, $tokenIndexes, true);
- static::assertSame(
- $expect,
- $tokensAnalyzer->isArray($index),
- sprintf('Expected %sarray, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
- );
- }
- }
- public function provideIsArray71Cases()
- {
- return [
- [
- '<?php
- [$a] = $z;
- ["a" => $a, "b" => $b] = $array;
- $c = [$d, $e] = $array[$a];
- [[$a, $b], [$c, $d]] = $d;
- $array = []; $d = array();
- ',
- [76, 84],
- ],
- ];
- }
- /**
- * @dataProvider provideIsBinaryOperator71Cases
- * @requires PHP 7.1
- */
- public function testIsBinaryOperator71(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isBinary) {
- static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
- if ($isBinary) {
- static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
- static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperator71Cases()
- {
- return [
- [
- '<?php try {} catch (A | B $e) {}',
- [11 => false],
- ],
- ];
- }
- /**
- * @dataProvider provideIsBinaryOperator74Cases
- * @requires PHP 7.4
- */
- public function testIsBinaryOperator74(string $source, array $expected): void
- {
- $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
- foreach ($expected as $index => $isBinary) {
- static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
- if ($isBinary) {
- static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
- static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
- }
- }
- }
- public function provideIsBinaryOperator74Cases()
- {
- return [
- [
- '<?php $a ??= $b;',
- [3 => true],
- ],
- ];
- }
- /**
- * @dataProvider provideArrayExceptionsCases
- */
- public function testIsNotArray(string $source, int $tokenIndex): void
- {
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertFalse($tokensAnalyzer->isArray($tokenIndex));
- }
- /**
- * @dataProvider provideArrayExceptionsCases
- */
- public function testIsMultiLineArrayException(string $source, int $tokenIndex): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $tokensAnalyzer->isArrayMultiLine($tokenIndex);
- }
- public function provideArrayExceptionsCases()
- {
- return [
- ['<?php $a;', 1],
- ["<?php\n \$a = (0+1); // [0,1]", 4],
- ['<?php $text = "foo $bbb[0] bar";', 8],
- ['<?php $text = "foo ${aaa[123]} bar";', 9],
- ];
- }
- public function testIsBlockMultilineException(): void
- {
- $this->expectException(\LogicException::class);
- $tokens = Tokens::fromCode('<?php foo(1, 2, 3);');
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $tokensAnalyzer->isBlockMultiline($tokens, 1);
- }
- /**
- * @param bool $isBlockMultiline
- * @param string $source
- * @param int $tokenIndex
- *
- * @dataProvider provideIsBlockMultilineCases
- */
- public function testIsBlockMultiline($isBlockMultiline, $source, $tokenIndex): void
- {
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertSame($isBlockMultiline, $tokensAnalyzer->isBlockMultiline($tokens, $tokenIndex));
- }
- public static function provideIsBlockMultilineCases()
- {
- yield [
- false,
- '<?php foo(1, 2, 3);',
- 2,
- ];
- yield [
- true,
- '<?php foo(1,
- 2,
- 3
- );',
- 2,
- ];
- yield [
- false,
- '<?php foo(1, "Multi
- string", 2, 3);',
- 2,
- ];
- yield [
- false,
- '<?php foo(1, havingNestedBlockThatIsMultilineDoesNotMakeTheMainBlockMultiline(
- "a",
- "b"
- ), 2, 3);',
- 2,
- ];
- }
- /**
- * @dataProvider provideGetFunctionPropertiesCases
- */
- public function testGetFunctionProperties(string $source, int $index, array $expected): void
- {
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $attributes = $tokensAnalyzer->getMethodAttributes($index);
- static::assertSame($expected, $attributes);
- }
- public function provideGetFunctionPropertiesCases()
- {
- $defaultAttributes = [
- 'visibility' => null,
- 'static' => false,
- 'abstract' => false,
- 'final' => false,
- ];
- $template = '
- <?php
- class TestClass {
- %s function a() {
- //
- }
- }
- ';
- $cases = [];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = T_PRIVATE;
- $cases[] = [sprintf($template, 'private'), 10, $attributes];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = T_PUBLIC;
- $cases[] = [sprintf($template, 'public'), 10, $attributes];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = T_PROTECTED;
- $cases[] = [sprintf($template, 'protected'), 10, $attributes];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = null;
- $attributes['static'] = true;
- $cases[] = [sprintf($template, 'static'), 10, $attributes];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = T_PUBLIC;
- $attributes['static'] = true;
- $attributes['final'] = true;
- $cases[] = [sprintf($template, 'final public static'), 14, $attributes];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = null;
- $attributes['abstract'] = true;
- $cases[] = [sprintf($template, 'abstract'), 10, $attributes];
- $attributes = $defaultAttributes;
- $attributes['visibility'] = T_PUBLIC;
- $attributes['abstract'] = true;
- $cases[] = [sprintf($template, 'abstract public'), 12, $attributes];
- $attributes = $defaultAttributes;
- $cases[] = [sprintf($template, ''), 8, $attributes];
- return $cases;
- }
- public function testIsWhilePartOfDoWhile(): void
- {
- $source =
- <<<'SRC'
- <?php
- // `not do`
- while(false) {
- }
- while (false);
- while (false)?>
- <?php
- if(false){
- }while(false);
- if(false){
- }while(false)?><?php
- while(false){}while(false){}
- while ($i <= 10):
- echo $i;
- $i++;
- endwhile;
- ?>
- <?php while(false): ?>
- <?php endwhile ?>
- <?php
- // `do`
- do{
- } while(false);
- do{
- } while(false)?>
- <?php
- if (false){}do{}while(false);
- // `not do`, `do`
- if(false){}while(false){}do{}while(false);
- SRC;
- $expected = [
- 3 => false,
- 12 => false,
- 19 => false,
- 34 => false,
- 47 => false,
- 53 => false,
- 59 => false,
- 66 => false,
- 91 => false,
- 112 => true,
- 123 => true,
- 139 => true,
- 153 => false,
- 162 => true,
- ];
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- foreach ($tokens as $index => $token) {
- if (!$token->isGivenKind(T_WHILE)) {
- continue;
- }
- static::assertSame(
- $expected[$index],
- $tokensAnalyzer->isWhilePartOfDoWhile($index),
- sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $expected[$index] ? '' : 'not ')
- );
- }
- }
- /**
- * @dataProvider provideGetImportUseIndexesCases
- */
- public function testGetImportUseIndexes(array $expected, string $input, bool $perNamespace = false): void
- {
- $tokens = Tokens::fromCode($input);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
- }
- public function provideGetImportUseIndexesCases()
- {
- return [
- [
- [1, 8],
- '<?php use E\F?><?php use A\B;',
- ],
- [
- [[1], [14], [29]],
- '<?php
- use T\A;
- namespace A { use D\C; }
- namespace b { use D\C; }
- ',
- true,
- ],
- [
- [[1, 8]],
- '<?php use D\B; use A\C?>',
- true,
- ],
- [
- [1, 8],
- '<?php use D\B; use A\C?>',
- ],
- [
- [7, 22],
- '<?php
- namespace A { use D\C; }
- namespace b { use D\C; }
- ',
- ],
- [
- [3, 10, 34, 45, 54, 59, 77, 95],
- <<<'EOF'
- use Zoo\Bar;
- use Foo\Bar;
- use Foo\Zar\Baz;
- <?php
- use Foo\Bar;
- use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
- use Foo\Bir as FBB;
- use Foo\Zar\Baz;
- use SomeClass;
- use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
- use Zoo\Bar;
- $a = new someclass();
- use Zoo\Tar;
- class AnnotatedClass
- {
- }
- EOF
- ,
- ],
- ];
- }
- /**
- * @dataProvider provideGetImportUseIndexesPHP70Cases
- * @requires PHP 7.0
- */
- public function testGetImportUseIndexesPHP70(array $expected, string $input, bool $perNamespace = false): void
- {
- $tokens = Tokens::fromCode($input);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
- }
- public function provideGetImportUseIndexesPHP70Cases()
- {
- return [
- [
- [1, 22, 41],
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C};
- use function some\a\{fn_a, fn_b, fn_c};
- use const some\a\{ConstA, ConstB, ConstC};
- ',
- ],
- [
- [[1, 22, 41]],
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C};
- use function some\a\{fn_a, fn_b, fn_c};
- use const some\a\{ConstA, ConstB, ConstC};
- ',
- true,
- ],
- ];
- }
- /**
- * @dataProvider provideGetImportUseIndexesPHP72Cases
- * @requires PHP 7.2
- */
- public function testGetImportUseIndexesPHP72(array $expected, string $input, bool $perNamespace = false): void
- {
- $tokens = Tokens::fromCode($input);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
- }
- public function provideGetImportUseIndexesPHP72Cases()
- {
- return [
- [
- [1, 23, 43],
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C,};
- use function some\a\{fn_a, fn_b, fn_c,};
- use const some\a\{ConstA, ConstB, ConstC,};
- ',
- ],
- [
- [[1, 23, 43]],
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C,};
- use function some\a\{fn_a, fn_b, fn_c,};
- use const some\a\{ConstA, ConstB, ConstC,};
- ',
- true,
- ],
- ];
- }
- public function testGetClassyElementsWithMultipleNestedAnonymousClass(): void
- {
- $source = '<?php
- class MyTestWithAnonymousClass extends TestCase
- {
- public function setUp()
- {
- $provider = new class(function () {}) {};
- }
- public function testSomethingWithMoney(
- Money $amount
- ) {
- $a = new class(function () {
- new class(function () {
- new class(function () {})
- {
- const A=1;
- };
- })
- {
- const B=1;
- public function foo() {
- $c = new class() {const AA=3;};
- $d = new class {const AB=3;};
- }
- };
- })
- {
- const C=1;
- };
- }
- }';
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- $elements = $tokensAnalyzer->getClassyElements();
- static::assertSame([
- 13 => [
- 'token' => $tokens[13],
- 'type' => 'method', // setUp
- 'classIndex' => 1,
- ],
- 46 => [
- 'token' => $tokens[46],
- 'type' => 'method', // testSomethingWithMoney
- 'classIndex' => 1,
- ],
- 100 => [
- 'token' => $tokens[100], // const A
- 'type' => 'const',
- 'classIndex' => 87,
- ],
- 115 => [
- 'token' => $tokens[115], // const B
- 'type' => 'const',
- 'classIndex' => 65,
- ],
- 124 => [
- 'token' => $tokens[124],
- 'type' => 'method', // foo
- 'classIndex' => 65, // $a
- ],
- 143 => [
- 'token' => $tokens[143], // const AA
- 'type' => 'const',
- 'classIndex' => 138,
- ],
- 161 => [
- 'token' => $tokens[161], // const AB
- 'type' => 'const',
- 'classIndex' => 158,
- ],
- ], $elements);
- }
- /**
- * @dataProvider provideIsSuperGlobalCases
- */
- public function testIsSuperGlobal(bool $expected, string $source, int $index): void
- {
- $tokens = Tokens::fromCode($source);
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- static::assertSame($expected, $tokensAnalyzer->isSuperGlobal($index));
- }
- public function provideIsSuperGlobalCases()
- {
- $superNames = [
- '$_COOKIE',
- '$_ENV',
- '$_FILES',
- '$_GET',
- '$_POST',
- '$_REQUEST',
- '$_SERVER',
- '$_SESSION',
- '$GLOBALS',
- ];
- $cases = [];
- foreach ($superNames as $superName) {
- $cases[] = [
- true,
- sprintf('<?php echo %s[0];', $superName),
- 3,
- ];
- }
- $notGlobalCodeCases = [
- '<?php echo 1; $a = static function($b) use ($a) { $a->$b(); }; // $_SERVER',
- '<?php class Foo{}?> <?php $_A = 1; /* $_SESSION */',
- ];
- foreach ($notGlobalCodeCases as $notGlobalCodeCase) {
- $tokensCount = \count(Tokens::fromCode($notGlobalCodeCase));
- for ($i = 0; $i < $tokensCount; ++$i) {
- $cases[] = [
- false,
- $notGlobalCodeCase,
- $i,
- ];
- }
- }
- return $cases;
- }
- private function doIsConstantInvocationTest(string $source, array $expected): void
- {
- $tokens = Tokens::fromCode($source);
- static::assertCount(
- $tokens->countTokenKind(T_STRING),
- $expected,
- 'All T_STRING tokens must be tested'
- );
- $tokensAnalyzer = new TokensAnalyzer($tokens);
- foreach ($expected as $index => $expectedValue) {
- static::assertSame(
- $expectedValue,
- $tokensAnalyzer->isConstantInvocation($index),
- sprintf('Token at index '.$index.' should match the expected value (%s).', $expectedValue ? 'true' : 'false')
- );
- }
- }
- }
|