123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- <?php
- /*
- * 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\Analyzer;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\Tokenizer\Analyzer\Analysis\ArgumentAnalysis;
- use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
- use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
- use PhpCsFixer\Tokenizer\Tokens;
- /**
- * @author VeeWee <toonverwerft@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer
- */
- final class FunctionsAnalyzerTest extends TestCase
- {
- /**
- * @param bool $isFunctionIndex
- * @param string $code
- * @param int $index
- *
- * @dataProvider provideIsGlobalFunctionCallCases
- */
- public function testIsGlobalFunctionCall($isFunctionIndex, $code, $index)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
- }
- public function provideIsGlobalFunctionCallCases()
- {
- yield '1' => [
- false,
- '<?php CONSTANT;',
- 1,
- ];
- yield '2' => [
- true,
- '<?php foo("bar");',
- 1,
- ];
- yield '3' => [
- false,
- '<?php \foo("bar");',
- 1,
- ];
- yield '4' => [
- true,
- '<?php \foo("bar");',
- 2,
- ];
- yield '5' => [
- false,
- '<?php foo\bar("baz");',
- 1,
- ];
- yield '6' => [
- false,
- '<?php foo\bar("baz");',
- 3,
- ];
- yield '7' => [
- false,
- '<?php foo::bar("baz");',
- 1,
- ];
- yield '8' => [
- false,
- '<?php foo::bar("baz");',
- 3,
- ];
- yield '9' => [
- false,
- '<?php $foo->bar("baz");',
- 3,
- ];
- yield '10' => [
- false,
- '<?php new bar("baz");',
- 3,
- ];
- yield '11' => [
- false,
- '<?php function foo() {}',
- 3,
- ];
- yield '12' => [
- false,
- '<?php function & foo() {}',
- 5,
- ];
- yield '13' => [
- false,
- '<?php namespace\foo("bar");',
- 3,
- ];
- yield '15' => [
- true,
- '<?php
- namespace A {
- use function A;
- }
- namespace B {
- use function D;
- A();
- }
- ',
- 30,
- ];
- yield '16' => [
- true,
- '<?php
- function A(){}
- A();
- ',
- 10,
- ];
- yield '17' => [
- true,
- '<?php
- function A(){}
- a();
- ',
- 10,
- ];
- yield '18' => [
- true,
- '<?php
- namespace {
- function A(){}
- A();
- }
- ',
- 14,
- ];
- yield '19' => [
- false,
- '<?php
- namespace Z {
- function A(){}
- A();
- }
- ',
- 16,
- ];
- yield '20' => [
- false,
- '<?php
- namespace Z;
- function A(){}
- A();
- ',
- 15,
- ];
- yield '21' => [
- true,
- '<?php
- function & A(){}
- A();
- ',
- 12,
- ];
- yield '22' => [
- true,
- '<?php
- class Foo
- {
- public function A(){}
- }
- A();
- ',
- 20,
- ];
- yield '23' => [
- true,
- '<?php
- namespace A {
- function A(){}
- }
- namespace B {
- A();
- }
- ',
- 24,
- ];
- yield '24' => [
- false,
- '<?php
- use function X\a;
- A();
- ',
- 11,
- ];
- yield '25' => [
- true,
- '<?php
- use A;
- A();
- ',
- 7,
- ];
- yield '26' => [
- true,
- '<?php
- use const A;
- A();
- ',
- 9,
- ];
- yield '27' => [
- true,
- '<?php
- use function A;
- str_repeat($a, $b);
- ',
- 9,
- ];
- yield '28' => [
- true,
- '<?php
- namespace {
- function A(){}
- A();
- $b = function(){};
- }
- ',
- 14,
- ];
- foreach ([1, 6, 11, 16, 21, 26] as $index) {
- yield [
- true,
- '<?php implode($a);implode($a);implode($a);implode($a);implode($a);implode($a);',
- $index,
- ];
- }
- if (\PHP_VERSION_ID < 80000) {
- yield '14' => [
- true,
- '<?php
- use function \ str_repeat;
- str_repeat($a, $b);
- ',
- 11,
- ];
- }
- }
- /**
- * @param bool $isFunctionIndex
- * @param string $code
- * @param int $index
- *
- * @dataProvider provideIsGlobalFunctionCallPhp70Cases
- * @requires PHP 7.0
- */
- public function testIsGlobalFunctionCallPhp70($isFunctionIndex, $code, $index)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
- }
- public function provideIsGlobalFunctionCallPhp70Cases()
- {
- yield [
- true,
- '<?php
- $z = new class(
- new class(){ private function A(){} }
- ){
- public function A() {}
- };
- A();
- ',
- 46,
- ];
- }
- /**
- * @param bool $isFunctionIndex
- * @param string $code
- * @param int $index
- *
- * @dataProvider provideIsGlobalFunctionCallPhp74Cases
- * @requires PHP 7.4
- */
- public function testIsGlobalFunctionCallPhp74($isFunctionIndex, $code, $index)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
- }
- public function provideIsGlobalFunctionCallPhp74Cases()
- {
- yield [
- false,
- '<?php $foo = fn() => false;',
- 5,
- ];
- }
- /**
- * @param bool $isFunctionIndex
- * @param string $code
- * @param int $index
- *
- * @dataProvider provideIsGlobalFunctionCallPhp80Cases
- * @requires PHP 8.0
- */
- public function testIsGlobalFunctionCallPhp80($isFunctionIndex, $code, $index)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
- }
- public function provideIsGlobalFunctionCallPhp80Cases()
- {
- yield [
- true,
- '<?php $a = new (foo());',
- 8,
- ];
- yield [
- true,
- '<?php $b = $foo instanceof (foo());',
- 10,
- ];
- yield [
- false,
- '<?php
- #[\Attribute(\Attribute::TARGET_CLASS)]
- class Foo {}
- ',
- 3,
- ];
- }
- /**
- * @param string $code
- * @param int $methodIndex
- * @param array $expected
- *
- * @dataProvider provideFunctionsWithArgumentsCases
- */
- public function testFunctionArgumentInfo($code, $methodIndex, $expected)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
- }
- /**
- * @param string $code
- * @param int $methodIndex
- * @param array $expected
- *
- * @dataProvider provideFunctionsWithReturnTypeCases
- */
- public function testFunctionReturnTypeInfo($code, $methodIndex, $expected)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- $actual = $analyzer->getFunctionReturnType($tokens, $methodIndex);
- static::assertSame(serialize($expected), serialize($actual));
- }
- /**
- * @param string $code
- * @param int $methodIndex
- * @param array $expected
- *
- * @dataProvider provideFunctionsWithReturnTypePhp70Cases
- * @requires PHP 7.0
- */
- public function testFunctionReturnTypeInfoPhp70($code, $methodIndex, $expected)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- $actual = $analyzer->getFunctionReturnType($tokens, $methodIndex);
- static::assertSame(serialize($expected), serialize($actual));
- }
- public function provideFunctionsWithArgumentsCases()
- {
- $tests = [
- ['<?php function(){};', 1, []],
- ['<?php function($a){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- ]],
- ['<?php function($a, $b){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- '$b' => new ArgumentAnalysis(
- '$b',
- 6,
- null,
- null
- ),
- ]],
- ['<?php function($a, $b = array(1,2), $c = 3){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- '$b' => new ArgumentAnalysis(
- '$b',
- 6,
- 'array(1,2)',
- null
- ),
- '$c' => new ArgumentAnalysis(
- '$c',
- 18,
- '3',
- null
- ),
- ]],
- ['<?php function(array $a = array()){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 5,
- 'array()',
- new TypeAnalysis(
- 'array',
- 3,
- 3
- )
- ),
- ]],
- ['<?php function(array ... $a){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 7,
- null,
- new TypeAnalysis(
- 'array',
- 3,
- 3
- )
- ),
- ]],
- ['<?php function(\Foo\Bar $a){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 8,
- null,
- new TypeAnalysis(
- '\Foo\Bar',
- 3,
- 6
- )
- ),
- ]],
- ];
- foreach ($tests as $index => $test) {
- yield $index => $test;
- }
- if (\PHP_VERSION_ID < 80000) {
- yield ['<?php function(\Foo/** TODO: change to something else */\Bar $a){};', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 9,
- null,
- new TypeAnalysis(
- '\Foo\Bar',
- 3,
- 7
- )
- ),
- ]];
- }
- }
- public function provideFunctionsWithReturnTypeCases()
- {
- yield ['<?php function(){};', 1, null];
- }
- public function provideFunctionsWithReturnTypePhp70Cases()
- {
- yield ['<?php function($a): array {};', 1, new TypeAnalysis('array', 7, 7)];
- yield ['<?php function($a): \Foo\Bar {};', 1, new TypeAnalysis('\Foo\Bar', 7, 10)];
- yield ['<?php function($a): /* not sure if really an array */array {};', 1, new TypeAnalysis('array', 8, 8)];
- if (\PHP_VERSION_ID < 80000) {
- yield ['<?php function($a): \Foo/** TODO: change to something else */\Bar {};', 1, new TypeAnalysis('\Foo\Bar', 7, 11)];
- }
- }
- /**
- * @param string $code
- * @param int $methodIndex
- * @param array $expected
- *
- * @dataProvider provideFunctionsWithArgumentsPhp74Cases
- * @requires PHP 7.4
- */
- public function testFunctionArgumentInfoPhp74($code, $methodIndex, $expected)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
- }
- public function provideFunctionsWithArgumentsPhp74Cases()
- {
- $tests = [
- ['<?php fn() => null;', 1, []],
- ['<?php fn($a) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- ]],
- ['<?php fn($a, $b) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- '$b' => new ArgumentAnalysis(
- '$b',
- 6,
- null,
- null
- ),
- ]],
- ['<?php fn($a, $b = array(1,2), $c = 3) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- '$b' => new ArgumentAnalysis(
- '$b',
- 6,
- 'array(1,2)',
- null
- ),
- '$c' => new ArgumentAnalysis(
- '$c',
- 18,
- '3',
- null
- ),
- ]],
- ['<?php fn(array $a = array()) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 5,
- 'array()',
- new TypeAnalysis(
- 'array',
- 3,
- 3
- )
- ),
- ]],
- ['<?php fn(array ... $a) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 7,
- null,
- new TypeAnalysis(
- 'array',
- 3,
- 3
- )
- ),
- ]],
- ['<?php fn(\Foo\Bar $a) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 8,
- null,
- new TypeAnalysis(
- '\Foo\Bar',
- 3,
- 6
- )
- ),
- ]],
- ];
- foreach ($tests as $index => $test) {
- yield $index => $test;
- }
- if (\PHP_VERSION_ID < 80000) {
- yield ['<?php fn(\Foo/** TODO: change to something else */\Bar $a) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 9,
- null,
- new TypeAnalysis(
- '\Foo\Bar',
- 3,
- 7
- )
- ),
- ]];
- }
- }
- /**
- * @param string $code
- * @param int $methodIndex
- * @param array $expected
- *
- * @dataProvider provideFunctionsWithReturnTypePhp74Cases
- * @requires PHP 7.4
- */
- public function testFunctionReturnTypeInfoPhp74($code, $methodIndex, $expected)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- $actual = $analyzer->getFunctionReturnType($tokens, $methodIndex);
- static::assertSame(serialize($expected), serialize($actual));
- }
- public function provideFunctionsWithReturnTypePhp74Cases()
- {
- yield ['<?php fn() => null;', 1, null];
- yield ['<?php fn(array $a) => null;', 1, null];
- yield ['<?php fn($a): array => null;', 1, new TypeAnalysis('array', 7, 7)];
- yield ['<?php fn($a): \Foo\Bar => null;', 1, new TypeAnalysis('\Foo\Bar', 7, 10)];
- yield ['<?php fn($a): /* not sure if really an array */array => null;', 1, new TypeAnalysis('array', 8, 8)];
- if (\PHP_VERSION_ID < 80000) {
- yield ['<?php fn($a): \Foo/** TODO: change to something else */\Bar => null;', 1, new TypeAnalysis('\Foo\Bar', 7, 11)];
- }
- }
- /**
- * @param bool $isTheSameClassCall
- * @param string $code
- * @param int $index
- *
- * @dataProvider provideIsTheSameClassCallCases
- */
- public function testIsTheSameClassCall($isTheSameClassCall, $code, $index)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame($isTheSameClassCall, $analyzer->isTheSameClassCall($tokens, $index));
- }
- public function provideIsTheSameClassCallCases()
- {
- $template = '<?php
- class Foo {
- public function methodOne() {
- $x = %sotherMethod(1, 2, 3);
- }
- }
- ';
- yield [
- false,
- sprintf($template, '$this->'),
- -1,
- ];
- // 24 is index of "otherMethod" token
- for ($i = 0; $i < 40; ++$i) {
- yield [
- 24 === $i,
- sprintf($template, '$this->'),
- $i,
- ];
- yield [
- 24 === $i,
- sprintf($template, 'self::'),
- $i,
- ];
- yield [
- 24 === $i,
- sprintf($template, 'static::'),
- $i,
- ];
- }
- yield [
- true,
- sprintf($template, '$THIS->'),
- 24,
- ];
- yield [
- false,
- sprintf($template, '$notThis->'),
- 24,
- ];
- yield [
- false,
- sprintf($template, 'Bar::'),
- 24,
- ];
- }
- /**
- * @param string $code
- * @param int $methodIndex
- * @param array $expected
- *
- * @dataProvider provideFunctionsWithArgumentsPhp80Cases
- * @requires PHP 8.0
- */
- public function testFunctionArgumentInfoPhp80($code, $methodIndex, $expected)
- {
- $tokens = Tokens::fromCode($code);
- $analyzer = new FunctionsAnalyzer();
- static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
- }
- public function provideFunctionsWithArgumentsPhp80Cases()
- {
- yield ['<?php function($aa,){};', 1, [
- '$aa' => new ArgumentAnalysis(
- '$aa',
- 3,
- null,
- null
- ),
- ]];
- yield ['<?php fn($a, $bc ,) => null;', 1, [
- '$a' => new ArgumentAnalysis(
- '$a',
- 3,
- null,
- null
- ),
- '$bc' => new ArgumentAnalysis(
- '$bc',
- 6,
- null,
- null
- ),
- ]];
- }
- }
|