@@ -39,10 +39,13 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
3939 continue ;
4040 }
4141
42+ // remove ones not having type at the beginning
43+ $ this ->removeVarAnnotationNotMatchingPattern ($ tokens , $ index , '/@var\s+[ \\\\a-zA-Z_\x7f-\xff]/ ' );
44+
4245 $ nextIndex = $ tokens ->getNextMeaningfulToken ($ index );
4346
4447 if ($ nextIndex === null ) {
45- $ this ->removeVarAnnotation ($ tokens , $ index );
48+ $ this ->removeVarAnnotationNotMatchingPattern ($ tokens , $ index, null );
4649 continue ;
4750 }
4851
@@ -60,37 +63,32 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
6063 continue ;
6164 }
6265
63- $ this ->removeVarAnnotation ($ tokens , $ index );
66+ $ this ->removeVarAnnotationNotMatchingPattern ($ tokens , $ index, null );
6467 }
6568 }
6669
6770 public function getPriority () : int
6871 {
69- // should be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
72+ // must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
73+ // must be after PhpdocVarAnnotationCorrectOrderFixer
7074 return 6 ;
7175 }
7276
73- private function removeVarAnnotation (Tokens $ tokens , int $ index , array $ allowedVariables = [] ) : void
77+ private function removeVarAnnotation (Tokens $ tokens , int $ index , array $ allowedVariables ) : void
7478 {
75- $ doc = new DocBlock ($ tokens [$ index ]->getContent ());
76-
77- foreach ($ doc ->getAnnotationsOfType (['var ' ]) as $ annotation ) {
78- $ allowedVariablesQuoted = \array_map (
79- static function (string $ variable ) : string {
80- return \preg_quote ($ variable , '/ ' ) . '\b ' ;
81- },
82- $ allowedVariables
83- );
84- if ($ allowedVariables === [] || \preg_match (\sprintf ('/%s/i ' , \implode ('| ' , $ allowedVariablesQuoted )), $ annotation ->getContent ()) !== 1 ) {
85- $ annotation ->remove ();
86- }
87- }
88-
89- if ($ doc ->getContent () === '' ) {
90- $ tokens ->clearTokenAndMergeSurroundingWhitespace ($ index );
91- } else {
92- $ tokens [$ index ] = new Token ([$ tokens [$ index ]->getId (), $ doc ->getContent ()]);
93- }
79+ $ this ->removeVarAnnotationNotMatchingPattern (
80+ $ tokens ,
81+ $ index ,
82+ '/ ' . \implode (
83+ '| ' ,
84+ \array_map (
85+ static function (string $ variable ) : string {
86+ return \preg_quote ($ variable , '/ ' ) . '\b ' ;
87+ },
88+ $ allowedVariables
89+ )
90+ ) . '/i '
91+ );
9492 }
9593
9694 private function removeVarAnnotationForControl (Tokens $ tokens , int $ commentIndex , int $ controlIndex ) : void
@@ -110,4 +108,21 @@ private function removeVarAnnotationForControl(Tokens $tokens, int $commentIndex
110108
111109 $ this ->removeVarAnnotation ($ tokens , $ commentIndex , $ variables );
112110 }
111+
112+ private function removeVarAnnotationNotMatchingPattern (Tokens $ tokens , int $ index , ?string $ pattern ) : void
113+ {
114+ $ doc = new DocBlock ($ tokens [$ index ]->getContent ());
115+
116+ foreach ($ doc ->getAnnotationsOfType (['var ' ]) as $ annotation ) {
117+ if ($ pattern === null || \preg_match ($ pattern , $ annotation ->getContent ()) !== 1 ) {
118+ $ annotation ->remove ();
119+ }
120+ }
121+
122+ if ($ doc ->getContent () === '' ) {
123+ $ tokens ->clearTokenAndMergeSurroundingWhitespace ($ index );
124+ } else {
125+ $ tokens [$ index ] = new Token ([$ tokens [$ index ]->getId (), $ doc ->getContent ()]);
126+ }
127+ }
113128}
0 commit comments