77use PhpCsFixer \FixerDefinition \CodeSample ;
88use PhpCsFixer \FixerDefinition \FixerDefinition ;
99use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
10+ use PhpCsFixer \Tokenizer \CT ;
1011use PhpCsFixer \Tokenizer \Token ;
1112use PhpCsFixer \Tokenizer \Tokens ;
1213use SplFileInfo ;
@@ -47,8 +48,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
4748 }
4849
4950 // ignore class if it's a Doctrine Entity
50- $ docToken = $ tokens [$ tokens ->getPrevNonWhitespace ($ classIndex )];
51- if ($ docToken ->isGivenKind (\T_DOC_COMMENT ) && false !== \mb_strpos ($ docToken ->getContent (), '@ORM\Entity ' )) {
51+ if (self ::isDoctrineEntity ($ tokens , $ classIndex )) {
5252 continue ;
5353 }
5454
@@ -61,4 +61,31 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
6161 );
6262 }
6363 }
64+
65+ private static function isDoctrineEntity (Tokens $ tokens , int $ classIndex ): bool
66+ {
67+ $ docToken = $ tokens [$ tokens ->getPrevNonWhitespace ($ classIndex )];
68+ if ($ docToken ->isGivenKind (\T_DOC_COMMENT ) && false !== \mb_strpos ($ docToken ->getContent (), '@ORM\Entity ' )) {
69+ return true ;
70+ }
71+
72+ while ($ classIndex > 0 && $ tokens [$ tokens ->getPrevNonWhitespace ($ classIndex )]->isGivenKind (CT ::T_ATTRIBUTE_CLOSE )) {
73+ $ attributeOpenIndex = $ tokens ->getPrevTokenOfKind ($ classIndex , [[\T_ATTRIBUTE ]]);
74+ \assert (null !== $ attributeOpenIndex );
75+ $ content = '' ;
76+ for ($ index = $ attributeOpenIndex ; $ index < $ classIndex ; ++$ index ) {
77+ $ content .= $ tokens [$ index ]->getContent ();
78+ }
79+ if (false !== \mb_strpos ($ content , '#[ORM\Entity] ' )) {
80+ return true ;
81+ }
82+ if (false !== \mb_strpos ($ content , '#[\Doctrine\ORM\Mapping\Entity ' )) {
83+ return true ;
84+ }
85+
86+ $ classIndex = $ attributeOpenIndex - 1 ;
87+ }
88+
89+ return false ;
90+ }
6491}
0 commit comments