@@ -166,7 +166,22 @@ public function process(File $phpcsFile, $stackPtr): void
166166 return ;
167167 }
168168
169- $ tokenptr ++;
169+ // Let's jump over all the extra (allowed) consecutive comments to find the first non-comment token.
170+ $ lastComment = $ tokenptr ;
171+ $ nextComment = $ tokenptr ;
172+ while (($ nextComment = $ phpcsFile ->findNext (T_COMMENT , ($ nextComment + 1 ), null , false )) !== false ) {
173+ // Only \n is allowed as spacing since the previous comment line.
174+ if (strpos ($ tokens [$ nextComment - 1 ]['content ' ], "\n" ) === false ) {
175+ // Stop looking for consecutive comments, some spacing broke the sequence.
176+ break ;
177+ }
178+ if ($ tokens [$ nextComment ]['line ' ] !== ($ tokens [$ lastComment ]['line ' ] + 1 )) {
179+ // Stop looking for comments, the lines are not consecutive.
180+ break ;
181+ }
182+ $ lastComment = $ nextComment ;
183+ }
184+ $ tokenptr = $ lastComment + 1 ; // Move to the last found comment + 1.
170185
171186 $ nextnonwhitespace = $ phpcsFile ->findNext (T_WHITESPACE , $ tokenptr , null , true );
172187
@@ -218,8 +233,16 @@ private function fullComment(): array
218233
219234 private function insertBoilerplate (File $ file , int $ stackptr ): void
220235 {
221- $ prefix = substr ($ file ->getTokens ()[$ stackptr ]['content ' ], -1 ) === "\n" ? '' : "\n" ;
222- $ file ->fixer ->addContent ($ stackptr , $ prefix . implode ("\n" , $ this ->fullComment ()) . "\n" );
236+ $ token = $ file ->getTokens ()[$ stackptr ];
237+ $ paddedComment = implode ("\n" , $ this ->fullComment ()) . "\n" ;
238+
239+ if ($ token ['code ' ] === T_OPEN_TAG ) {
240+ $ replacement = trim ($ token ['content ' ]) . "\n" . $ paddedComment ;
241+ $ file ->fixer ->replaceToken ($ stackptr , $ replacement );
242+ } else {
243+ $ prefix = substr ($ token ['content ' ], -1 ) === "\n" ? '' : "\n" ;
244+ $ file ->fixer ->addContent ($ stackptr , $ prefix . $ paddedComment );
245+ }
223246 }
224247
225248 private function moveBoilerplate (File $ file , int $ start , int $ target ): void
@@ -265,6 +288,11 @@ private function completeBoilerplate(File $file, $stackptr, int $lineindex): voi
265288 */
266289 private function regexForLine (string $ line ): string
267290 {
291+ // We need to match the blank lines in their entirety.
292+ if ($ line === '// ' ) {
293+ return '/^\/\/$/ ' ;
294+ }
295+
268296 return str_replace (
269297 ['Moodle ' , 'https \\: ' ],
270298 ['.* ' , 'https? \\: ' ],
0 commit comments