* 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\ClassyAnalyzer; use PhpCsFixer\Tokenizer\Tokens; /** * @internal * * @covers \PhpCsFixer\Tokenizer\Analyzer\ClassyAnalyzer */ final class ClassyAnalyzerTest extends TestCase { /** * @param array $expected * * @dataProvider provideIsClassyInvocationCases */ public function testIsClassyInvocation(string $source, array $expected): void { self::assertClassyInvocation($source, $expected); } public static function provideIsClassyInvocationCases(): iterable { yield [ ' true], ]; yield [ ' true], ]; yield [ ' false, 5 => true], ]; yield [ ' true], ]; yield [ ' true, 3 => false], ]; yield [ ' true, 4 => false], ]; yield [ ' false, 3 => true, 5 => false], ]; yield [ ' true], ]; yield [ ' false, 8 => true], ]; yield [ ' false, 7 => true, 10 => false, 12 => true, 16 => true, 19 => true], ]; yield [ ' false, 9 => true, 12 => false, 14 => true, 18 => true, 21 => true, 25 => true, 32 => true], ]; yield 'with reference' => [ ' false, 5 => true, 10 => true, 17 => true, 23 => false, 25 => true], ]; yield [ ' false, 9 => false, 15 => false, 17 => false, 22 => false, 24 => false, 33 => false], ]; yield [ ' false, 7 => false], ]; yield [ ' false], ]; yield [ ' false, 7 => false], ]; yield [ ' false, 9 => true], ]; yield [ ' false, 6 => true, 12 => false, 14 => true, 22 => true], ]; yield [ ' false, 5 => false, 11 => false], ]; yield [ ' false, 6 => false, 13 => false], ]; yield [ ' false, 5 => false, 10 => false, 17 => false], ]; yield [ ' false, 8 => true], ]; foreach (['bool', 'float', 'int', 'parent', 'self', 'string', 'void'] as $returnType) { yield [ \sprintf(' false, 8 => false], ]; } yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; } /** * @param array $expected * * @dataProvider provideIsClassyInvocation80Cases * * @requires PHP 8.0 */ public function testIsClassyInvocation80(string $source, array $expected): void { self::assertClassyInvocation($source, $expected); } public static function provideIsClassyInvocation80Cases(): iterable { yield [ ' false, 9 => true, 11 => false], ]; yield [ ' false, 9 => true, 11 => true, 13 => false], ]; yield [ ' false, 8 => false, 10 => true, 12 => false], ]; yield [ ' false, 8 => false, 10 => true, 12 => false], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; } /** * @param array $expected * * @dataProvider provideIsClassyInvocation81Cases * * @requires PHP 8.1 */ public function testIsClassyInvocation81(string $source, array $expected): void { self::assertClassyInvocation($source, $expected); } public static function provideIsClassyInvocation81Cases(): iterable { yield 'never' => [ ' false, 8 => false], ]; yield 'intersection' => [ ' false, 9 => true, 11 => true], ]; } /** * @param array $expected */ private static function assertClassyInvocation(string $source, array $expected): void { $tokens = Tokens::fromCode($source); $analyzer = new ClassyAnalyzer(); foreach ($expected as $index => $isClassy) { self::assertSame($isClassy, $analyzer->isClassyInvocation($tokens, $index), \sprintf('Token at index %d should match the expected value "%s".', $index, true === $isClassy ? 'true' : 'false')); } } }