* 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\CommentsAnalyzer; use PhpCsFixer\Tokenizer\Tokens; /** * @author Kuba Werłos * * @internal * * @covers \PhpCsFixer\Tokenizer\Analyzer\CommentsAnalyzer */ final class CommentsAnalyzerTest extends TestCase { public function testWhenNotPointingToComment(): void { $analyzer = new CommentsAnalyzer(); $tokens = Tokens::fromCode('expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Given index must point to a comment.'); $analyzer->getCommentBlockIndices($tokens, 4); } /** * @param list $borders * * @dataProvider provideCommentsCases */ public function testComments(string $code, int $index, array $borders): void { $tokens = Tokens::fromCode($code); $analyzer = new CommentsAnalyzer(); self::assertSame($borders, $analyzer->getCommentBlockIndices($tokens, $index)); self::assertFalse($analyzer->isHeaderComment($tokens, $index)); } public static function provideCommentsCases(): iterable { yield 'discover all 4 comments for the 1st comment with slash' => [ ' [ ' [ ' [ ' [ str_replace("\n", "\r", ' [ ' [ ' [ 'expectException(\InvalidArgumentException::class); $analyzer->isHeaderComment($tokens, 2); } /** * @dataProvider provideHeaderCommentCases */ public function testHeaderComment(string $code, int $index): void { $tokens = Tokens::fromCode($code); $analyzer = new CommentsAnalyzer(); self::assertTrue($analyzer->isHeaderComment($tokens, $index)); } /** * @return iterable */ public static function provideHeaderCommentCases(): iterable { yield ['isHeaderComment($tokens, $index)); } /** * @return iterable */ public static function provideNotHeaderCommentCases(): iterable { yield ['expectException(\InvalidArgumentException::class); $analyzer->isBeforeStructuralElement($tokens, 2); } /** * @dataProvider providePhpdocCandidateCases */ public function testPhpdocCandidate(string $code): void { $tokens = Tokens::fromCode($code); $index = $tokens->getNextTokenOfKind(0, [[T_COMMENT], [T_DOC_COMMENT]]); $analyzer = new CommentsAnalyzer(); self::assertTrue($analyzer->isBeforeStructuralElement($tokens, $index)); } /** * @return iterable */ public static function providePhpdocCandidateCases(): iterable { yield ['doSomething();']; yield [' $x + 1;']; yield [' $x + 1;']; yield ['getNextTokenOfKind(0, [[T_COMMENT], [T_DOC_COMMENT]]); $analyzer = new CommentsAnalyzer(); self::assertFalse($analyzer->isBeforeStructuralElement($tokens, $index)); } /** * @return iterable */ public static function provideNotPhpdocCandidateCases(): iterable { yield ['testPhpdocCandidate($code); } /** * @return iterable */ public static function providePhpdocCandidatePhp80Cases(): iterable { yield 'attribute between class and phpDoc' => [ 'testPhpdocCandidate($code); } /** * @return iterable */ public static function providePhpdocCandidatePhp81Cases(): iterable { yield 'public readonly' => [ ' [ ' [ ' [ ' [ ' [ ' [ 'testNotPhpdocCandidate($code); } /** * @return iterable */ public static function provideNotPhpdocCandidatePhp81Cases(): iterable { yield 'enum and switch' => [ ' [ 'getNextTokenOfKind(0, [[T_COMMENT], [T_DOC_COMMENT]]); $analyzer = new CommentsAnalyzer(); self::assertSame($expected, $analyzer->isBeforeReturn($tokens, $index)); } /** * @return iterable */ public static function provideReturnStatementCases(): iterable { yield 'docblock before var' => [ ' [ ' [ ' [ '