* Dariusz Rumiński * * 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\ArgumentsAnalyzer; use PhpCsFixer\Tokenizer\Tokens; /** * @author Dariusz Rumiński * * @internal * * @covers \PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer */ final class ArgumentsAnalyzerTest extends TestCase { /** * @param array $arguments * * @dataProvider provideArgumentsCases */ public function testArguments(string $code, int $openIndex, int $closeIndex, array $arguments): void { $tokens = Tokens::fromCode($code); $analyzer = new ArgumentsAnalyzer(); self::assertSame(\count($arguments), $analyzer->countArguments($tokens, $openIndex, $closeIndex)); self::assertSame($arguments, $analyzer->getArguments($tokens, $openIndex, $closeIndex)); } public static function provideArgumentsCases(): iterable { yield [' 3]]; yield [' 4]]; yield [' 3, 5 => 6]]; yield [' 3, 5 => 15, 17 => 22]]; yield 'non condition (hardcoded)' => [ ' 7, 9 => 10], ]; yield [' 7, 9 => 11]]; yield [' 16]]; yield [' 10]]; yield [' 11]]; yield [' 12, 14 => 20]]; yield [' 3]]; yield [' 3]]; yield [' 14]]; yield [' 15]]; yield [' 5, 7 => 7, 9 => 9, 11 => 11, 13 => 13]]; yield [' 3, 5 => 7]]; } /** * @param array $arguments * * @requires PHP 8.0 * * @dataProvider provideArguments80Cases */ public function testArguments80(string $code, int $openIndex, int $closeIndex, array $arguments): void { $this->testArguments($code, $openIndex, $closeIndex, $arguments); } public static function provideArguments80Cases(): iterable { yield [' 22]]; yield [' 22]]; yield [' 22]]; yield [' 13, 15 => 22]]; yield [' 9, 11 => 15]]; } /** * @param array $arguments * * @requires PHP 8.1 * * @dataProvider provideArguments81Cases */ public function testArguments81(string $code, int $openIndex, int $closeIndex, array $arguments): void { $this->testArguments($code, $openIndex, $closeIndex, $arguments); } public static function provideArguments81Cases(): iterable { yield [' 12, 14 => 19]]; } /** * @dataProvider provideArgumentInfoCases */ public function testArgumentInfo(string $code, int $openIndex, int $closeIndex, ArgumentAnalysis $expected): void { $tokens = Tokens::fromCode($code); $analyzer = new ArgumentsAnalyzer(); self::assertArgumentAnalysis($expected, $analyzer->getArgumentInfo($tokens, $openIndex, $closeIndex)); } /** * @return iterable */ public static function provideArgumentInfoCases(): iterable { yield ['', 5, 6, new ArgumentAnalysis( '$b', 6, null, null )]; yield ['', 5, 6, new ArgumentAnalysis( null, null, null, null )]; yield ['testArgumentInfo($code, $openIndex, $closeIndex, $expected); } /** * @return iterable */ public static function provideArgumentInfo80Cases(): iterable { yield [ 'testArgumentInfo($code, $openIndex, $closeIndex, $expected); } /** * @return iterable */ public static function provideArgumentInfo81Cases(): iterable { yield [ 'getDefault(), $actual->getDefault(), 'Default.'); self::assertSame($expected->getName(), $actual->getName(), 'Name.'); self::assertSame($expected->getNameIndex(), $actual->getNameIndex(), 'Name index.'); self::assertSame($expected->hasDefault(), $actual->hasDefault(), 'Has default.'); self::assertSame($expected->hasTypeAnalysis(), $actual->hasTypeAnalysis(), 'Has type analysis.'); if ($expected->hasTypeAnalysis()) { $expectedTypeAnalysis = $expected->getTypeAnalysis(); $actualTypeAnalysis = $actual->getTypeAnalysis(); self::assertSame($expectedTypeAnalysis->getEndIndex(), $actualTypeAnalysis->getEndIndex(), 'Type analysis end index.'); self::assertSame($expectedTypeAnalysis->getName(), $actualTypeAnalysis->getName(), 'Type analysis name.'); self::assertSame($expectedTypeAnalysis->getStartIndex(), $actualTypeAnalysis->getStartIndex(), 'Type analysis start index.'); self::assertSame($expectedTypeAnalysis->isNullable(), $actualTypeAnalysis->isNullable(), 'Type analysis nullable.'); self::assertSame($expectedTypeAnalysis->isReservedType(), $actualTypeAnalysis->isReservedType(), 'Type analysis reserved type.'); } else { self::assertNull($actual->getTypeAnalysis()); } self::assertSame(serialize($expected), serialize($actual)); } }