@@ -36,14 +36,17 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $commentStartPoi
3636
3737 $ commentContent = $ this ->getCommentContent ($ phpcsFile , $ commentStartPointer , $ commentEndPointer );
3838
39- $ isEmpty = $ this ->isLineComment ($ phpcsFile , $ commentStartPointer )
40- ? (bool ) preg_match ('~^ \\s*$~ ' , $ commentContent )
41- : (bool ) preg_match ('~^[ \\s\*]*$~ ' , $ commentContent );
39+ $ isLineComment = $ this ->isLineComment ($ phpcsFile , $ commentStartPointer );
40+ $ isEmpty = $ this ->isEmpty ($ commentContent , $ isLineComment );
4241
4342 if (!$ isEmpty ) {
4443 return ;
4544 }
4645
46+ if ($ isLineComment && $ this ->isPartOfMultilineInlineComments ($ phpcsFile , $ commentStartPointer , $ commentEndPointer )) {
47+ return ;
48+ }
49+
4750 $ fix = $ phpcsFile ->addFixableError (
4851 'Empty comment ' ,
4952 $ commentStartPointer ,
@@ -96,6 +99,13 @@ private function isLineComment(\PHP_CodeSniffer\Files\File $phpcsFile, int $comm
9699 return (bool ) preg_match ('~^(?://|#)(.*)~ ' , $ phpcsFile ->getTokens ()[$ commentPointer ]['content ' ]);
97100 }
98101
102+ private function isEmpty (string $ comment , bool $ isLineComment ): bool
103+ {
104+ return $ isLineComment
105+ ? (bool ) preg_match ('~^ \\s*$~ ' , $ comment )
106+ : (bool ) preg_match ('~^[ \\s\*]*$~ ' , $ comment );
107+ }
108+
99109 private function getCommentEndPointer (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ commentStartPointer ): ?int
100110 {
101111 $ tokens = $ phpcsFile ->getTokens ();
@@ -132,4 +142,76 @@ private function getCommentContent(\PHP_CodeSniffer\Files\File $phpcsFile, int $
132142 return substr (TokenHelper::getContent ($ phpcsFile , $ commentStartPointer , $ commentEndPointer ), 2 , -2 );
133143 }
134144
145+ private function isPartOfMultilineInlineComments (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ commentStartPointer , int $ commentEndPointer ): bool
146+ {
147+ if (!$ this ->isNonEmptyLineCommentBefore ($ phpcsFile , $ commentStartPointer )) {
148+ return false ;
149+ }
150+
151+ if (!$ this ->isNonEmptyLineCommentAfter ($ phpcsFile , $ commentStartPointer , $ commentEndPointer )) {
152+ return false ;
153+ }
154+
155+ return true ;
156+ }
157+
158+ private function isNonEmptyLineCommentBefore (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ commentStartPointer ): bool
159+ {
160+ $ tokens = $ phpcsFile ->getTokens ();
161+
162+ /** @var int $beforeCommentStartPointer */
163+ $ beforeCommentStartPointer = TokenHelper::findPreviousExcluding ($ phpcsFile , T_WHITESPACE , $ commentStartPointer - 1 );
164+
165+ if ($ tokens [$ beforeCommentStartPointer ]['code ' ] !== T_COMMENT ) {
166+ return false ;
167+ }
168+
169+ if (!$ this ->isLineComment ($ phpcsFile , $ beforeCommentStartPointer )) {
170+ return false ;
171+ }
172+
173+ if ($ tokens [$ beforeCommentStartPointer ]['line ' ] + 1 !== $ tokens [$ commentStartPointer ]['line ' ]) {
174+ return false ;
175+ }
176+
177+ /** @var int $beforeCommentEndPointer */
178+ $ beforeCommentEndPointer = $ this ->getCommentEndPointer ($ phpcsFile , $ beforeCommentStartPointer );
179+ if (!$ this ->isEmpty ($ this ->getCommentContent ($ phpcsFile , $ beforeCommentStartPointer , $ beforeCommentEndPointer ), true )) {
180+ return true ;
181+ }
182+
183+ return $ this ->isNonEmptyLineCommentBefore ($ phpcsFile , $ beforeCommentStartPointer );
184+ }
185+
186+ private function isNonEmptyLineCommentAfter (\PHP_CodeSniffer \Files \File $ phpcsFile , int $ commentStartPointer , int $ commentEndPointer ): bool
187+ {
188+ $ tokens = $ phpcsFile ->getTokens ();
189+
190+ $ afterCommentStartPointer = TokenHelper::findNextExcluding ($ phpcsFile , T_WHITESPACE , $ commentEndPointer + 1 );
191+
192+ if ($ afterCommentStartPointer === null ) {
193+ return false ;
194+ }
195+
196+ if ($ tokens [$ afterCommentStartPointer ]['code ' ] !== T_COMMENT ) {
197+ return false ;
198+ }
199+
200+ if (!$ this ->isLineComment ($ phpcsFile , $ afterCommentStartPointer )) {
201+ return false ;
202+ }
203+
204+ if ($ tokens [$ commentEndPointer ]['line ' ] + 1 !== $ tokens [$ afterCommentStartPointer ]['line ' ]) {
205+ return false ;
206+ }
207+
208+ /** @var int $afterCommentEndPointer */
209+ $ afterCommentEndPointer = $ this ->getCommentEndPointer ($ phpcsFile , $ afterCommentStartPointer );
210+ if (!$ this ->isEmpty ($ this ->getCommentContent ($ phpcsFile , $ afterCommentStartPointer , $ afterCommentEndPointer ), true )) {
211+ return true ;
212+ }
213+
214+ return $ this ->isNonEmptyLineCommentAfter ($ phpcsFile , $ afterCommentStartPointer , $ afterCommentEndPointer );
215+ }
216+
135217}
0 commit comments