1818use PhpCsFixer \FixerDefinition \FixerDefinition ;
1919use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
2020use PhpCsFixer \Preg ;
21+ use PhpCsFixer \Tokenizer \CT ;
2122use PhpCsFixer \Tokenizer \Token ;
2223use PhpCsFixer \Tokenizer \Tokens ;
2324use PhpCsFixerCustomFixers \TokenRemover ;
@@ -71,7 +72,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7172 }
7273
7374 if ($ tokens [$ nextIndex ]->isGivenKind ([\T_PRIVATE , \T_PROTECTED , \T_PUBLIC , \T_VAR , \T_STATIC ])) {
74- $ this ->removeForClassElement ($ tokens , $ index );
75+ $ this ->removeForClassElement ($ tokens , $ index, $ nextIndex );
7576 continue ;
7677 }
7778
@@ -94,20 +95,27 @@ private function isTokenCandidate(Token $token): bool
9495 return $ token ->isGivenKind (\T_DOC_COMMENT ) && \stripos ($ token ->getContent (), '@var ' ) !== false ;
9596 }
9697
97- private function removeForClassElement (Tokens $ tokens , int $ index ): void
98+ private function removeForClassElement (Tokens $ tokens , int $ index, int $ propertyStartIndex ): void
9899 {
99- /** @var int $nextIndex */
100- $ nextIndex = $ tokens ->getTokenNotOfKindsSibling ($ index , 1 , [\T_PRIVATE , \T_PROTECTED , \T_PUBLIC , \T_VAR , \T_STATIC , \T_WHITESPACE ]);
100+ $ tokenKinds = [\T_NS_SEPARATOR , \T_STATIC , \T_STRING , \T_WHITESPACE , CT ::T_ARRAY_TYPEHINT , CT ::T_NULLABLE_TYPE , CT ::T_TYPE_ALTERNATION ];
101101
102- if ($ tokens [$ nextIndex ]->isGivenKind (\T_VARIABLE )) {
103- if (Preg::match ('/@var\h+(.+\h+)?\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ ' , $ tokens [$ index ]->getContent ()) === 1 ) {
104- $ this ->removeVarAnnotation ($ tokens , $ index , [$ tokens [$ nextIndex ]->getContent ()]);
105- }
102+ if (\defined ('T_READONLY ' )) {
103+ $ tokenKinds [] = CT ::T_TYPE_INTERSECTION ;
104+ $ tokenKinds [] = \T_READONLY ;
105+ }
106+
107+ /** @var int $variableIndex */
108+ $ variableIndex = $ tokens ->getTokenNotOfKindsSibling ($ propertyStartIndex , 1 , $ tokenKinds );
109+
110+ if (!$ tokens [$ variableIndex ]->isGivenKind (\T_VARIABLE )) {
111+ $ this ->removeVarAnnotationNotMatchingPattern ($ tokens , $ index , null );
106112
107113 return ;
108114 }
109115
110- $ this ->removeVarAnnotationNotMatchingPattern ($ tokens , $ index , null );
116+ if (Preg::match ('/@var\h+(.+\h+)?\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ ' , $ tokens [$ index ]->getContent ()) === 1 ) {
117+ $ this ->removeVarAnnotation ($ tokens , $ index , [$ tokens [$ variableIndex ]->getContent ()]);
118+ }
111119 }
112120
113121 /**
0 commit comments